[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

[Prepare for PHPUnit12] [ INFO ] No tests found ← This [dataProvider]

[Prepare for PHPUnit12] [ INFO ] No tests found ← This [dataProvider]

Introduction

My name is Enoki from the System Development Department.
This time, I will describe how to solve the problem when I use dataProvider and get INFO No tests found when running a test.
It's a long farce, so if you just want to know the answer, please just look at the conclusion

Main story

The beginning of things

*The test content is not even remotely important in this article, so don't worry about it not being part of the test itself. Me: ``Would you like to write a test...? For this test content, dataProvider would be fine!'' Me: ``Hmph.''
<?php namespace Tests\Unit; use PHPUnit\Framework\Attributes\DataProvider; use Tests\TestCase; class DataProviderTest extends TestCase { /** * @return array */ public function provideParam(): array { return[ 'params' => ['data', 'true'], ]; } /** * @dataProvider provideParam */ public function test_Data provider test($data, $expected) $this->assertTrue(true); } }
root:/var/www/html/src# php artisan test tests/Unit/DataProviderTest.php WARN Metadata found in doc-comment for method Tests\Unit\DataProviderTest::test_DataProviderTest(). Metadata in doc- comments is deprecated and will no longer be supported in PHPUnit 12. Update your test code to use attributes instead. INFO No tests found.

Me: ``Huh?'
' Me: ``Huh~ From PHPUnit12, you specify it with attributes instead of Doc! Understood!''
*This is the attribute → #[]

#[DataProvider('provideParam')] public function test_Data provider test($data, $expected) { $this->assertTrue(true); }

Me: ``Hey!''
Me: ``Let's do it.''

root:/var/www/html/src# php artisan test tests/Unit/DataProviderTest.php INFO No tests found.

Me: “Yeah?”

Main topic

I can almost hear people saying, "This is a farce." Anyway, the important thing here is that it says  test not found  What would you do in a situation like this? For now, I think you'll probably do things like php artisan optimize:clear, composer dump-autoload, and review the namespace and file names. (Please do so.) Naturally, I was also like, ``Ah, that's the usual thing.'' However, writing this article naturally meant that I couldn't get over it. What I want you to remember here is that he only appeared as INFO. Normally, when you specify and execute a test that does not exist, the result is as follows.
root:/var/www/html/src# php artisan test tests/Unit/NoneTest.php PHPUnit 11.2.2 by Sebastian Bergmann and contributors. Test file "tests/Unit/NoneTest.php" not found

This means that the test must exist, but it cannot be found. Next, I created a random test to see if this theory was correct.
<?php namespace Tests\Unit; use PHPUnit\Framework\Attributes\DataProvider; use Tests\TestCase; class DataProviderTest extends TestCase { /** * @return array */ public function provideParam(): array { return[ 'params' => ['data', 'true'], ]; } public function test_Appropriate test() { $this->assertTrue(true); } #[DataProvider('provideParam')] public function test_Testing the data provider ($data, $expected) { $this->assertTrue(true); } }
root:/var/www/html/src# php artisan test tests/Unit/DataProviderTest.php FAIL Tests\Unit\DataProviderTest ⨯ Data provider test ✓ Any test 0.06s ──────────── ──────────────────────────────────────────────── ──────────────────────────────────────────────── FAILED Tests\ Unit\DataProviderTest > Data Provider Test The data provider specified for Tests\Unit\DataProviderTest::test_Data Provider Test is invalid Data Provider method Tests\Unit\DataProviderTest::provideParam() is not static Tests: 1 failed, 1 passed (1 assertions) Duration: 0.10s

I got more results than I expected. Not only files but also tests are recognized.
And with accurate errors.
It seems like you want to create the dataProvider method as static.
Become a robot that follows errors.

<?php namespace Tests\Unit; use PHPUnit\Framework\Attributes\DataProvider; use Tests\TestCase; class DataProviderTest extends TestCase { /** * @return array */ public static function provideParam(): array { return[ 'params' => ['data', 'true'], ]; } #[DataProvider('provideParam')] public function test_Data provider test($data, $expected) { $this->assertTrue(true); } }

root:/var/www/html/src# php artisan test tests/Unit/DataProviderTest.php PASS Tests\Unit\DataProviderTest ✓ Data provider test with data set "params" 0.06s Tests: 1 passed (1 assertions) Duration: 0.09s

It passed!
Apparently, if you make the method used as dataProvider static, it will behave as you want.
I have solved the problem for now, but I would like to pursue the mystery of "INFO No tests found."

Pursuit of cause

The hypothesis we can make based on what we know so far is that the method used as the dataProvider must be static.
Here, the results of the first run are helpful.

root:/var/www/html/src# php artisan test tests/Unit/DataProviderTest.php WARN Metadata found in doc-comment for method Tests\Unit\DataProviderTest::test_DataProviderTest(). Metadata in doc- comments is deprecated and will no longer be supported in PHPUnit 12. Update your test code to use attributes instead. INFO No tests found. 

As you can see, using dataProvider in Doc comments is still only "deprecated", so it's a WARN.
This means that even the dataProvider method can be executed successfully if it is static.

<?php namespace Tests\Unit; use PHPUnit\Framework\Attributes\DataProvider; use Tests\TestCase; class DataProviderTest extends TestCase { /** * @return array */ public static function provideParam(): array { return[ 'params' => ['data', 'true'], ]; } /** * @dataProvider provideParam */ public function test_Data provider test($data, $expected) { $this->assertTrue(true); } }

 

root:/var/www/html/src# php artisan test tests/Unit/DataProviderTest.php WARN Metadata found in doc-comment for method Tests\Unit\DataProviderTest::test_DataProviderTest(). Metadata in doc- comments is deprecated and will no longer be supported in PHPUnit 12. Update your test code to use attributes instead. PASS Tests\Unit\DataProviderTest ✓ Data Provider Tests with data set "params" 0.09s Tests: 1 passed (1 assertions) Duration : 0.14s

It was executed successfully as hypothesized.
However, why does it need to be static? From my memory and looking at the source code it shouldn't have needed to be static before.
So I looked into it.
〇Citation: https://docs.phpunit.de/en/10.5/writing-tests-for-phpunit.html#data-providers


A data provider method must be public and static.
  a value that is iterable, either an array or an object that implements the Traversable interface.
Translation
 data provider methods must be public and static. It must return an iterable value (an array or an object implementing the Traversable interface).

That seems to be the case. Keep up with the changes.
There was also the following statement:

Original textWhen
 a test depends on a test that uses data providers, the depending test will be executed when the test it depends upon is successful for at least one data set.
 The result of a test that uses data providers cannot be injected into a depending test .
 translation
 test depends on a test that uses a data provider, the dependent test will run if the dependent test succeeds on at least one dataset. The results of tests that use data providers are not injected into dependent tests.

This seems to be the reason why an error appeared when I put in the appropriate test.
When you say it, it's a legitimate punch that won't make you say, "Yes! You're right!"
If the part before the test fails and there is only a test that assumes that, "No tests found." is quite natural.

conclusion

Make the target method of dataProvider a static method.

Digression

Why does it need to be static?
 The question arises. (I'm wondering why your opinion is that if you're an engineer, you're trembling in fear of being asked to provide the source.) I think it's probably a way to "guarantee the independence of test data from testing." Reproducibility and non-interference with other tests are very important in testing, and we can guarantee that we provide pure data that is independent of instance state. I tried searching various official sources, but please make it static! Information regarding this update has been found here and there. However, I couldn't find any information about why it was forced to be static... (If anyone knows, please let me know)
If you found this article helpful , please give it a like!
10
Loading...
10 votes, average: 1.00 / 110
376
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Enoki

I play everything from FPS, RPG, MMO, crafting, etc.