pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.07k stars 315 forks source link

[Bug]: It's not possible to put tests next to what they’re testing and utilize "uses()" #1092

Closed clementbirkle closed 4 months ago

clementbirkle commented 4 months ago

What Happened

I want to to put tests next to what they're testing like Angular, NestJS. You can read this article: https://morrislaptop.medium.com/put-tests-next-to-what-theyre-testing-in-laravel-acd01d357d81

But my own test case is never apply because my test file is outside "tests" directory. I want to keep "tests" directory but also have tests located in "app" directory.

In my case, I got error: Target [App\Banishment\BanishmentRepositoryContract] is not instantiable.

The same test in "tests/Feature" works.

How to Reproduce

In ./phpunit.xml add "App" test suite:

    <testsuites>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
        <testsuite name="App">
            <directory suffix="Test.php">./app</directory>
        </testsuite>
    </testsuites>

In ./tests.Pest.php add "App" for test case:

$testCaseIn = ['Feature', 'App'];

uses(TestCase::class)->in(...$testCaseIn);

Create a test in "app" directory who require TestCase::class.

Sample Repository

No response

Pest Version

2.32.2

PHP Version

8.1.20

Operation System

Linux

Notes

No response

clementbirkle commented 4 months ago

I found the solution...

The Pest.php file should be like that:

$testCaseIn = ['Feature', '../app'];

uses(TestCase::class)->in(...$testCaseIn);
clementbirkle commented 4 months ago

I wasn't an issue.