Reputation: 531
I'm trying to make my PHPUnit tests working. I want a result of the test coverage (it will be upload to a sonarqube)
The problem is, when I execute my tests, the coveredmethods still at 0...
src/Controller/DefaultController.php :
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
#[Route('/default', name: 'default')]
public function index(): Response
{
return $this->render('default/index.html.twig', [
'controller_name' => 'DefaultController',
]);
}
#[Route('/default2', name: 'default2')]
public function test2(): Response
{
$test = 0;
$test++;
echo $test;
return $this->render('default/index.html.twig', [
'controller_name' => 'DefaultController',
]);
}
#[Route('/default3', name: 'default3')]
public function test3(): Response
{
$test = 0;
$test++;
echo $test;
return $this->render('default/index.html.twig', [
'controller_name' => 'DefaultController',
]);
}
}
tests/DefautTest.php :
<?php
namespace App\Tests;
use PHPUnit\Framework\TestCase;
class DefautTest extends TestCase
{
/**
* @covers \App\Controller\DefaultController::index
*/
public function testSomething(): void
{
$this->assertTrue(true);
}
}
phpunit.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
convertDeprecationsToExceptions="false"
>
<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src/Controller</directory>
<directory suffix=".php">src/Entity</directory>
<directory suffix=".php">src/Repository</directory>
</include>
<report>
<clover outputFile="result-coverage.xml"/>
<xml outputDirectory="xml-coverage"/>
</report>
</coverage>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
<listener class="Symfony\Bridge\PhpUnit\CoverageListener"/>
</listeners>
And, the result-coverage.xml, with coveredmethods = 0... It would be one; no ?
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1628073827">
<project timestamp="1628073827">
<file name="/usr/src/app/src/Controller/DefaultController.php">
<class name="App\Controller\DefaultController" namespace="global">
<metrics complexity="3" methods="3" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="12" coveredstatements="0" elements="15" coveredelements="0"/>
</class>
<line num="11" type="method" name="index" visibility="public" complexity="1" crap="2" count="0"/>
<line num="14" type="stmt" count="0"/>
<line num="15" type="stmt" count="0"/>
<line num="19" type="method" name="test2" visibility="public" complexity="1" crap="2" count="0"/>
<line num="22" type="stmt" count="0"/>
<line num="24" type="stmt" count="0"/>
<line num="26" type="stmt" count="0"/>
<line num="28" type="stmt" count="0"/>
<line num="29" type="stmt" count="0"/>
<line num="33" type="method" name="test3" visibility="public" complexity="1" crap="2" count="0"/>
<line num="36" type="stmt" count="0"/>
<line num="38" type="stmt" count="0"/>
<line num="40" type="stmt" count="0"/>
<line num="42" type="stmt" count="0"/>
<line num="43" type="stmt" count="0"/>
<metrics loc="46" ncloc="46" classes="1" methods="3" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="12" coveredstatements="0" elements="15" coveredelements="0"/>
</file>
<metrics files="1" loc="46" ncloc="46" classes="1" methods="3" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="12" coveredstatements="0" elements="15" coveredelements="0"/>
</project>
</coverage>
If anyone have an idea why do I have coveredmethods = 0...
Thanks
Upvotes: 0
Views: 151
Reputation: 135
After review the code, the code coverage is the representation about what code was executed with the test, now lets look the test:
<?php
namespace App\Tests;
use PHPUnit\Framework\TestCase;
class DefautTest extends TestCase
{
/**
* @covers \App\Controller\DefaultController::index
*/
public function testSomething(): void
{
$this->assertTrue(true);
}
}
Basically this tests is only making an assertion of true, but not executing the code on the controller, the mark or annotation "@covers" is the representation about, what supposes to cover the test.
Try with this example:
namespace App\Tests;
use PHPUnit\Framework\WebTestCase;
class DefautTest extends WebTestCase
{
public function testFunctional()
{
$client = static::createClient();
$client->request('GET', '/default');
$this->assertResponseIsSuccessful(); //This method can be different of your version of phpunit
}
}
Upvotes: 1