Using PHPUnit Command-Line Test Runner
Introduction
PHPUnit command line test runner is used to run PHPUnit tests at the terminal. This
command line test runner can be invoked using the `phpunit` command. The code snippet below shows how to run tests with the PHPUnit command-line test runner:
$ phpunit ArrayTest
PHPUnit |version|.0 by Sebastian Bergmann and contributors.
..
Time: 0 seconds
OK (2 tests, 2 assertions)
When the above command is invoked as shown, the PHPUnit command-line test runner will look for an `ArrayTest.php` file in the current working directory, load it, and expect to find an `ArrayTest` test class in it. It will then execute the tests of this class.
For each test executed, the PHPUnit command-line tool uses one character to indicate the progress of the test being executed. Some of these characters include:
- `.` This is printed when a test succeeds.
- `F` This indicates that an assertion failed during the execution of a test method.
- `E` Indicates the occurrence of an error while running a test method.
- `R` Printed when the test has been marked as risky.
- `S` This is printed when a test has been skipped
- `I` Indicates tests marked as being incomplete or not yet implemented.
PHPUnit distinguishes between failures and errors. A failure is a violated PHPUnit assertion such as a failing `assertSame()` call.
An error is an unexpected exception or a PHP error. Sometimes this distinction proves useful since errors tend to be easier to fix than failures.
#Command-Line Options
PHPUnit has a lot of command line options engineered to making out PHPUnit test easy and seamless.The command `phpunit --help` will give you a comprehensive list of all PHPUnit command line options as shown below.
$ phpunit --help
PHPUnit |version|.0 by Sebastian Bergmann and contributors.
Usage:
phpunit [options] UnitTest [UnitTest.php]
phpunit [options] <directory>
Code Coverage Options:
--coverage-clover <file> | Generate code coverage report in Clover XML format |
--coverage-crap4j <file> | Generate code coverage report in Crap4J XML format |
--coverage-html <dir> | Generate code coverage report in HTML format |
--coverage-php <file> | Export PHP_CodeCoverage object to file |
--coverage-text=<file> | Generate code coverage report in text format Default: Standard output |
--coverage-xml <dir> | Generate code coverage report in PHPUnit XML format |
--whitelist <dir> | Whitelist <dir> for code coverage analysis |
--disable-coverage-ignore | Disable annotations for ignoring code coverage |
--no-coverage | Ignore code coverage configuration |
--dump-xdebug-filter <file> | Generate script to set Xdebug code coverage filter |
Logging Options:
--log-junit <file> | Log test execution in JUnit XML format to file |
--log-teamcity <file> | Log test execution in TeamCity format to file |
--testdox-html <file> | Write agile documentation in HTML format to file |
--testdox-text <file> | Write agile documentation in Text format to file |
--testdox-xml <file> | Write agile documentation in XML format to file |
--reverse-list | Print defects in reverse order |
Test Selection Options:
--filter <pattern> | Filter which tests to run |
--testsuite <name,...> | Filter which testsuite to run |
--group ... | Only runs tests from the specified group(s) |
--exclude-group ... | Exclude tests from the specified group(s) |
--list-groups | List available test groups |
--list-suites | List available test suites |
--list-tests | List available test |
--list-tests-xml <file> | List available tests in XML format |
--test-suffix ... | Only search for the test in files with a specified suffix(es). Default: Test.php,.phpt |
Test Execution Options:
--dont-report-useless-tests |
Do not report tests that do not test anything |
--strict-coverage |
Be strict about @covers annotation usage |
--strict-global-state | Be strict about changes to global state |
--testdox-text <file> | Write agile documentation in Text format to file |
--testdox-xml <file> | Write agile documentation in XML format to file |
--disallow-test-output | Be strict about output during tests |
--disallow-resource-usage | Be strict about resource usage during small tests |
--enforce-time-limit | Enforce time limit based on test size |
--default-time-limit=<sec> | Timeout in seconds for tests without @small, @medium or @large |
--disallow-todo-tests | Disallow @todo-annotated tests |
--process-isolation | Run each test in a separate PHP process |
--globals-backup | Backup and restore $GLOBALS for each test |
--static-backup | Backup and restore static attributes for each test |
--colors=<flag> | Use colors in output ("never", "auto" or "always") |
--columns <n> | Number of columns to use for progress output |
--columns max | Use a maximum number of columns for progress output |
--stderr | Write to STDERR instead of STDOUT |
--stop-on-defect | Stop execution upon first not-passed test |
--stop-on-error | Stop execution upon first error |
--stop-on-failure | Stop execution upon first error or failure |
--stop-on-warning | Stop execution upon first warning |
--stop-on-risky | Stop execution upon first risky test |
--stop-on-skipped | Stop execution upon first skipped test |
--stop-on-incomplete | Stop execution upon first incomplete test |
--fail-on-warning | Treat tests with warnings as failures |
--fail-on-risky | Treat risky tests as failures |
-v|--verbose | Output more verbose information |
--debug | Display debugging information |
--loader <loader> | TestSuiteLoader implementation to use |
--repeat <times> | Runs the test(s) repeatedly |
--teamcity | Report test execution progress in TeamCity format |
--testdox | Report test execution progress in TestDox format |
--testdox-group | Only include tests from the specified group(s) |
--testdox-exclude-group | Exclude tests from the specified group(s) |
--printer <printer> | TestListener implementation to use |
--resolve-dependencies | Resolve dependencies between tests |
--order-by=<order> | Run tests in order: default|reverse|random|defects|depends |
--random-order-seed=<N> | Use a specific random seed <N> for random order |
--cache-result | Write run result to cache to enable ordering tests defects-first |
Configuration Options:
--prepend <file> | A PHP script that is included as early as possible |
--bootstrap <file> | A PHP script that is included before the tests run |
-c|--configuration <file> | Read configuration from XML file |
--no-configuration | Ignore default configuration file (phpunit.xml) |
--no-logging | Ignore logging configuration |
--no-extensions | Do not load PHPUnit extensions |
--include-path <path(s)> | Prepend PHP's include_path with given path(s) |
-d key[=value] | Sets a php.ini value |
--generate-configuration | Generate configuration file with suggested settings |
--cache-result-file==<FILE> | Specify result cache path and filename |
Miscellaneous Options:
-h|--help | Prints this usage information |
--version | Prints the version and exits |
--atleast-version <min> | Checks that version is greater than min and exits |
--check-version | Check whether PHPUnit is the latest version |
#Command-Line Commands
`phpunit UnitTest`
This command the tests that are provided by the class `UnitTest`. This class is expected to be declared in the `UnitTest.php` file.
`UnitTest` must be either a class that inherits from `PHPUnit\Framework\TestCase` or a class that provides a `public static suite()` method which returns a `PHPUnit\Framework\Test` object, for example an instance of the `PHPUnit\Framework\TestSuite` class.
`phpunit UnitTest UnitTest.php`
Runs the tests that are provided by the class `UnitTest`. This class is expected to be declared in the specified source file.
Some optional flags can also be passed to the above command, some of these optional flags are:
`--coverage-clover`
Generates a logfile in XML format with the code coverage information for the tests run.
It should be noted that this functionality is only available when the tokenizer and Xdebug extensions are installed.
`--coverage-crap4j`
Generates a code coverage report in Crap4j format. This functionality is also only available upon the installation of tokenizer and Xdebug extensions.
`--coverage-html`
Generates a code coverage report in HTML format. This option also only works when the tokenizer and Xdebug extensions are installed.
`--coverage-php`
This generates a serialized PHP_CodeCoverage object with the code coverage information.
Please note that this functionality is only available when the tokenizer and Xdebug extensions are installed.
`--coverage-text`
Generates a logfile or command-line output in human readable format with the code coverage information for the tests run. This functionality is only available when the tokenizer and Xdebug extensions are installed.
`--log-junit`
Generates a logfile in JUnit XML format for the tests run.
`--testdox-html` and `--testdox-text`
Generates agile documentation in HTML or plain text format for the tests that are run
`--filter`
Only runs tests whose name matches the given regular expression pattern. If the pattern is not enclosed in delimiters, PHPUnit will enclose the pattern in `/` delimiters.
The test names to match will be in one of the following formats:
`TestNameSpace\TestCaseClass::testMethod`
The default test name format is the equivalent of using the `__METHOD__` magic constant inside the test method.
`TestNamespace\TestCaseClass::testMethod with data set #0`
When a test has a data provider, each iteration of the data gets the current index appended to the end of the default test name.
`TestNamespace\TestCaseClass::testMethod with data set "my named data" `
When a test has a data provider that uses named sets, each iteration of the data gets the current name appended to the end of the default test name.
The code snippet below illustrates an example of named data sets.
<?php
use PHPUnit\Framework\TestCase;
namespace TestNamespace;
class TestCaseClass extends TestCase
{
/**
* @dataProvider provider
*/
public function testMethod($data)
{
$this->assertTrue($data);
}
public function provider()
{
return [
'my named data' => [true],
'my data' => [true]
];
}
}
`/path/to/my/test.php`
The test name for a PHPUnit test is the filesystem path. The example below illustrates some valid filter patterns.
--filter 'TestNamespace\\TestCaseClass::testMethod'
--filter 'TestNamespace\\TestCaseClass'
--filter TestNamespace
--filter TestCaseClase
--filter testMethod
--filter '/::testMethod .*"my named data"/'
--filter '/::testMethod .*#5$/'
--filter '/::testMethod .*#(5|6|7)$/'
Shortcuts, could also be used to check for available for matching data providers, examples are shown below on some filter shortcuts.
--filter 'testMethod#2'
--filter 'testMethod#2-4'
--filter '#2'
--filter '#2-4'
--filter 'testMethod@my named data'
--filter 'testMethod@my.*data'
--filter '@my named data'
--filter '@my.*data'
Previous:
Writing Tests for Exceptions and Errors in PHPUnit.
Next:
Advanced PHPUnit Command-Line Options.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/php/PHPUnit/the-command-line-test-runner-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics