nette/tester
Nette Tester is a lightweight PHP unit testing framework with simple assertions, clear output, and easy CLI running. It supports writing isolated tests, reporting failures nicely, and integrates well into CI pipelines for fast, reliable test suites.
Installation:
composer require nette/tester --dev
Add to composer.json under require-dev:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
Basic Test File:
Create tests/MyTest.php:
<?php
use Tester\TestCase;
class MyTest extends TestCase
{
public function testBasicAssertion()
{
$this->assert(1 + 1 === 2);
}
}
Run Tests:
vendor/bin/tester tests/
public function testAssertions()
{
$this->assert($value === 'expected');
$this->assertNull($nullValue);
$this->assertException(function () {
throw new RuntimeException('Expected');
});
}
Data-Driven Testing:
#[DataProvider('dataProvider')]
public function testWithData($input, $expected)
{
$this->assert($this->transform($input) === $expected);
}
public function dataProvider()
{
return [
['input1', 'output1'],
['input2', 'output2'],
];
}
Mocking & Isolation:
public function testWithMock()
{
$mock = $this->getMockBuilder(Service::class)
->disableOriginalConstructor()
->getMock();
$mock->method('doWork')->willReturn('mocked');
$this->assert($mock->doWork() === 'mocked');
}
HTTP Testing (via HttpAssert):
public function testHttpResponse()
{
$response = $this->get('/api/users');
$this->assert($response->code === 200);
$this->assert($response->json['id'] === 1);
}
vendor/bin/tester --parallel tests/
vendor/bin/tester --coverage tests/
vendor/bin/tester --watch tests/
Service Container Testing:
public function testServiceBinding()
{
$this->assertTrue(app()->bound('MyService'));
$this->assertInstanceOf(MyService::class, app('MyService'));
}
Route Testing:
public function testRoute()
{
$response = $this->get('/dashboard');
$this->assert($response->code === 302);
$this->assert($response->headers['location'] === '/login');
}
Database Transactions:
public function testDatabaseOperation()
{
$this->beginTransaction();
User::create(['name' => 'Test']);
$this->assert(User::count() === 1);
$this->rollBack();
}
System php.ini:
php.ini by default. Use --config or -C to override:
vendor/bin/tester --config=php.ini tests/
Parallel Execution:
proc_open()).Assertion Errors:
getResult() throws LogicException if called before test completion. Use Test::getResult() only after the test finishes.Data Providers:
public function dataProvider()
{
return [
['valid', 'data'],
// Avoid: []
];
}
IDE Hints:
@var or PHPDoc for complex types (e.g., #[Assert\Type('array<int, string>')]).Dumper:
dump() for debugging:
dump($complexObject, 'object_name');
Error Output:
$this->assertNoError(function () {
// Code that may trigger errors
}, 'Custom error message');
Slow Tests:
$this->benchmark(function () {
// Slow operation
}, 'Operation took {time}ms');
Custom Assertions:
use Tester\Assert;
Assert::add('isEven', function ($value) {
return $value % 2 === 0;
});
$this->assertIsEven(4);
Test Lifecycle Hooks:
public static function before()
{
// Runs before all tests
}
public static function after()
{
// Runs after all tests
}
Environment Setup:
public static function setup()
{
putenv('APP_ENV=testing');
}
Custom Templates:
coverage.html in tests/_templates/ for custom reports.Artisan Commands:
Artisan::call() in tests:
$this->assertArtisanSuccess('migrate:fresh');
Queue Testing:
public function testQueueJob()
{
$job = new ProcessPodcast();
$this->assertQueueJob($job);
}
Session/Authentication:
public function testAuthenticatedRoute()
{
$this->actingAs(User::first());
$this->get('/profile')->assertOk();
}
How can I help you explore Laravel packages today?