Example 4.1 shows how to write a test with PHPUnit to test the PHP array operation. This example introduces the basic practices and procedures for writing tests with PHPUnit:
Tests for class classes are written in class Classtest.
Classtest (usually) inherits from Phpunit_framework_testcase.
Tests are common methods named test*.
In addition, you can use @test annotations in the method's document comment block (DocBlock) to mark it as a test method.
Within the test method, the assertion method, similar to Assertequals () (See the "Assertion" section), is used to assert that the actual value matches the expected value.
Example 4.1: Test array operations with PHPUnit
<?php
Class Stacktest extends Phpunit_framework_testcase
{
Public Function Testpushandpop ()
{
$stack = Array ();
$this->assertequals (0, Count ($stack));
Array_push ($stack, ' foo ');
$this->assertequals (' foo ', $stack [count ($stack)-1]);
$this->assertequals (1, Count ($stack));
$this->assertequals (' foo ', Array_pop ($stack));
$this->assertequals (0, Count ($stack));
}
}
?>
|
When you want to write something to a print statement or debug an expression, don't do it, write it as a test instead. |
|
|
--martin Fowler |
Test Dependencies
|
Unit testing is written primarily as a good practice that helps developers identify and fix bugs, refactor code, and can be viewed as documents for software units under test. To achieve these benefits, the ideal unit test should overwrite all possible paths in the program. A unit test typically overwrites a specific path in a function or method. However, the test method does not necessarily have to be a self-contained entity with good encapsulation. Often implicit dependencies between test methods are hidden in the implementation of the test. |
|
|
--adrian Kuhn et. Al. |
PHPUnit supports the declaration of explicit dependencies between test methods. This dependency is not defined in the execution order of the test method, but rather allows the producer (producer) to return an instance of the test base (fixture) and pass this instance on to the consumer (consumer) who is dependent on it.
A producer (producer) is a test method that generates a measured unit and takes it as a return value.
A consumer (consumer) is a test method that relies on one or more producers and their return values.
Example 4.2 shows how to use @depends annotations to express the dependencies between test methods.
Example 4.2: Using @depends annotations to express dependencies
<?php
Class Stacktest extends Phpunit_framework_testcase
{
Public Function Testempty ()
{
$stack = Array ();
$this->assertempty ($stack);
return $stack;
}
/**
* @depends Testempty
*/
Public function Testpush (array $stack)
{
Array_push ($stack, ' foo ');
$this->assertequals (' foo ', $stack [count ($stack)-1]);
$this->assertnotempty ($stack);
return $stack;
}
/**
* @depends Testpush
*/
Public function Testpop (array $stack)
{
$this->assertequals (' foo ', Array_pop ($stack));
$this->assertempty ($stack);
}
}
?>
In the example above, the first Test, Testempty (), creates a new array and asserts that it is empty. The test then returns the base as a result. The second Test, Testpush (), relies on