1

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.

4
  • 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. Commented Jul 27, 2018 at 9:13
  • @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. Commented Jul 28, 2018 at 2:33
  • 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. Commented Jul 28, 2018 at 10:22
  • it was a silly mistake. i forgot to add the <?php :) Commented Jul 29, 2018 at 3:23

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.