1
votes

In my symfony 4 application I try to run the first unit test from the symfony doc. But it returns the follwing message.

    // tests/Util/CalculatorTest.php
    namespace App\Tests\Util;

    use App\Util\Calculator;
    use PHPUnit\Framework\TestCase;

    public class CalculatorTest extends TestCase
    {
        public function testAdd()
        {
            $calculator = new Calculator();
            $result = $calculator->add(30, 12);

            // assert that your calculator added the numbers correctly!
            $this->assertEquals(4, $result);
        }
    }

PHPUnit 5.7.27 by Sebastian Bergmann and contributors.

Time: 17 ms, Memory: 2.00MB

No tests executed!

And when I try to run that test directly using following command it also gives following error.

$ ./bin/phpunit tests/Util/CalculatorTest.php
    #!/usr/bin/env php
    // tests/Util/CalculatorTest.php
    namespace App\Tests\Util;

    use App\Util\Calculator;
    use PHPUnit\Framework\TestCase;

    public class CalculatorTest extends TestCase
    {
        public function testAdd()
        {
            $calculator = new Calculator();
            $result = $calculator->add(30, 12);

            // assert that your calculator added the numbers correctly!
            $this->assertEquals(4, $result);
        }
    }

PHP Fatal error: Uncaught PHPUnit\Runner\Exception: Class 'tests/Util/CalculatorTest' could not be found in '/home/username/server/tests/Util/CalculatorTest.php'. in /home/username/server/bin/.phpunit/phpunit-6.5/src/Runner/StandardTestSuiteLoader.php:102

Any kind of help would be appreciated.

1
check if you are running correct php unit. Not sure about symfony 4, but in symfony 3, i had to use the phpunit from vendor dir. It was getting mixed up with the globally installed phpunit that failed to detect symfony's unit test configurations.Simas Joneliunas
@SimasJoneliunas I also tried that too. But still same result. I also edit the question to display error when I try to directly run that test.Gayan Charith
if the namespace is App\Tests\Util and the classname is CalculatorTest, why does it try to find the class: tests/Util/CalculatorTest? You have an autoloading problem.Dragos
it was a silly mistake. i forgot to add the <?php :)Gayan Charith

1 Answers

0
votes

I had the same problem and the solution for me was add a phpunit.xml config file in the root directory of my project.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

Regards