How to start
1. Prepare your project
Install the Allure Report command-line tool, if it is not yet installed in your operating system. Note that Allure Report requires Java, see Installation.
Make sure that your project uses:
- PHP version 8.1, 8.2 or 8.3,
- PHPUnit version 10.
allure-framework/allure-phpunitto your project's development dependencies:Text
php composer.phar require allure-framework/allure-phpunit --dev
Enable the Allure extension for PHPUnit via a
phpunit.xml. In the
configparameter, specify a path where you will put the configuration file for Allure PHPUnit. For example:XML
<?xml version="1.0" encoding="UTF-8"?> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"> <extensions> <bootstrap class="Qameta\Allure\PHPUnit\AllureExtension"> <parameter name="config" value="config/allure.config.php"/> </bootstrap> </extensions> </phpunit>
Create an Allure PHPUnit configuration file at the specified location (
config/allure.config.phpin the example above). The file must return an associative array containing configuration options. For example:PHP
<?php return [ 'outputDirectory' => 'build/allure-results', ];
2. Run tests
Run your PHPUnit tests the same way as you would run them usually. For example:
This will save necessary data into
build/allure-results or other directory, according to the
outputDirectory setting. If the directory already exists, the new files will be added to the existing ones, so that a future report will be based on them all.
3. Generate a report
Finally, convert the test results into an HTML report. This can be done by one of two commands:
allure generateprocesses the test results and saves an HTML report into the
allure-reportdirectory. To view the report, use the
Use this command if you need to save the report for future reference or for sharing it with colleagues.
allure servecreates the same report as
allure generatebut puts it into a temporary directory and starts a local web server configured to show this directory's contents. The command then automatically opens the main page of the report in a web browser.
Use this command if you need to view the report for yourself and do not need to save it.
The Allure PHPUnit adapter extends the standard reporting features of PHPUnit by providing additional capabilities for crafting more informative and structured tests. This section highlights key enhancements that can be utilized:
- Metadata Annotation: Enhance test reports with descriptions, links, and other metadata.
- Test Organization: Structure your tests into clear hierarchies for better readability and organization.
- Step Division: Break down tests into smaller test steps for easier understanding and maintenance.
- Parametrized Tests: Clearly describe the parameters for parametrized tests to specify different scenarios.
- Attachments: Automatically capture screenshots and other files during test execution.
- Environment Details: Include comprehensive environment information to accompany the test report.
In most cases, Allure PHPUnit provides two different ways to use a feature: the Attributes API and the Runtime API.
Attributes API: add a PHP attribute to a test method or a whole class to add certain data to the test result. When using this approach, the data is guaranteed to be added regardless of how the test itself runs.
Runtime API: use Allure's functions to add certain data to the test result during its execution. This approach allows for constructing the data dynamically.
Note that it is recommended to call the Allure's functions as close to the beginning of the test as possible. This way, the data will be added even if the test fails early.
Specify description, links and other metadata
As described in Improving navigation in your test report, Allure supports multiple ways to organize tests into hierarchical structures. Allure PHPUnit provides the API to assign the relevant fields to tests either by adding attributes or “dynamically” (same as for the metadata fields).
To specify a test's location in the behavior-based hierarchy:
To specify a test's location in the suite-based hierarchy:
Divide a test into steps
Describe parametrized tests
Attach screenshots and other files
You can attach any sorts of files to your Allure report. For example, a popular way to make a report easier to understand is to attach a screenshot of the user interface at a certain point.
Allure PHPUnit provides various ways to create an attachment, both from existing files or generated dynamically, see reference.
Select tests via a test plan file
Test plan is currently not supported by the Allure PHPUnit adapter.
For the main page of the report, you can collect various information about the environment in which the tests were executed.
For example, it is a good idea to use this to remember the OS version and PHP version. This may help the future reader investigate bugs that are reproducible only in some environments.
To provide environment information, put a file named
environment.properties into the
allure-results directory after running the tests. See the example in Environment file.
Note that this feature should be used for properties that do not change for all tests in the report. If you have properties that can be different for different tests, consider using Parametrized tests.