Oct 2, 2024
0
Level 10
How to create test coverage for modules?
I tried to create a test-coverage for my modules folder. In the past I could generate code-coverage repots without issue for ./app dir.
This is my current phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">./modules</directory>
</include>
<exclude>
<directory suffix=".blade.php">./modules</directory>
<file>./app/_ide_helper.php</file>
</exclude>
</coverage>
<testsuites>
<testsuite name="modules">
<directory suffix="Test.php">./modules/Example/Tests</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>mysql</group>
</exclude>
</groups>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="testing"/>
<env name="MAIL_MAILER" value="log"/>
</php>
</phpunit>
<?php
namespace Modules\Example\Tests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Modules\Example\Chatbot;
use Tests\TestCase;
class ExamleTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function create_course_for_qualification_prints()
{
$chatBot = new Chatbot();
$this->assertEquals('Hello World!', $chatBot->sayHello());
}
}
<?php
namespace Modules\Example;
class Chatbot
{
public function sayHello(): string
{
return 'Hello World!';
}
}
I am using Laravel 9.52.16 with PHP 8.2.23
When I run the code-coverage, it has the code files inside modules in their, but the index.php does not show a single line of code coverage.
If I change the setup to
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
<exclude>
<file>./app/_ide_helper.php</file>
</exclude>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="features">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="modules">
<directory suffix="Test.php">./modules/*/Tests</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>mysql</group>
</exclude>
</groups>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="testing"/>
<env name="MAIL_MAILER" value="log"/>
</php>
</phpunit>
It works, but only files inside ./app are shown.
Please or to participate in this conversation.