The modern, simple and intuitive PHP unit testing framework.
The modern, simple and intuitive PHP unit testing framework.
| PHP version | atoum version |
|---|---|
| 5.3 -> 5.6 | 1.x -> 3.x |
| 7.2 -> 8.1 | 4.X -> 4.1 |
| 8.x | 4.1 < 4.X (current) |
atoum now has complete support for modern PHP features, including:
true/false/null types#[\Override] Attribute, Typed Class Constants#[\Deprecated] AttributeAll these features are fully supported in the mock generator, ensuring your modern PHP code can be properly tested. See PHP_MODERN_SUPPORT.md for detailed documentation.
Just like SimpleTest or PHPUnit, atoum is a unit testing framework specific to the PHP language. However, it has been designed from the start with the following ideas in mind:
To accomplish that, it massively uses capabilities provided by PHP, to give the developer a whole new way of writing unit tests. Therefore, it can be installed and integrated into an existing project extremely easily, since it is only a single PHAR archive, which is the one and only entry point for the developer. Also, thanks to its fluent interface, it allows for writing unit tests in a fashion close to natural language. It also makes it easier to implement stubbing within tests, thanks to intelligent uses of anonymous functions and closures. atoum natively, and by default, performs the execution of each unit test within a separate PHP process, to warrant isolation. Of course, it can be used seamlessly for continuous integration, and given its design, it can be made to cope with specific needs extremely easily. atoum also accomplishes all of this without affecting performance, since it has been developed to boast a reduced memory footprint while allowing for hastened test execution. It can also generate unit test execution reports in the Xunit format, which makes it compatible with continuous integration tools such as Jenkins. atoum also generates code coverage reports, in order to make it possible to supervise unit tests. Finally, even though it is developed mainly on UNIX, it can also work on Windows.
<?php
$this
->integer(150)
->isGreaterThan(100)
->isLowerThanOrEqualTo(200)
;<?php
$this
->given($testedInstance = new testedClass())
->and($testedClass[] = $firstValue = uniqid())
->then
->sizeof($testedInstance)->isEqualTo(1)
->string($testedClass[0])->isEqualTo($firstValue)
;<?php
$this
->given($testedInstance = new testedClass())
->and($aMock = new \mock\foo\bar()) // here a mock of the class \foo\bar is created dynamically
->and($this->calling($aMock)->doOtherThing = true) // each call to doOtherThing() by the instance will return true
->and($testedInstance->setDependency($aMock))
->then
->boolean($testedInstance->doSomething())->isTrue()
->mock($aMock)
->call('doOtherThing')->withArguments($testedInstance)->once() // Asserts that the doOtherThing() method of $aMock was called once
;<?php
$this
->given($testedInstance = new testedClass())
->and($aMock = new \mock\foo\bar()) // here a mock of the class \foo\bar is created dynamically
->and($this->calling($aMock)->doOtherThing->throw = $exception = new \exception()) // Call to doOtherThing() will throw an exception
->and($testedInstance->setDependency($aMock))
->then
->exception(function() use ($testedInstance) { $testedInstance->doSomething(); })
->isIdenticalTo($exception)
;<?php
$this
->given($this->function->session_start = false)
->and($session = new testedClass())
->then
->exception(function () use ($session) { $session->start(); })
->isInstanceOf('project\namespace\exception')
->hasMessage('Unable to start session')
->function('session_start')->wasCalled()->once()
;php path/to/test/file.php,<?php
$script->addDefaultArguments('--test-it', '-ncc');
$runner->addTestsFromDirectory(__DIR__ . '/tests/units/classes');
$testGenerator = new atoum\atoum\test\generator();
$testGenerator
->setTestClassesDirectory(__DIR__ . '/tests/units/classes');
->setTestClassNamespace('atoum\atoum\tests\\units');
->setTestedClassesDirectory(__DIR__ . '/classes');
->setTestedClassNamespace('atoum\atoum')
->setRunnerPath(__DIR__ . '/scripts/runner.php')
;
$runner->setTestGenerator($testGenerator);atoum absolutely requires PHP >= 8.0.0 or later to work.
On UNIX, in order to check whether you have the right PHP version, you just need to run the following command in your terminal:
$ php -vIf PHP 8.0.x or higher gets displayed, then you have the right PHP version installed.
Should you want to use atoum using its PHAR archive, you also need PHP to be able to access the phar module, which is normally available by default.
On UNIX, in order to check whether you have this module or not, you just need to run the following command in your terminal:
$ php -m | grep -i pharIf Phar or equivalent gets displayed, then the module is properly installed.
Generating reports in the Xunit format requires the xml module.
On UNIX, in order to check whether you have this module or not, you just need to run the following command in your terminal:
$ php -m | grep -i xmlIf Xml or equivalent gets displayed, then the module is properly installed.
Should you wish to monitor the coverage rate of your code by the unit tests, the Xdebug 2.3 module will be required.
On UNIX, in order to check whether you have this module or not, you just need to run the following command in your terminal:
$ php -v | grep -oi 'xdebug'If Xdebug or equivalent gets displayed, then the module is properly installed.
You just have to download its PHAR archive and store it where you wish, for example under /path/to/project/tests/atoum.phar.
This PHAR archive contains the latest development version to pass the totality of atoum's unit tests.
atoum's source code is also available via the GitHub repository.
To check if atoum works correctly with your configuration, you can execute all its unit tests.
To do that, you just need to run the following command in your terminal:
$ php atoum.phar --test-itUsing your preferred text editor, create the file path/to/project/tests/units/helloWorld.php and add the following code:
<?php
namespace vendor\project\tests\\units;
require_once 'path/to/atoum.phar';
include_once 'path/to/project/classes/helloWorld.php';
use atoum\atoum;
use vendor\project;
class helloWorld extends atoum\test
{
public function testSay()
{
$helloWorld = new project\helloWorld();
$this->string($helloWorld->say())->isEqualTo('Hello World!');
}
}Launch your terminal and run the following command:
$ php path/to/test/file[enter]You should get the following result or something equivalent:
> atoum version XXX by Frédéric Hardy.
Error: Unattended exception: Tested class 'vendor\project\helloWorld' does not exist for test class 'vendor\project\tests\\units\helloWorld'Using again your preferred text editor, create the file path/to/project/classes/helloWorld.php and add the following code:
<?php
namespace vendor\project;
class helloWorld
{
public function say()
{
return 'Hello World!';
}
}In the same terminal, run the following command once again:
$ php path/to/test/file[enter]You should get the following result, or something equivalent:
> atoum version 288 by Frédéric Hardy.
> Run vendor\project\tests\\units\helloWorld...
[S___________________________________________________________][1/1]
=> Test duration: 0.00 second.
=> Memory usage: 0.25 Mb.
> Total test duration: 0.00 second.
> Total test memory usage: 0.25 Mb.
> Code coverage value: 100.00%
> Running duration: 0.08 second.
> Success (1 test, 1 method, 2 assertions, 0 error, 0 exception)!<?php
namespace vendor\project\tests\\units;
require_once 'path/to/atoum.phar';
include_once 'path/to/project/classes/helloWorld.php';
use atoum\atoum;
use vendor\project;
class helloWorld extends atoum\test
{
public function test__construct()
{
$helloWorld = new project\helloWorld();
$this
->string($helloWorld->say())->isEqualTo('Hello!')
->string($helloWorld->say($name = 'Frédéric Hardy'))->isEquaNo open issues yet, or sync has not completed.