phpunit extend test suite - php

I extended PHPUnit_Framework_TestSuite to overwrite the setUp and tearDown methode because I need PHPUnit to do some operation before and after a suite of test. (the test are in multiple TestCase class.)
class MyTestSuite extends PHPUnit_Framework_TestSuite {
protected function setUp()
{
//do some stuff before all tests are run
}
protected function tearDown()
{
//do some stuff after all tests are run
}
}
How in a xml config file do I tell phpunit to use this TestSuite class and then bound the testCase class to it? All I can find are example like this which doesnt seem like specify which test suite class phpunit shall use.
<phpunit>
<testsuites>
<testsuite name="money">
<file>tests/IntlFormatterTest.php</file>
<file>tests/MoneyTest.php</file>
<file>tests/CurrencyTest.php</file>
</testsuite>
</testsuites>
</phpunit>

In https://phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.testsuites there is this information:
The <testsuites> element and its one or more <testsuite> children can
be used to compose a test suite out of test suites and test cases.
I use Ecomdev_PHPUnit in Magento, which does subclass like you want, look at its xml:
<testsuite name="Magento Test Suite">
<file>app/code/community/EcomDev/PHPUnit/Test/Suite.php</file>
</testsuite>
So just pass the test suite complete path I guess!

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!

PHPunit Framework TypeError when running my first test

I have set up the PHPunit Framework for testing and upon running a simple test I am getting a TypeError below:
SampleTest::testTrueAssertsToTrue
TypeError: Argument 3 passed to
SebastianBergmann\GlobalState\Snapshot::__construct() must be of the type boolean, null given, called in /usr/share/php/PHPUnit/Framework/TestCase.php on line 2412
My test case is below:
class SampleTest extends \PHPUnit_Framework_TestCase
{
public function testTrueAssertsToTrue()
{
$this->assertTrue(true);
}
}
The PHPunit version is ^6.2 and below is the configuration XML file:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
stopOnFailure="false">
<testsuites>
<testsuite name ="Test suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
Please help I have searched online whole day and I cant find a solution.
Because you're using phpunit 6.2, \PHPUnit_Framework_TestCase has been removed and you should be instead extending the namespaced PHPUnit\Framework\TestCase
<?php
use PHPUnit\Framework\TestCase;
class SampleTest extends TestCase
{
public function testTrueAssertsToTrue()
{
$this->assertTrue(true);
}
}
You can verify the successful build here on travis-ci:
https://travis-ci.org/scratchers/phpunit6truetest/builds/242989226

phpunit no tests executed

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 ;)

PHPUnit Hangs on Assert

Everytime an assert fails in my PHP code, the PHPUnit command line hangs for ever. I expected it will show the error (assert) and move on to the next test. Is that not expected?
Here is my code:
class MyTest extends PHPUnit_Framework_TestCase
{
public function testSomething()
{
// send async request
// wait for request to complete
$this->assert(<something>);
}
}
phpunit just sits there forever when is false. I tried different kind of asserts to no help.
PHPUnit config is pretty simple actually.
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="name">
<file>tests/ATest.php</file>
<file>tests/BTest.php</file>
<file>tests/CTest.php</file>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
<blacklist>
<directory>src/models</directory>
</blacklist>
</filter>
</phpunit>
The assert is also nothing special.
For example, checking if some string is non-empty.
$this->assert("" !== $model->getId(), "ID is required.")

How to test every module in my application phpunit - zend framework 2

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.

Categories