Home  >  Article  >  Backend Development  >  PHPUnit Pocket Guide - The Purpose of PHPUnit_PHP Tutorial

PHPUnit Pocket Guide - The Purpose of PHPUnit_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:32:45923browse

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