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 ;)
Related
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 have a parent and a child classes - loaded with spl_register_autoload()) and I'm trying to write PHPUnit tests; I have a problem with the inheritance : when I run tests, this warning message appears:
Warning: include(classes/PHPUnit\Composer\Autoload\ClassLoader.class.php): failed to open stream: No such file or directory in C:\wamp64\www\test\test\unit_tests\test\Employe9Test.php on line 36.
Any idea?
The relative path to ClassLoader.class.php is not found when the file is called from Employe9Test.php
If you need an autoloader for your tests, may I suggest you to use the bootstrap property in the phpunit config file?
<phpunit bootstrap="src/autoload.php">
<testsuites>
<testsuite name="money">
<file>tests/IntlFormatterTest.php</file>
<file>tests/MoneyTest.php</file>
<file>tests/CurrencyTest.php</file>
</testsuite>
</testsuites>
</phpunit>
Source
That way you won't have to adjust/edit the autoload file path in each of your test file if the autoload file path changes.
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.
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') );
I have somewhat complex dir structure for my tests.
En example:
unitests
unitests/moduleA
unitests/moduleA/t1.php
unitests/moduleA/t2.php
unitests/moduleA/t3.php
unitests/moduleB
unitests/moduleB/t1.php
unitests/moduleB/t2.php
unitests/moduleB/t3.php
I might have deeper directory structure.
How would I run ALL the tests in one command?
(I tried phpunit unitests/*/* which ran only the first file found unitests/moduleA/t1.php)
You should use a configuration file, it will make things easier to run the tests in the future.
If you create a phpunit.xml file containing :
<phpunit>
<testsuites>
<testsuite name="Unit">
<directory>unitests/*</directory>
</testsuite>
</testsuites>
</phpunit>
and run phpunit, all tests should run.
You should also suffix all your classes with Test.
Also it's not a bad idea to create a test suite per sub-folder.