@codeCoverageIgnore*

@codeCoverageIgnore* The @codeCoverageIgnore, @codeCoverageIgnoreStart and @codeCoverageIgnoreEnd annotations can be used to exclude lines of code from the coverage analysis. For usage see the section called “Ignoring Code Blocks”.

Database Testing

Many beginner and intermediate unit testing examples in any programming language suggest that it is perfectly easy to test your application's logic with simple tests. For database-centric applications this is far away from the reality. Start using Wordpress, TYPO3 or Symfony with Doctrine or Propel, for example, and you will easily experience considerable problems with PHPUnit: just because the database is so tightly coupled to these libraries. Make sure you have the PHP extension pdo and da

Incomplete and Skipped Tests

Incomplete Tests When you are working on a new test case class, you might want to begin by writing empty test methods such as: public function testSomething() { } to keep track of the tests that you have to write. The problem with empty test methods is that they are interpreted as a success by the PHPUnit framework. This misinterpretation leads to the test reports being useless -- you cannot see whether a test is actually successful or just not yet implemented. Calling $this->fail() in th

Organizing Tests

One of the goals of PHPUnit is that tests should be composable: we want to be able to run any number or combination of tests together, for instance all tests for the whole project, or the tests for all classes of a component that is part of the project, or just the tests for a single class. PHPUnit supports different ways of organizing tests and composing them into a test suite. This chapter shows the most commonly used approaches. Composing a Test Suite Using the Filesystem Probably the

Extending PHPUnit

PHPUnit can be extended in various ways to make the writing of tests easier and customize the feedback you get from running tests. Here are common starting points to extend PHPUnit. Subclass PHPUnit\Framework\TestCase Write custom assertions and utility methods in an abstract subclass of PHPUnit\Framework\TestCase and derive your test case classes from that class. This is one of the easiest ways to extend PHPUnit. Write custom assertions When writing custom assertions it is the best pract

@expectedExceptionMessageRegExp

@expectedExceptionMessageRegExp The expected message can also be specified as a regular expression using the @expectedExceptionMessageRegExp annotation. This is helpful for situations where a substring is not adequate for matching a given message. use PHPUnit\Framework\TestCase; class MyTest extends TestCase { /** * @expectedException MyException * @expectedExceptionMessageRegExp /Argument \d+ can not be an? \w+/ */ public function testExceptionHasRi

assertJsonStringEqualsJsonFile()

assertJsonStringEqualsJsonFile() assertJsonStringEqualsJsonFile(mixed $expectedFile, mixed $actualJson[, string $message = '']) Reports an error identified by $message if the value of $actualJson does not match the value of $expectedFile. Example A.34: Usage of assertJsonStringEqualsJsonFile() <?php use PHPUnit\Framework\TestCase; class JsonStringEqualsJsonFileTest extends TestCase { public function testFailure() { $this->assertJsonStringEqualsJsonFile( 'p

assertTrue()

assertTrue() assertTrue(bool $condition[, string $message = '']) Reports an error identified by $message if $condition is false. assertNotTrue() is the inverse of this assertion and takes the same arguments. Example A.50: Usage of assertTrue() <?php use PHPUnit\Framework\TestCase; class TrueTest extends TestCase { public function testFailure() { $this->assertTrue(false); } } ?> phpunit TrueTest PHPUnit 5.6.0 by Sebastian Bergmann and contributors. F Time: 0 sec

The Command-Line Test Runner

The PHPUnit command-line test runner can be invoked through the phpunit command. The following code shows how to run tests with the PHPUnit command-line test runner: phpunit ArrayTest PHPUnit 5.6.0 by Sebastian Bergmann and contributors. .. Time: 0 seconds OK (2 tests, 2 assertions) When invoked as shown above, the PHPUnit command-line test runner will look for a ArrayTest.php sourcefile in the current working directory, load it, and expect to find a ArrayTest test case class. It will th

The XML Configuration File

PHPUnit The attributes of the <phpunit> element can be used to configure PHPUnit's core functionality. <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd" backupGlobals="true" backupStaticAttributes="false" <!--bootstrap="/path/to/bootstrap.php"--> cacheTokens="false" colors="false" convertErrorsToExceptions="true"