I am working on creating a PHP library and want to start writing tests. I am getting an error Fatal error: Class 'PHPUnit\Framework\TestCase' not found
.
My project structure is: in my main directory I have composer.json, a src/ directory with all my classes, a tests/ directory with unit/ and acceptance/ subdirectories. The tests I am trying to run are in the the unit/ directory. I am using the command line interface to run the test so the error happens when running phpunit tests/unit/testMyClass.php
testMyClass.php looks like:
<?php
require 'vendor/autoload.php';
use PHPUnit\Framework\TestCase;
class MyClassTest extends TestCase {
public function testCreateMyClass() {
// Tests are written here
}
}
?>
My composer.json is:
{
"require-dev": {
"phpunit/phpunit": "4.8.*"
}
"autoload": {
"classmap": [
"src/"
}
}
}
I had the same problem and I solved it by extending my test class from the PHPUnit_Framework_TestCase class instead of using the namespace PHPUnit\Framework\TestCase. After rebuilding your project-structure it worked fine for me.
<?php
require './vendor/autoload.php';
class MyClassTest extends PHPUnit_Framework_TestCase {
public function testCreateMyClass() {
// Tests are written here
}
}
?>
{
"name": "root/project",
"authors": [
{
"name": "watzerm",
"email": "[email protected]"
}
],
"require": {
"phpunit/phpunit": "5.4.*"
},
"autoload": {
"classmap": [
"src/"
]
}
}
$./vendor/bin/phpunit tests/unit/testMyClass.php
PHPUnit 4.8.27 by Sebastian Bergmann and contributors.
.
Time: 252 ms, Memory: 2.25MB
OK (1 test, 0 assertions)
Please let me know if this worked out for you too!
I solved the problem with newer version of PHPUnit:
wget https://phar.phpunit.de/phpunit-6.0.phar
php phpunit-6.0.phar -c app/
Output:
PHPUnit 6.0.10 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 486 ms, Memory: 14.00MB
OK (2 tests, 3 assertions)
This is working on Symfony 2.8 LTS and PHPunit 6.0 github repo - https://github.com/nikola-bodrozic/PHPUnit-Symfony28
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With