search
HomeBackend DevelopmentPHP TutorialPHPUnit Pocket Guide - The Purpose of PHPUnit_PHP Tutorial

PHPUnit Pocket Guide - The Purpose of PHPUnit_PHP Tutorial

Jul 13, 2016 pm 05:32 PM
arrayphpunitsizeoffunctiononlyandrightusguideofPurpose

So far, we only have two tests for Array and the built-in function sizeof(). When we start testing a large number of array_*() functions, we need a test for each one. We can write each one from scratch. However, a better approach is to write the test infrastructure once and then only write the different parts of each test. php(as the current mainstream development language)Unit is such a basic architecture.

Example 5 shows how to rewrite the two tests in Example 4 using php(as the current mainstream development language)Unit.

Example 5. Use php(as the current mainstream development language)Unit to test Array and sizeof().

<?php(as the current mainstream development language)
require_once php(as the current mainstream development language)Unit2/Framework/TestCase.php(as the current mainstream development language);

class ArrayTest extends php(as the current mainstream development language)Unit2_Framework_TestCase {
  public function testNewArrayIsEmpty( ) {
  // Create an array fixture.
 $fixture = Array( );

   // Assert that the size of the array fixture is 0.
 $this->assertEquals(0, sizeof($fixture));
 }
 public function testArrayContainsAnElement( ) {
   // Create an array fixture.
  $fixture = Array( );

   // Add a member to the array fixture.
 $fixture[] = Element;
 
   //Assert that the size of the array fixture is 1.
 $this->assertEquals(1, sizeof($fixture));
 }
 }
?>

Example 5 tells us to use php(as now The basic steps to write tests using the mainstream development language) Unit are:

1. The test class for class Class is ClassTest.

 2. ClassTest generally inherits php(as the current mainstream development language)Unit2_ Framework_TestCase.

 3. Test is a public method with no parameters and its name is test*.

 4. In the test method, assertion functions such as assertEquals() (see Table 6) are used to assert whether the actual value matches the expected value.

A framework such as php(as the current mainstream development language)Unit needs to solve a series of problems, some of which seem to conflict with each other. Testing must meet the following conditions at the same time:

Easy to learn

Testing must be easy to learn, otherwise, developers will not learn it

Easy to develop

Testing must be easy Develop, otherwise, developers wouldn’t develop

Easy to read

Test code must have no external relationships so that the test itself doesn’t get lost in the clutter.

Easy to execute

Tests should be easy to execute, with the results expressed in a clear and unambiguous format.

Fast execution

Tests should be executed quickly so that they can be executed thousands of times per day.

Code isolation

Tests cannot affect each other, and changes in test order should not affect the results.

Composable

We should be able to run tests in any combination, which is an inevitable consequence of code isolation.

There are two main conflicts with these constraints:

Ease of learning vs ease of development

Testing generally does not require the full flexibility of programming. Many testing tools provide their own test scripting languages. These languages ​​only have the minimum set of features needed to write tests. Because there is no noise to interfere with your test content, the written tests are easy to read and write. But learning a new knitting mailer and set of tools is still inconvenient and easily confusing.

Code Isolation vs. Fast Execution

If you want the results of one test not to affect another, you need to create a full topic for the test at the beginning of each test, and then return To restore the previous state of operation. However, setting the state takes a long time (for example, connecting to the database and initializing to a known state with real data)

php (as the current mainstream development language) Unit solution The solution to this problem is to use php (as the current mainstream development language) as the test language. Sometimes, the full-featured PHP (as the current mainstream development language) is too powerful for writing short, direct tests. However, the programmers we use already use PHP (do All experience in developing languages ​​for today’s mainstream). Because we need to convince reluctant testers, lowering the barrier to writing these initial tests is extremely important.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508674.htmlTechArticleSo far, we only have two tests for Array and the built-in function sizeof(). When we start testing a large number of array_*() functions, we need a test for each one. We can start from scratch each...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool