Below are the paths where files are located,
src\TW\Talk\Entity\Talk.php
src\Tests\Talk\Entity\TalkTest.php
src\phpunit.xml.dist
In TalkTest.php, I have included PHPUnit and the entity Talk.
require_once 'TW/Talk/Entity/Talk.php';
require('PHPUnit/Autoload.php');
Class TalkTest extends PHPUnit_Framework_TestCase
{
...
}
In phpunit.xml.dist file, I have,
<phpunit>
<testsuites>
<testsuite name="TW">
<file>Tests/Talk/Entity/TalkTest.php</file>
</testsuite>
</testsuites>
</phpunit>
I am running phpunit command from src directory, I am getting error that Fatel Error: Class 'Tests\TW\Talk\Enity\Talk' not found.
For reference, I am referring to php-object-freezer-master which has similar structure.
Any idea why the TalkTest is not able to find Talk class ?
phpunit command is trying to find Talk entity in Tests folder.
Changing phpunit.xml.dist to
<phpunit bootstrap="loader.php">
<testsuites>
<testsuite name="TW_Talk">
<directory>Tests</directory>
</testsuite>
</testsuites>
</phpunit>
and loader file as,
<?php
function tw_test_autoloader($class) {
if(file_exists(__DIR__."\\" . $class . ".php"))
require_once(__DIR__."\\" . $class . ".php");
}
spl_autoload_register('tw_test_autoloader');
Worked for me.
But still if I replace directory tag to file
<file>Tests\TW\Talk\Entity\TalkTest.php</file>
It does not work.
Check your include_path:
echo get_include_path();
It should contain the directory to which your TW/Talk/Entity/Talk.php is relative. If it is not there, then you must add it either to php.ini or to PHPUnit's bootstrap.
You can easily test if PHP can find your file using your include path with this:
var_dump( stream_resolve_include_path('TW/Talk/Entity/Talk.php') );
Related
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
I am working on a task that is expose a xml file with given url. I want to test my code and I do this with PHPUnit. I used composer to install PHPUnit.
The example test that I want to run is this :
<?php
use PHPUnit\Framework\TestCase;
class IndexTest extends TestCase{
public function testGetXmlWithUrl(){
require 'index.php';
$XmlClass = new XmlReaderClass("localhost", "8000", "status.xml", "");
$url= $XmlClass->$url;
$myUrl = "http://localhost:8000/status.xml?password=";
$this->assertEquals($myUrl, $url);
}} ?>
TestCase class cannot be added. I mean even PhpUnit name is not colored in blue in the code. So I guess I could not include it in my project actually. Error is like:
Fatal error: Uncaught Error: Class 'PHPUnit\Framework\TestCase' not found in /Users/demetsen/Desktop/tests/IndexTest.php:4
My second problem is when I try to run testGetXmlWithUrl() method the result is :
Failed asserting that SimpleXMLElement Object (...) matches expected '\n ....
Why am I getting this result and how can I solve it?
A default phpunit.xml (or better, use phpunit.xml.dist, so you can copy/edit it locally if needed), can be created with phpunit --generate-configuration will include a line for bootstrap:
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
bootstrap="path/to/bootstrap.php"
<!-- more lines -->
</phpunit>
Here, the bootstrap="...." line can, at its simplest, point to the composer autoloader. PHPunit can also create a suitable file for you.
$ php ./phpunit-9.2.phar --generate-configuration # or vendor/bin/phpunit, via composer
PHPUnit 9.2.6 by Sebastian Bergmann and contributors.
Generating phpunit.xml in /home/username/code/test
Bootstrap script (relative to path shown above; default: vendor/autoload.php):
Tests directory (relative to path shown above; default: tests):
Source directory (relative to path shown above; default: src):
Generated phpunit.xml in /home/username/code/test
# optional, but useful:
# mv phpunit.xml phpunit.xml.dist # .dist will be read if .xml does not exist
The output file:
$ cat phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.2/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
If you had something you needed to add to the bootstrap process, create a file with your needs, and you'd probably also add something like require __DIR__.'/vendor/autoload.php'; to setup the autoloading.
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
I have a 'No tests executed' with phpunit..
This line works
$ phpunit install/InstallDbTest.php
...
<result expected>
...
$ cat suites/installtests.xml
<phpunit>
<testsuites>
<testsuite name="database">
<file>install/InstallDbTest.php</file>
</testsuite>
</testsuites>
</phpunit>
$ phpunit -c suites/installtests.xml
PHPUnit 4.7.4 by Sebastian Bergmann and contributors.
Time: 130 ms, Memory: 11.25Mb
No tests executed!
Does anyone can tell me what I am doing wrong ?
Thanks in advance!
You need to correct the path in your phpunit.xml.
It is trying to find the filesuites/install/InstallDbTest.php. Since the file doesn't exist, no tests are run and you get the message.
Change the configuration to the following:
<phpunit>
<testsuites>
<testsuite name="database">
<file>../install/InstallDbTest.php</file>
</testsuite>
</testsuites>
</phpunit>
File paths in phpunit.xml are all relative to the location of the xml file.
Maybe in the InstallDbTest.php file you are missing:
<?php
...
For those of you who you use a different bootstrap file than the Composer's autoload file, make sure you don't use one of your test case classes.
Indeed, PHPUnit (version 6.2 in my case) add to the running TestSuite all PHP files for which no class is loaded yet (see how it's done in the code). From the TestSuite::addTestFile($filename) method:
$classes = get_declared_classes();
$filename = Fileloader::checkAndLoad($filename);
$newClasses = \array_diff(\get_declared_classes(), $classes);
So if your class already belong to the get_declared_classes it will be ignored.
To summarize: don't use your test class before ;)
I already made test for my application module and other module. They are working fine but i want to run all the test (application module and other module) toguether to generate clover report for jenkins. What should i do?? create another config file to invoke other config files??
---edit---
I have the same code for each module bootstrap.php and i want to use the same for each module to avoid code duplication. Rigth now i have two modules Application and Problem when i run phpunit it throws me this error:
**.PHP Fatal error: Class 'ProblemTest\Bootstrap' not found ....**
The test for the Application module works fine but the test for Problem module doesn't work for the namespace declaration in the phpunit_bootstrap.php file.
My Application test uses this declaration:
<?php
namespace ApplicationTest\Controller;
use ApplicationTest\Bootstrap; ....
My Problem tes uses this declaration:
<?php
namespace ProblemTest\Controller;
use ProblemTest\Bootstrap;
This is my phpunit.xml
<phpunit bootstrap="phpunit_bootstrap.php">
<testsuites>
<testsuite name="ALL">
<directory>module/Application/test</directory>
<directory>module/Problem/test</directory>
</testsuite>
</testsuites>
this is my phpunit_bootstrap.php
<?php
namespace ApplicationTest;
The problem that i have rigth now to run all the test toguether is how to include the same bootstrap for every test to avoid that exception.
You can make a testsuite to run a bunch of individual groups of tests all at once. In your phpunit.xml.dist file:
<phpunit bootstrap="phpunit_bootstrap.php">
<testsuites>
<testsuite name="all">
<directory>./</directory>
</testsuite>
<testsuite name="ALL">
<directory>/path/to/module1tests</directory>
<directory>/path/to/module2tests</directory>
<directory>/path/to/module3tests</directory>
<exclude>/path/to/module4tests</exclude>
</testsuite>
<testsuite name="module1only">
<directory>./module1tests</directory>
</testsuite>
</testsuites>
And then you can run it with: /path/to/phpunit --testsuite="ALL"
I can't comment yet so another solution to this issue can be found at zend framework 2 + phpunit + multiple modules + continuous integration If you don't want to go through naming each module independently.