How do I run all my PHPUnit tests? - php

I have script called Script.php and tests for it in Tests/Script.php, but when I run phpunit Tests it does not execute any tests in my test file. How do I run all my tests with phpunit?
PHPUnit 3.3.17, PHP 5.2.6-3ubuntu4.2, latest Ubuntu
Output:
$ phpunit Tests
PHPUnit 3.3.17 by Sebastian Bergmann.
Time: 0 seconds
OK (0 tests, 0 assertions)
And here are my script and test files:
Script.php
<?php
function returnsTrue() {
return TRUE;
}
?>
Tests/Script.php
<?php
require_once 'PHPUnit/Framework.php';
require_once 'Script.php'
class TestingOne extends PHPUnit_Framework_TestCase
{
public function testTrue()
{
$this->assertEquals(TRUE, returnsTrue());
}
public function testFalse()
{
$this->assertEquals(FALSE, returnsTrue());
}
}
class TestingTwo extends PHPUnit_Framework_TestCase
{
public function testTrue()
{
$this->assertEquals(TRUE, returnsTrue());
}
public function testFalse()
{
$this->assertEquals(FALSE, returnsTrue());
}
}
?>

Php test's filename must end with Test.php
phpunit mydir will run all scripts named xxxxTest.php in directory mydir
(looks likes it's not described in the phpunit documentation)

I created following phpunit.xml and now atleast I can do phpunit --configuration phpunit.xml in my root directory to run the tests located in Tests/
<phpunit backupGlobals="false"
backupStaticAttributes="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Tests">
<directory suffix=".php">Tests</directory>
</testsuite>
</testsuites>
</phpunit>

I think forPHPUnit to decide to automatically run it it must follow a filename convention: somethingTest.php.

You think they would have documented this. I just looked through the manual, and they say you can pass a directory, but not really how to do it.
Perhaps your class name has to match the basename (everything but the ".php") of your test scripts filename?

<?php
//Files required for phpunit test
require_once 'PHPUnit/Framework.php';
//Knowing the drupal environment
require_once './includes/bootstrap.inc'; //initialize the Drupal framework
//Loading the drupal bootstrap
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
//Helper file
include_once 'helper.inc';
//Including inc file of addresses module
include_once(module_load_include('inc','addresses_user','addresses_user'));
class addresses_test extends PHPUnit_Framework_TestCase {
protected $uid;
protected function setUp()
{
$this->uid = 1;
}

Related

PHPUnit can't find a class loaded in the bootstrap

My problem with PHPUnit is quite simple: if the test class file is included in the bootstrap file, PHPUnit won't be able to find the test class.
You can easily recreate the issue:
The file hierarchy:
./tests/MyTest.php
./bootstrap.php
./phpunit.xml
phpunit.xml
<phpunit bootstrap="./bootstrap.php">
<testsuites>
<testsuite name="MyTest">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
bootstrap.php:
<?php
require __DIR__ .'/vendor/autoload.php';
require __DIR__ .'/tests/MyTest.php';
tests/MyTest.php
<?php
namespace App\Tests;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
public function testTrue()
{
$this->assertTrue(true);
}
}
In this case, when I run ./vendor/bin/phpunit, it returns "No tests executed!". And if I run ./vendor/bin/phpunit ./tests/MyTest.php, it returns "Class 'MyTest' could not be found in '[...]\tests\MyTest.php'.".
Now, if I remove the second require_once from autoload.php, both commands work as expected.
I've only seen a couple of similar issues on stack. They are six years old and have not been resolved, which makes me think that not allowing this way of loading classes is actually a design choice.
Could you please help me? Thank you!

Namespacing "Class not found" when autoloading via composer [duplicate]

I'm trying to use PHPUnit in a PHP project.
Here is my project structure (files are in italic font style)
controllers
Pages.php
tests
pagesTest.php
vendor
bin
phpunit.bat
composer.json
My files:
composer.json
{
"require-dev": {
"phpunit/phpunit":"5.5.4"
}
}
Pages.php
<?php
namespace controllers
class Pages
{
public function render()
{
return 'Hello World';
}
}
pagesTest.php
<?php
class PagesTest extends PHPUnit_Framework_TestCase
{
public function testRenderReturnsHelloWorld()
{
$pages = new \controllers\Pages();
$expected = 'Hello Word';
$this->assertEquals($expected, $pages->render());
}
}
When I open the command line I write:
C:\xampp\htdocs\PHPUnitTestProject\vendor\bin>phpunit ../../tests/PagesTest.php
I receive this error message: PHP Fatal error: Class 'controllers\Pages' not found in C:\xampp\htdocs\PHPUnitTestProject\tests\pagesTest.php on line 7
It's a path problem. I think it's because it searches for C:\xampp\htdocs\PHPUnitTestProject\vendor\bin\controllers\Pages() which doesn't exists.
It should be C:\xampp\htdocs\PHPUnitTestProject\controllers\Pages()
You need to point to the tested class, so in pagesTest.php add a require:
require __DIR__ . "/../controllers/Pages.php";
Or if you are using autoloading, then you can bootstrap the autoload in your command line
phpunit --bootstrap src/autoload.php
Or you can set up a phpunit.xml configuration file like this example (from the PHPUnit page I linked to above):
<phpunit bootstrap="src/autoload.php">
<testsuites>
<testsuite name="money">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
Which you then use with the --configuration option.
Adding bootstrap="vendor/autoload.php" in phpunit.xml.dist solved the issue for me.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"> <!-- in here -->
<php>
<!-- ... -->
</php>
<testsuites>
<!-- ... -->
</testsuites>
</phpunit>
Try composer dump-autoload -o command
Call phpunit from the root folder:
$ cd C:\xampp\htdocs\PHPUnitTestProject\
$ vendor\bin\phpunit tests/PagesTest.php
I was getting the same error because I hadn't named my Class the same as the filename that phpunit was calling.
e.g. I was calling:
phpunit TEST_myweb_controller.php
which had a class definition of: class web_controller_test extends PHPUnit\Framework\TestCase
This returned error: Class 'TEST_myweb_controller.php' could not be found in '\my\path\to\tests\TEST_myweb_controller.php'
To fix this I changed the class deinition to: class TEST_myweb_controller extends PHPUnit\Framework\TestCase

PHPUnit won't find my test files or execute them directly

My PHPUnit is installed via Composer (PHPUnit 3.7.21).
I have the following directory structure:
.
├── Code
├── Test
│   ├── Php
│   │   └── PlanningModuleTest.php
│   └── bootstrap.php
└── phpunit.xml
When I execute
$ phpunit
from the project root, I get the following output:
PHPUnit 3.7.21 by Sebastian Bergmann.
Configuration read from D:\Development\...\phpunit.xml
Time: 40 ms, Memory: 4.00MB
No tests executed!
My phpunit.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
bootstrap="Test/bootstrap.php"
>
<testsuites>
<testsuite name="pms">
<directory suffix="Test.php">./Test/Php/</directory>
</testsuite>
</testsuites>
<php>
<ini name="display_errors" value="true"/>
</php>
</phpunit>
I have a single file PlanningModuleTest.php with the following content:
For the record the first part of my PHP file:
<?php
use jamesiarmes\PhpEws\Enumeration\UnindexedFieldURIType;
use PHPUnit\Framework\TestCase;
class PlanningModuleTest extends TestCase
{
public function setUp()
{
$_SESSION = array();
require_once('Code/Config.php');
parent::setUp();
}
public function testExchangeCalendarItemCreation()
{
$this->assertInstanceOf(ExchangeCalendarItem::class, new ExchangeCalendarItem());
}
public function testExchangeCalendarItem()
{
// ...
}
}
So that should be correct, since PHPUnit checks if files and classnames end with Test.php.
Why doesn't phpunit execute my tests?
Edit
I tried executing my test directly with
$ phpunit --verbose --debug Test\Php\PlanningModuleTest.php
and it returns this:
Class 'Test\Php\PlanningModuleTest' could not be found in 'D:\Development\Git\projectmanagement\Test\Php\PlanningModuleTest.php'.`
As already suggested in the comments, before we start debugging issues related to using a globally installed version of phpunit that is apparently different from the one you have installed with the project, try running
$ ./vendor/bin/phpunit
instead.
First thing that catches the eye is that the test class is missing a namespace while it appears that it should have one:
<?php
namespace Test\Php;
use jamesiarmes\PhpEws\Enumeration\UnindexedFieldURIType;
use PHPUnit\Framework\TestCase;
class PlanningModuleTest extends TestCase
{
// ...
}
Then also make sure to configure as well as document the namespace requirements for your test code in composer.json:
and that your autoloading is properly set up in composer.json, for example
{
"autoload-dev": {
"psr-4": {
"Test\\": "Test/"
}
}
}
Since you haven't shared it with us, make sure Test/bootstrap.php contains
<?php
require_once __DIR__ . '/../vendor/autoload.php';
to set up the autoloading properly.
You have to use phpunit command from the project directory just like this:
project> phpunit
and additionally your testsuite has to be like this:
<testsuite name="pms">
<directory>Test/Php</directory>
</testsuite>

PHP Fatal error: Class not found - PHPUnit

I'm trying to use PHPUnit in a PHP project.
Here is my project structure (files are in italic font style)
controllers
Pages.php
tests
pagesTest.php
vendor
bin
phpunit.bat
composer.json
My files:
composer.json
{
"require-dev": {
"phpunit/phpunit":"5.5.4"
}
}
Pages.php
<?php
namespace controllers
class Pages
{
public function render()
{
return 'Hello World';
}
}
pagesTest.php
<?php
class PagesTest extends PHPUnit_Framework_TestCase
{
public function testRenderReturnsHelloWorld()
{
$pages = new \controllers\Pages();
$expected = 'Hello Word';
$this->assertEquals($expected, $pages->render());
}
}
When I open the command line I write:
C:\xampp\htdocs\PHPUnitTestProject\vendor\bin>phpunit ../../tests/PagesTest.php
I receive this error message: PHP Fatal error: Class 'controllers\Pages' not found in C:\xampp\htdocs\PHPUnitTestProject\tests\pagesTest.php on line 7
It's a path problem. I think it's because it searches for C:\xampp\htdocs\PHPUnitTestProject\vendor\bin\controllers\Pages() which doesn't exists.
It should be C:\xampp\htdocs\PHPUnitTestProject\controllers\Pages()
You need to point to the tested class, so in pagesTest.php add a require:
require __DIR__ . "/../controllers/Pages.php";
Or if you are using autoloading, then you can bootstrap the autoload in your command line
phpunit --bootstrap src/autoload.php
Or you can set up a phpunit.xml configuration file like this example (from the PHPUnit page I linked to above):
<phpunit bootstrap="src/autoload.php">
<testsuites>
<testsuite name="money">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
Which you then use with the --configuration option.
Adding bootstrap="vendor/autoload.php" in phpunit.xml.dist solved the issue for me.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"> <!-- in here -->
<php>
<!-- ... -->
</php>
<testsuites>
<!-- ... -->
</testsuites>
</phpunit>
Try composer dump-autoload -o command
Call phpunit from the root folder:
$ cd C:\xampp\htdocs\PHPUnitTestProject\
$ vendor\bin\phpunit tests/PagesTest.php
I was getting the same error because I hadn't named my Class the same as the filename that phpunit was calling.
e.g. I was calling:
phpunit TEST_myweb_controller.php
which had a class definition of: class web_controller_test extends PHPUnit\Framework\TestCase
This returned error: Class 'TEST_myweb_controller.php' could not be found in '\my\path\to\tests\TEST_myweb_controller.php'
To fix this I changed the class deinition to: class TEST_myweb_controller extends PHPUnit\Framework\TestCase

Unittests running twice in parent class

For many good reasons I put some tests into parent test class und some tests in inhereted class. I have got this structure then:
/bar/foo/foo.php
/bar.php
This command I use to start phpunit: phpunit --configuration unit.xml I tried this one too, but with same result: phpunit --configuration unit.xml --testsuite foo
So now I want to run all tests in a folder foo. Problem is, that tests in bar.php will be running twice and I have no idea why. Do someone has the answer to that? Is it maybe a feature of phpunit? How can I told phpunit not to run them twice?
If I run phpunit bar/foo/foo.php everything works fine and test function in bar.php is running only once.
foo.php
require_once '/bar.php';
class foo_test extends bar_test {
public function test_1_pass() {
$this->assertEquals('a', 'a');
}
}
bar.php
class bar_test extends PHPUnit_Framework_TestCase {
public function test_2_fail() {
$this->assertEquals('a', 'b');
}
}
Unittest responce
PHPUnit 3.7.29 by Sebastian Bergmann.
Configuration read from /unit.xml
F.F
Time: 104 ms, Memory: 3.25Mb
There were 2 failures:
1) bar_test::test_2_fail
Failed asserting that two strings are equal.
/bar.php:6
2) foo_test::test_2_fail
Failed asserting that two strings are equal.
/bar.php:6
FAILURES!
Tests: 3, Assertions: 3, Failures: 2.
unit.xml
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
syntaxCheck="false">
<testsuites>
<testsuite name="foo">
<directory suffix=".php">bar/foo</directory>
</testsuite>
</testsuites>
</phpunit>
If you don't intend to execute the parent test class on its own, make it abstract, this prevents PHPUnit from trying to instantiate it and running its tests.

Categories