Test
to the name of the class being tested. For example, the class under test is called Class
, testing - ClassTest
.ClassTest
inherited (in most cases) from PHPUnit_Framework_TestCase.test
prefix to the names of the tested methods.assertEquals()
assertEquals()
sets the match between the actual value and the expected value.<?php<br>
require_once 'PHPUnit/Framework.php' ;<br>
<br>
class StackTest extends PHPUnit_Framework_TestCase<br>
{<br>
public function testPushAndPop()<br>
{<br>
$stack = array();<br>
$ this ->assertEquals(0, count($stack));<br>
<br> array_push($stack, 'foo' );<br>
$ this ->assertEquals( 'foo' , $stack[count($stack)-1]);<br>
$ this ->assertEquals(1, count($stack));<br>
<br> $ this ->assertEquals( 'foo' , array_pop($stack));<br>
$ this ->assertEquals(0, count($stack));<br>
}<br>
}<br>
?> <br>
<br>
* This source code was highlighted with Source Code Highlighter .
print
function or @depends
annotation to describe dependencies between tests.@depends
annotation to describe dependenciesIn this example, the first test,<?php<br>
class StackTest extends PHPUnit_Framework_TestCase<br>
{<br>
public function testEmpty()<br>
{<br>
$stack = array();<br>
$ this ->assertTrue(empty($stack));<br>
<br>
return $stack;<br>
}<br>
<br>
/** <br>
* @depends testEmpty <br>
*/ <br>
public function testPush(array $stack)<br>
{<br>
array_push($stack, 'foo' );<br>
$ this ->assertEquals( 'foo' , $stack[count($stack)-1]);<br>
$ this ->assertFalse(empty($stack));<br>
<br>
return $stack;<br>
}<br>
<br>
/** <br>
* @depends testPush <br>
*/ <br>
public function testPop(array $stack)<br>
{<br>
$ this ->assertEquals( 'foo' , array_pop($stack));<br>
$ this ->assertTrue(empty($stack));<br>
}<br>
}<br>
?> <br>
<br>
* This source code was highlighted with Source Code Highlighter .
testEmpty()
, creates an empty array and sets the statement that the array is empty. After this, the test returns the fixture environment as a result.testPush()
, depends on testEmpty()
and receives the result of the testEmpty()
operation as an argument. Finally, testPop()
depends on testPush()
.<?php<br>
class DependencyFailureTest extends PHPUnit_Framework_TestCase<br>
{<br>
public function testOne()<br>
{<br>
$ this ->assertTrue(FALSE);<br>
}<br>
<br>
/** <br>
* @depends testOne <br> */ <br>
public function testTwo()<br> {<br>
}<br>}<br>?> <br>
<br>
* This source code was highlighted with Source Code Highlighter .
phpunit --verbose DependencyFailureTest PHPUnit 3.4.2 by Sebastian Bergmann. DependencyFailureTest FS Time: 0 seconds There was 1 failure: 1) testOne (DependencyFailureTest) Failed asserting that <boolean: false> is true. /home/sb/DependencyFailureTest.php:6 There was 1 skipped test: 1) testTwo (DependencyFailureTest) This test depends on "DependencyFailureTest :: testOne" to pass. FAILURES! Tests: 2, Assertions: 1, Failures: 1, Skipped: 1.
@depends
annotations.provider()
, see Example 4.4).@dataProvider
annotation.public
and must return an array of arrays or an object that supports the Iterator
interface, which returns an array at each iteration.<?php<br>
class DataTest extends PHPUnit_Framework_TestCase<br>
{<br>
/** <br>
* @dataProvider provider <br>
*/ <br>
public function testAdd($a, $b, $c)<br>
{<br>
$ this ->assertEquals($c, $a + $b);<br>
}<br>
<br>
public function provider()<br>
{<br>
return array(<br>
array(0, 0, 0),<br>
array(0, 1, 1),<br>
array(1, 0, 1),<br>
array(1, 1, 3)<br>
);<br>
}<br>
}<br>
?> <br>
<br>
* This source code was highlighted with Source Code Highlighter .
phpunit DataTest PHPUnit 3.4.2 by Sebastian Bergmann. ... f Time: 0 seconds There was 1 failure: 1) testAdd (DataTest) with data (1, 1, 3) Failed asserting that <integer: 2> matches expected value <integer: 3>. /home/sb/DataTest.php:21 FAILURES! Tests: 4, Assertions: 4, Failures: 1.
@dataProvider
) and one or several tests that are defined as dependent ( @depends
), then the data source is used first and only then other tests.@expectedException
annotation for testing.<?php<br>
require_once 'PHPUnit/Framework.php' ;<br>
<br>
class ExceptionTest extends PHPUnit_Framework_TestCase<br>
{<br>
/** <br>
* @expectedException InvalidArgumentException <br>
*/ <br>
public function testException()<br>
{<br>
}<br>
}<br>
?> <br>
<br>
* This source code was highlighted with Source Code Highlighter .
phpunit ExceptionTest PHPUnit 3.4.2 by Sebastian Bergmann. F Time: 0 seconds There was 1 failure: 1) testException (ExceptionTest) Expected exception InvalidArgumentException FAILURES! Tests: 1, Assertions: 1, Failures: 1.
setExpectedException()
method is another way to indicate that an exception is thrown in the test method, see Example 4.6.<?php<br>
require_once 'PHPUnit/Framework.php' ;<br>
<br>
class ExceptionTest extends PHPUnit_Framework_TestCase<br>
{<br>
public function testException()<br>
{<br>
$ this ->setExpectedException( 'InvalidArgumentException' );<br>
}<br>
}<br>
?> <br>
<br>
* This source code was highlighted with Source Code Highlighter .
phpunit ExceptionTest PHPUnit 3.4.2 by Sebastian Bergmann. F Time: 0 seconds There was 1 failure: 1) testException (ExceptionTest) Expected exception InvalidArgumentException FAILURES! Tests: 1, Assertions: 1, Failures: 1.
void setExpectedException(string $exceptionName)
$exceptionName
.String getExpectedException()
<?php<br>
require_once 'PHPUnit/Framework.php' ;<br>
<br>
class ExceptionTest extends PHPUnit_Framework_TestCase {<br>
public function testException() {<br>
try {<br>
// ... Code that is expected to raise an exception ... <br>
}<br>
<br>
catch (InvalidArgumentException $expected) {<br>
return ;<br>
}<br>
<br>
$ this ->fail( 'An expected exception has not been raised.' );<br>
}<br>
}<br>
?> <br>
<br>
* This source code was highlighted with Source Code Highlighter .
fail()
method will be called, (seecatch
block will work and the test will end successfully.<?php<br>
class ExpectedErrorTest extends PHPUnit_Framework_TestCase<br>
{<br>
/** <br>
* @expectedException PHPUnit_Framework_Error <br>
*/ <br>
public function testFailingInclude()<br>
{<br>
include 'not_existing_file.php' ;<br>
}<br>
}<br>
?> <br>
<br>
* This source code was highlighted with Source Code Highlighter .
phpunit ExpectedErrorTest PHPUnit 3.4.2 by Sebastian Bergmann. . Time: 0 seconds OK (1 test, 1 assertion)
PHPUnit_Framework_Error_Notice
and PHPUnit_Framework_Error_Warning
Source: https://habr.com/ru/post/89175/
All Articles