PHP UniTester is a unit testing library for PHP that provides a straightforward interface for writing and executing tests. It focuses on a minimalist approach, allowing developers to create assertions and organize tests at the lowest level of PHP, without reliance on complex external libraries.
|
|
1 gün önce | |
|---|---|---|
| bin | 1 gün önce | |
| src | 1 gün önce | |
| tests | 1 gün önce | |
| .gitignore | 1 gün önce | |
| LICENSE | 1 gün önce | |
| README.md | 1 gün önce | |
| composer.json | 1 gün önce |
UniTester is a lightweight, zero-dependency unit testing library for PHP. It is designed for developers who want to write tests at the lowest level of PHP, without the overhead or complexity of massive frameworks.
vendor.composer require depo/unitester
tests/ directory at your project root.Depo\UniTester\TestCase.execute() method to list your tests.Example:
<?php
namespace Tests;
use Depo\UniTester\TestCase;
class MathTest extends TestCase
{
protected function setUp(): void
{
// Executed before all tests in the class
}
protected function tearDown(): void
{
// Executed after all tests in the class
}
protected function execute(): void
{
$this->testAddition();
$this->testPerformance();
}
public function testAddition()
{
$this->assertEquals(4, 2 + 2, "Addition should be correct");
}
public function testPerformance()
{
$this->assertExecutionTimeLessThan(10, function() {
// Critical code
});
}
}
Run tests:
php vendor/bin/unitester tests/
All assertions accept an optional last $message parameter.
assertEquals($expected, $actual): Loose equality (==).assertStrictEquals($expected, $actual): Strict equality (===).assertNotEquals($expected, $actual)assertNotStrictEquals($expected, $actual)assertTrue($condition)assertFalse($condition)assertNull($value)assertNotNull($value)assertEmpty($value)assertNotEmpty($value)assertInstanceOf($expectedClass, $object)assertCount($expectedCount, $haystack): Checks size of array or Countable.assertArrayHasKey($key, $array): Checks for key existence.assertStringContains($haystack, $needle)assertStringStartsWith($haystack, $needle)assertStringEndsWith($haystack, $needle)assertStringLength($string, $length)assertExecutionTimeLessThan(float $ms, callable $callback): Verifies callback runs under X ms.assertMemoryUsageLessThan(int $bytes, callable $callback): Verifies callback consumes less than X bytes.assertFileExists($path)assertFileNotExists($path)assertDirectoryExists($path)assertIsReadable($path)assertIsWritable($path)$this->log(string $message): Prints a message to the console during tests.$this->dump($value): Prints formatted variable content.fail(string $message): Manually fails the test.UniTester est une librairie de tests unitaires pour PHP, légère et sans aucune dépendance. Elle est conçue pour les développeurs qui souhaitent écrire des tests au plus près du langage, sans la complexité des gros frameworks.
vendor obèse.composer require depo/unitester
tests/ à la racine de votre projet.Depo\UniTester\TestCase.execute() pour lister vos tests.Exemple :
<?php
namespace Tests;
use Depo\UniTester\TestCase;
class MathTest extends TestCase
{
protected function setUp(): void
{
// Exécuté avant l'ensemble des tests de la classe
}
protected function tearDown(): void
{
// Exécuté après l'ensemble des tests de la classe
}
protected function execute(): void
{
$this->testAddition();
$this->testPerformance();
}
public function testAddition()
{
$this->assertEquals(4, 2 + 2, "L'addition doit être correcte");
}
public function testPerformance()
{
$this->assertExecutionTimeLessThan(10, function() {
// Code critique
});
}
}
Lancer les tests :
php vendor/bin/unitester tests/
Toutes les assertions acceptent un dernier paramètre $message optionnel.
assertEquals($expected, $actual) : Égalité souple (==).assertStrictEquals($expected, $actual) : Égalité stricte (===).assertNotEquals($expected, $actual)assertNotStrictEquals($expected, $actual)assertTrue($condition)assertFalse($condition)assertNull($value)assertNotNull($value)assertEmpty($value)assertNotEmpty($value)assertInstanceOf($expectedClass, $object)assertCount($expectedCount, $haystack) : Vérifie la taille d'un tableau ou Countable.assertArrayHasKey($key, $array) : Vérifie la présence d'une clé.assertStringContains($haystack, $needle)assertStringStartsWith($haystack, $needle)assertStringEndsWith($haystack, $needle)assertStringLength($string, $length)assertExecutionTimeLessThan(float $ms, callable $callback) : Vérifie que le callback s'exécute en moins de X ms.assertMemoryUsageLessThan(int $bytes, callable $callback) : Vérifie que le callback consomme moins de X octets.assertFileExists($path)assertFileNotExists($path)assertDirectoryExists($path)assertIsReadable($path)assertIsWritable($path)$this->log(string $message) : Affiche un message dans la console pendant les tests.$this->dump($value) : Affiche le contenu formaté d'une variable.fail(string $message) : Fait échouer le test manuellement.