Phpunit: No tests found in class "TestCase".

Created on 29 Oct 2019  路  2Comments  路  Source: sebastianbergmann/phpunit

If create test which extends the PHPUnit Test Case, it does not find tests in the test case unless you run all the tests in the directory.

class TestCase extends \PHPUnit\Framework\TestCase
{
}


class FooTest extends TestCase
{
    public function testFoo()
    {
        $foo = new Foo();
        $this->assertEquals('foo', $foo->bar());
    }
}

For example

macos ~/code/punit > vendor/bin/phpunit tests/FooTest.php
PHPUnit 8.4.2 by Sebastian Bergmann and contributors.

W 1 / 1 (100%)

Time: 29 ms, Memory: 4.00 MB

There was 1 warning:

1) Warning
No tests found in class "TestCase".

WARNINGS!
Tests: 1, Assertions: 0, Warnings: 1.

However if you run both the test files in the directory (when it loops through them), everything works fine.

macos ~/code/punit > vendor/bin/phpunit tests/
PHPUnit 8.4.2 by Sebastian Bergmann and contributors.

.. 2 / 2 (100%)

Time: 67 ms, Memory: 4.00 MB

OK (2 tests, 2 assertions)

Attached are tests

Edited - My PHP Version
PHP 7.3.9 (cli) (built: Sep 14 2019 18:07:55) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.9, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.9, Copyright (c) 1999-2018, by Zend Technologies

Archive.zip

typbug

Most helpful comment

The problem is this block in StandardTestSuiteLoader::load:
src/Runner/StandardTestSuiteLoader.php:45

The logic expects the parent class to be abstract. Yours is not, and another condition is satisfied (line 67), and PHPUnit mistakenly loads the parent class of your test case.

If you change

class TestCase extends \PHPUnit\Framework\TestCase

to

abstract class TestCase extends \PHPUnit\Framework\TestCase

then everything works as expected. Although I am not sure that this behavior is desired, and docs do not warn that base classes must always be abstract.

All 2 comments

The problem is this block in StandardTestSuiteLoader::load:
src/Runner/StandardTestSuiteLoader.php:45

The logic expects the parent class to be abstract. Yours is not, and another condition is satisfied (line 67), and PHPUnit mistakenly loads the parent class of your test case.

If you change

class TestCase extends \PHPUnit\Framework\TestCase

to

abstract class TestCase extends \PHPUnit\Framework\TestCase

then everything works as expected. Although I am not sure that this behavior is desired, and docs do not warn that base classes must always be abstract.

Thank you. I have made the adjustment to my original class, and it solved the problem. Maybe line 56 should warn user that class is not abstract?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

klesun picture klesun  路  4Comments

ezzatron picture ezzatron  路  3Comments

stemis picture stemis  路  3Comments

rentalhost picture rentalhost  路  4Comments

sebastianbergmann picture sebastianbergmann  路  4Comments