3. Annotations

An annotation is a special form of syntactic metadata that can be added to the source code of some programming languages. Until PHP 8, PHP had no dedicated language feature for attaching metadata to units of code. The usage of tags such as @annotation arguments in a documentation block has been established in the PHP community to annotate source code since the time of PHP 4. Documentation blocks can be accessed through the Reflection API’s getDocComment() method on the function, class, method, and attribute level. Applications such as PHPUnit can use this information at runtime to access annotations in documentation blocks.

Note

A doc comment in PHP must start with /** and end with */. Annotations in any other style of comment will be ignored.

Prior to PHPUnit 10, annotations in special PHP comments, so-called “DocBlocks” or “doc-comments”, were the only means of attaching metadata to code units. These annotations are documented in this appendix.

PHPUnit will first look for metadata in attributes before it looks for annotations in comments. When metadata is found in attributes, metadata in comments is ignored. Support for metadata in comments is closed for further development: bugs will be fixed, but no new functionality will be implemented based on annotations.

Note

Do not use annotations in comments in new tests that you write. Use attributes instead.

@author

The @author annotation is an alias for the @group annotation (see @group) and allows to filter tests based on their authors.

@after

The @after annotation can be used to specify methods that should be called after each test method in a test case class.

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class MyTest extends TestCase
  4. {
  5. /**
  6. * @after
  7. */
  8. public function tearDownSomeFixtures(): void
  9. {
  10. // ...
  11. }
  12. /**
  13. * @after
  14. */
  15. public function tearDownSomeOtherFixtures(): void
  16. {
  17. // ...
  18. }
  19. }

@afterClass

The @afterClass annotation can be used to specify static methods that should be called after all test methods in a test class have been run to clean up shared fixtures.

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class MyTest extends TestCase
  4. {
  5. /**
  6. * @afterClass
  7. */
  8. public static function tearDownSomeSharedFixtures(): void
  9. {
  10. // ...
  11. }
  12. /**
  13. * @afterClass
  14. */
  15. public static function tearDownSomeOtherSharedFixtures(): void
  16. {
  17. // ...
  18. }
  19. }

@backupGlobals

PHPUnit can optionally backup all global and super-global variables before each test and restore this backup after each test.

The @backupGlobals enabled annotation can be used on the class level to enable this operation for all tests of a test case class:

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. /**
  4. * @backupGlobals enabled
  5. */
  6. final class MyTest extends TestCase
  7. {
  8. // ...
  9. }

The @backupGlobals annotation can also be used on the test method level. This allows for a fine-grained configuration of the backup and restore operations:

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. /**
  4. * @backupGlobals enabled
  5. */
  6. final class MyTest extends TestCase
  7. {
  8. public function testThatInteractsWithGlobalVariables()
  9. {
  10. // ...
  11. }
  12. /**
  13. * @backupGlobals disabled
  14. */
  15. public function testThatDoesNotInteractWithGlobalVariables(): void
  16. {
  17. // ...
  18. }
  19. }

@backupStaticAttributes

PHPUnit can optionally backup all static attributes in all declared classes before each test and restore this backup after each test.

The @backupStaticAttributes enabled annotation can be used on the class level to enable this operation for all tests of a test case class:

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. /**
  4. * @backupStaticAttributes enabled
  5. */
  6. final class MyTest extends TestCase
  7. {
  8. // ...
  9. }

The @backupStaticAttributes annotation can also be used on the test method level. This allows for a fine-grained configuration of the backup and restore operations:

  1. use PHPUnit\Framework\TestCase;
  2. /**
  3. * @backupStaticAttributes enabled
  4. */
  5. class MyTest extends TestCase
  6. {
  7. public function testThatInteractsWithStaticAttributes(): void
  8. {
  9. // ...
  10. }
  11. /**
  12. * @backupStaticAttributes disabled
  13. */
  14. public function testThatDoesNotInteractWithStaticAttributes(): void
  15. {
  16. // ...
  17. }
  18. }

Note

@backupStaticAttributes is limited by PHP internals and may cause unintended static values to persist and leak into subsequent tests in some circumstances.

See Global State for details.

@before

The @before annotation can be used to specify methods that should be called before each test method in a test case class.

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class MyTest extends TestCase
  4. {
  5. /**
  6. * @before
  7. */
  8. public function setupSomeFixtures(): void
  9. {
  10. // ...
  11. }
  12. /**
  13. * @before
  14. */
  15. public function setupSomeOtherFixtures(): void
  16. {
  17. // ...
  18. }
  19. }

@beforeClass

The @beforeClass annotation can be used to specify static methods that should be called before any test methods in a test class are run to set up shared fixtures.

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class MyTest extends TestCase
  4. {
  5. /**
  6. * @beforeClass
  7. */
  8. public static function setUpSomeSharedFixtures(): void
  9. {
  10. // ...
  11. }
  12. /**
  13. * @beforeClass
  14. */
  15. public static function setUpSomeOtherSharedFixtures(): void
  16. {
  17. // ...
  18. }
  19. }

@codeCoverageIgnore*

The @codeCoverageIgnore, @codeCoverageIgnoreStart and @codeCoverageIgnoreEnd annotations can be used to exclude lines of code from the coverage analysis.

For usage see Ignoring Code Blocks.

@covers

The @covers annotation can be used in the test code to specify which parts of the code it is supposed to test:

  1. /**
  2. * @covers \BankAccount
  3. */
  4. public function testBalanceIsInitiallyZero(): void
  5. {
  6. $this->assertSame(0, $this->ba->getBalance());
  7. }

If provided, this effectively filters the code coverage report to include executed code from the referenced code parts only. This will make sure that code is only marked as covered if there are dedicated tests for it, but not if it used indirectly by the tests for a different class, thus avoiding false positives for code coverage.

This annotation can be added to the docblock of the test class or the individual test methods. The recommended way is to add the annotation to the docblock of the test class, not to the docblock of the test methods.

When the requireCoverageMetadata configuration option in the configuration file is set to true, every test method needs to have an associated @covers annotation (either on the test class or the individual test method).

appendixes.annotations.covers shows the syntax of the @covers annotation. The section Specifying Covered Code Parts provides longer examples for using the annotation.

Please note that this annotation requires a fully-qualified class name (FQCN). To make this more obvious to the reader, it is recommended to use a leading backslash (even if this not required for the annotation to work correctly).

@covers ClassName (recommended)

Specifies that the annotated test class covers all methods of a given class.

@covers ClassName::methodName (not recommended)

Specifies that the annotated test class covers the specified method.

@covers ::functionName (recommended)

Specifies that the annotated test class covers the specified global function.

@coversDefaultClass

The @coversDefaultClass annotation can be used to specify a default namespace or class name. That way long names don’t need to be repeated for every @covers annotation. See Example 3.1.

Please note that this annotation requires a fully-qualified class name (FQCN). To make this more obvious to the reader, it is recommended to use a leading backslash (even if this not required for the annotation to work correctly).

Example 3.1 Using @coversDefaultClass to shorten annotations

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. /**
  4. * @coversDefaultClass \Foo\CoveredClass
  5. */
  6. final class CoversDefaultClassTest extends TestCase
  7. {
  8. /**
  9. * @covers ::publicMethod
  10. */
  11. public function testSomething(): void
  12. {
  13. $o = new Foo\CoveredClass;
  14. $o->publicMethod();
  15. }
  16. }

@coversNothing

The @coversNothing annotation can be used in the test code to specify that no code coverage information will be recorded for the annotated test case.

This can be used for integration testing. See A test that specifies that it does not want to contribute to code coverage for an example.

The annotation can be used on the class and the method level and will override any @covers tags.

@dataProvider

A test method can accept arbitrary arguments. These arguments are to be provided by one or more data provider methods (provider() in Using a data provider that returns an array of arrays). The data provider method to be used is specified using the @dataProvider annotation.

See Data Providers for more details.

@depends

PHPUnit supports the declaration of explicit dependencies between test methods. Such dependencies do not define the order in which the test methods are to be executed but they allow the returning of an instance of the test fixture by a producer and passing it to the dependent consumers. Using the Depends attribute to express dependencies shows how to use the @depends annotation to express dependencies between test methods.

See Test Dependencies for more details.

@doesNotPerformAssertions

Prevents a test that performs no assertions from being considered risky.

@group

A test can be tagged as belonging to one or more groups using the @group annotation like this

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class MyTest extends TestCase
  4. {
  5. /**
  6. * @group specification
  7. */
  8. public function testSomething(): void
  9. {
  10. }
  11. /**
  12. * @group regression
  13. * @group bug2204
  14. */
  15. public function testSomethingElse(): void
  16. {
  17. }
  18. }

The @group annotation can also be provided for the test class. It is then “inherited” to all test methods of that test class.

Tests can be selected for execution based on groups using the --group and --exclude-group options of the command-line test runner or using the respective directives of the XML configuration file.

@large

The @large annotation is an alias for @group large.

If the PHP_Invoker package is installed and strict mode is enabled, a large test will fail if it takes longer than 60 seconds to execute. This timeout is configurable via the timeoutForLargeTests attribute in the XML configuration file.

@medium

The @medium annotation is an alias for @group medium. A medium test must not depend on a test marked as @large.

If the PHP_Invoker package is installed and strict mode is enabled, a medium test will fail if it takes longer than 10 seconds to execute. This timeout is configurable via the timeoutForMediumTests attribute in the XML configuration file.

@preserveGlobalState

When a test is run in a separate process, PHPUnit will attempt to preserve the global state from the parent process by serializing all globals in the parent process and unserializing them in the child process. This can cause problems if the parent process contains globals that are not serializable. To fix this, you can prevent PHPUnit from preserving global state with the @preserveGlobalState annotation.

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class MyTest extends TestCase
  4. {
  5. /**
  6. * @runInSeparateProcess
  7. * @preserveGlobalState disabled
  8. */
  9. public function testInSeparateProcess(): void
  10. {
  11. // ...
  12. }
  13. }

@requires

The @requires annotation can be used to skip tests when common preconditions, like the PHP Version or installed extensions, are not met.

Table 3.1 Possible @requires usages

Type

Possible Values

Examples

Another example

PHP

Any PHP version identifier along with an optional operator

@requires PHP 7.1.20

@requires PHP >= 7.2

PHPUnit

Any PHPUnit version identifier along with an optional operator

@requires PHPUnit 7.3.1

@requires PHPUnit < 8

OS

A regexp matching PHP_OS

@requires OS Linux

@requires OS WIN32|WINNT

OSFAMILY

Any OS family

@requires OSFAMILY Solaris

@requires OSFAMILY Windows

function

Any valid parameter to function_exists

@requires function imap_open

@requires function ReflectionMethod::setAccessible

extension

Any extension name along with an optional version identifier and optional operator

@requires extension mysqli

@requires extension redis >= 2.2.0

The following operators are supported for PHP, PHPUnit, and extension version constraints: <, <=, >, >=, =, ==, !=, <>.

Versions are compared using PHP’s version_compare function. Among other things, this means that the = and == operator can only be used with complete X.Y.Z version numbers and that just X.Y will not work.

@runTestsInSeparateProcesses

Indicates that all tests in a test class should be run in a separate PHP process.

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. /**
  4. * @runTestsInSeparateProcesses
  5. */
  6. final class MyTest extends TestCase
  7. {
  8. // ...
  9. }

Note: By default, PHPUnit will attempt to preserve the global state from the parent process by serializing all globals in the parent process and unserializing them in the child process. This can cause problems if the parent process contains globals that are not serializable. See @preserveGlobalState for information on how to fix this.

@runInSeparateProcess

Indicates that a test should be run in a separate PHP process.

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class MyTest extends TestCase
  4. {
  5. /**
  6. * @runInSeparateProcess
  7. */
  8. public function testInSeparateProcess(): void
  9. {
  10. // ...
  11. }
  12. }

@small

The @small annotation is an alias for @group small. A small test must not depend on a test marked as @medium or @large.

If the PHP_Invoker package is installed and strict mode is enabled, a small test will fail if it takes longer than 1 second to execute. This timeout is configurable via the timeoutForSmallTests attribute in the XML configuration file.

Note

Tests need to be explicitly annotated by either @small, @medium, or @large to enable run time limits.

@test

As an alternative to prefixing your test method names with test, you can use the @test annotation in a method’s DocBlock to mark it as a test method.

  1. /**
  2. * @test
  3. */
  4. public function initialBalanceShouldBe0(): void
  5. {
  6. $this->assertSame(0, $this->ba->getBalance());
  7. }

@testdox

Specifies an alternative description used when generating the agile documentation sentences.

The @testdox annotation can be applied to both test classes and test methods.

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. /**
  4. * @testdox A bank account
  5. */
  6. final class BankAccountTest extends TestCase
  7. {
  8. /**
  9. * @testdox has an initial balance of zero
  10. */
  11. public function balanceIsInitiallyZero(): void
  12. {
  13. $this->assertSame(0, $this->ba->getBalance());
  14. }
  15. }

Note

Prior to PHPUnit 7.0 (due to a bug in the annotation parsing), using the @testdox annotation also activated the behaviour of the @test annotation.

When using the @testdox annotation at method level with a @dataProvider you may use the method parameters as placeholders in your alternative description. $_dataName is available in addition to use the actual name of the current data. That would be data set 1 up to 4 in below example.

  1. /**
  2. * @dataProvider additionProvider
  3. * @testdox Adding $a to $b results in $expected
  4. */
  5. public function testAdd($a, $b, $expected)
  6. {
  7. $this->assertSame($expected, $a + $b);
  8. }
  9. public static function additionProvider()
  10. {
  11. return [
  12. 'data set 1' => [0, 0, 0],
  13. 'data set 2' => [0, 1, 1],
  14. 'data set 3' => [1, 0, 1],
  15. 'data set 4' => [1, 1, 3]
  16. ];
  17. }

@testWith

Instead of implementing a method for use with @dataProvider, you can define a data set using the @testWith annotation.

A data set consists of one or many elements. To define a data set with multiple elements, define each element in a separate line. Each element of the data set must be an array defined in JSON.

See Data Providers to learn more about passing a set of data to a test.

  1. /**
  2. * @testWith ["test", 4]
  3. * ["longer-string", 13]
  4. */
  5. public function testStringLength(string $input, int $expectedLength): void
  6. {
  7. $this->assertSame($expectedLength, strlen($input));
  8. }

An object representation in JSON will be converted into an associative array.

  1. /**
  2. * @testWith [{"day": "monday", "conditions": "sunny"}, ["day", "conditions"]]
  3. */
  4. public function testArrayKeys(array $array, array $keys): void
  5. {
  6. $this->assertSame($keys, array_keys($array));
  7. }

@ticket

The @ticket annotation is an alias for the @group annotation (see @group) and allows to filter tests based on their ticket ID.

@uses

The @uses annotation specifies code which will be executed by a test, but is not intended to be covered by the test. A good example is a value object which is necessary for testing a unit of code.

  1. /**
  2. * @covers \BankAccount
  3. * @uses \Money
  4. */
  5. public function testMoneyCanBeDepositedInAccount(): void
  6. {
  7. // ...
  8. }

Example 7.1 shows another example.

In addition to being helpful for persons reading the code, this annotation is useful in strict coverage mode where unintentionally covered code will cause a test to fail. See Unintentionally Covered Code for more information regarding strict coverage mode.

Please note that this annotation requires a fully-qualified class name (FQCN). To make this more obvious to the reader, it is recommended to use a leading backslash (even if this is not required for the annotation to work correctly).