PHPUnit Code coverage not generating reports correctly - php

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.

Related

How to compare 2 URL adres with PHPUnit?

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.

Symfony\Bundle\FrameworkBundle\Test\WebTestCase class not found

I created fresh Symfony 4.1.x project, but when I'm trying to create and execute functional test I am getting error:
PHP Fatal error: Class
'Symfony\Bundle\FrameworkBundle\Test\WebTestCase' not found in
/home/tomasz/my_project/tests/ExampleControllerTest.php on line 7
Steps to reproduce:
Create project
composer create-project symfony/skeleton my_project
Install phpunit, functional tests components and maker for generating skeleton of functional test
composer req --dev symfony/phpunit-bridge symfony/css-selector symfony/browser-kit symfony/maker-bundle
Create example functional test
bin/console make:functional-test ExampleControllerTest
Content of created file:
<?php
namespace App\Tests;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ExampleControllerTest extends WebTestCase
{
public function testSomething()
{
$client = static::createClient();
$crawler = $client->request('GET', '/');
$this->assertSame(200, $client->getResponse()->getStatusCode());
$this->assertContains('Hello World', $crawler->filter('h1')->text());
}
}
Run tests
bin/phpunit
Output (after installing phpunit dependencies):
PHP Fatal error: Class
'Symfony\Bundle\FrameworkBundle\Test\WebTestCase' not found in
/home/tomasz/my_project/tests/ExampleControllerTest.php on line 7
I checked and class Symfony\Bundle\FrameworkBundle\Test\WebTestCase does exist in vendor/. Any clues?
phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
</phpunit>

PHPUnit: Mockery not found when using #runTestsInSeparateProcesses

I'm using PHPUnit with Mockery, the latter i installed via composer:
"require-dev": {
"mockery/mockery": "0.9.*"
},
Now, consider the following test case
<?php
use Mockery as m;
class FooTest extends PHPUnit_Framework_TestCase {
function testFoo() {
m::mock('DateTime');
}
}
phpunit run fine. Now consider the following:
use Mockery as m;
/**
* #runTestsInSeparateProcesses
* #preserveGlobalState disabled
*/
class FooTest extends PHPUnit_Framework_TestCase {
function testFoo() {
m::mock('DateTime');
}
}
In this case i get a Class 'Mockery' not found exception.
Mockery is not found - actually, nothing in the /vendor directory is found, as if the composer autoloading is totally messed up. I've ran both composer update and composer dump-autoload.
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
I'm using PHPUnit 3.7.10 and i run tests with command line phpunit without any args.
How can i fix this?
Upgrading to PHPUnit 4.x fixed this issue for me.

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.")

Categories