thomasnordahldk / tester
An object oriented approach to testing for PHP.
Requires
- php: >=7.1
- phpunit/php-code-coverage: ^5.3
This package is not auto-updated.
Last update: 2024-12-19 08:31:36 UTC
README
An object oriented approach to testing PHP code.
Tester aims to be: easy to learn, light weight, object oriented, and extensible.
Installation
The library is released as a composer package.
composer require --dev thomasnordahldk/tester
Tests
Tests are defined by creating TestCase
classes.
class MyTestCase implements TestCase { public function getDescription(): string { return "My new unit test"; } public function run(Tester $tester): void { $tester->assert(true, "This assertion passes!"); $tester->assert(false, "This assertion fails!"); } }
The test case is defined as a description of the test and a run test method.
Docs: Creating a test case.
Test suites
Tests suites are defined by the TestSuite
class which is created with a
description and an array of TestCase
classes.
$unit_tests = new TestSuite("Unit tests", [new UserUnitTest, AddressUnitTest]);
Docs: Test Suites.
Running tests
The library comes with a native test runner that is run from the command line interface, and outputs a summary of the test.
Which tests to run is defined in the file test.php
in the root composer
directory. The file is expected to return an array of test suites.
# test.php $unit_tests = new TestSuite("Unit tests", [new UserUnitTest, new AddressUnitTest]); return [$unit_tests];
$composer-root/~ bin/tester
---------------------------------------------------------------------------
- Unit tests - cases: 2
--- Unit test of User ✔
--- Unit test of Address ✔
---------------------------------------------------------------------------
success: 2, failure: 0, assertions: 8, time: 0.04s
---------------------------------------------------------------------------
For a comprehensive description of the options available for the native test runner script:
Docs: How to run tests.
Inspiration
This library is inspired by the testing library mindplay-dk/testies.