Skip to content
...

Inline Tests

Inline tests let you write tests directly on the method being tested using the #[TestInline]#[TestInline(array $arguments, mixed $result = null)]Declares an inline test on a method or function. attribute. No separate test function needed.

Plugin class: InlineTestPlugin\Testo\Inline\InlineTestPlugin. Included in SuitePlugins\Testo\Application\Config\Plugin\SuitePlugins — enabled by default.

Configuration

It's recommended to create a separate Test Suite for inline tests, pointing to the src folder: you'll most likely embed tests in your application code, not in tests/.

#[TestInline]

Declares an inline test on a method or function.

#[TestInline(array $arguments, mixed $result = null)]

Can be used multiple times — each attribute creates a separate test.

Inline tests work well for:

  • Simple pure functions where a dedicated test file would be excessive.
  • Private helper methods that you want to test without changing visibility.
  • Prototyping when you need immediate validation without switching context.

For larger test suites (10+ cases) or tests that need explanation, consider writing separate tests using providers from the Data plugin.

Parameters:

$arguments
Array of values passed to the method. Named arguments are supported.
$result
Expected return value. Can accept a Closure(mixed $result)\Closure(mixed $result), in which case an arbitrary check inside the closure will be performed instead of comparing against a specific value.

Examples:

Works with public and private methods, as well as functions:

php
#[TestInline([1, 1], 2)]
#[TestInline([40, 2], 42)]
#[TestInline([-5, 5], 0)]
private static function sum(int $a, int $b): int
{
    return $a + $b;
}
php
#[TestInline([1, 1], 2)]
#[TestInline([40, 2], 42)]
#[TestInline([-5, 5], 0)]
function sum(int $a, int $b): int
{
    return $a + $b;
}

Use named arguments for better readability:

php
#[TestInline(['price' => 100.0, 'discount' => 0.1, 'tax' => 0.2], 108.0)]
#[TestInline(['price' => 50.0, 'discount' => 0.0, 'tax' => 0.1], 55.0)]
private static function calculateFinalPrice(
    float $price,
    float $discount,
    float $tax
): float {
    return $price * (1 - $discount) * (1 + $tax);
}

Custom Assertions

Available in PHP 8.5+ (closures in attributes)

For complex checks, pass a closure as the second parameter, where you can perform any assertions on the result, which will be passed as an argument:

php
#[TestInline(
    arguments: ['john.doe@example.com'],
    result: function (User $user) {
        Assert::same('john.doe@example.com', $user->email);
        Assert::true($user->isActive);
        Assert::notNull($user->createdAt);
    }
)]
public function createUser(string $email): User
{
    // ...
}

In PHP 8.6 this becomes even more elegant with partial application:

php
#[TestInline([10, 3], Assert::greaterThan(3, ?))]
public function divide(int $a, int $b): float
{
    return $a / $b;
}