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.")
Related
I am trying to execute some test in PHPUnits but it's not getting executed.
<?php
use \PHPUnit\Framework\TestCase;
class UserControllerTest extends TestCase {
public function testThatCanGetNumber() {
$mock = $this->getMockBuilder('UserModel')
->setMethods(array('getNumber'))
->getMock();
$mock->expects($this->once())
->method('getNumber')
->with($this->equalTo(5));
}
}
However, if I extend the class with \PHPUnit_Framework_TestCase it gets executed. Why is that?
This is my phpunit.xml:
<?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>
You expect that the method is getting called but you are not executing anything in the test. You should call the method where the mocked method is called.
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
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 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!
My phpunit xml:
<phpunit bootstrap="bootstrap.php">
<testsuites>
<testsuite name="Phase Unit Tests">
<directory>./Phase</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">Api/app/library</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="build/report" charset="UTF-8"
highlight="false" lowUpperBound="35" highLowerBound="70"/>
<log type="testdox-html" target="build/testdox.html"/>
</logging>
</phpunit>
My test run successfully and I am not getting errors. I have checked for any DIE() and EXIT calls and did not find any in my code. When I look at my report it is just the empty folders with no files being scanned.
I am using PHP_CodeCoverage 1.2.7 using PHP 5.4.3 and PHPUnit 3.7.13 on Windows 8 with Wamp server 2.2
Ok so I wrote a small thing to test PHPUnit coverage reporting... Below is my classes:
/**
* test
*/
class Application
{
public $testprop;
function __construct()
{
$this->testprop = "Stuff";
}
}
/**
* Tests for the application class
*/
class ApplicationTest extends PHPUnit_Framework_TestCase
{
public function testRunTheThing()
{
$app = new Application();
$this->assertEquals($app->testprop, 'Stuff');
}
}
I run the following in my command prompt:
phpunit --coverage-html report ApplicationTest.php
The test passes but the code coverage report generated is of the composer ClassLoader file.
It seems to have been an issue with the versions of phpunit and its dependencies. I ran an update on phpunit and now it is generating the coverage reports successfully. I am now using PHP_CodeCoverage 1.2.16, PHP 5.4.3 and PHPUnit 3.7.32.