Welcome Guest, Not a member yet? Register   Sign In
PHPUnit and extended class
#1

Hello, I am testing PHPunit with CI4.

I run a test on HTTP testing (to test routes), so my classe extends "FeatureTestCase"

And when I run my test, PHPUNIT says : 

Code:
1) Warning

No tests found in class "CodeIgniter\Test\FeatureTestCase".



This is my test class :
PHP Code:
<?php

namespace App;

use 
CodeIgniter\Test\FeatureTestCase;

class 
TestUrlExists extends FeatureTestCase
{
    public function setUp(): void
    
{
        parent::setUp();
    }

    public function tearDown(): void
    
{
        parent::tearDown();
    }

    public function testHome(){
        
        $result 
$this->call('get''/');        
        $this
->assertTrue($result->isOK());

    }

    public function testContact(){        
        $result 
$this->call('get''/contact');        
        $this
->assertTrue($result->isOK());
    }




I just want to "exclude" the parent class from the code coverage, but I search everywhere, I didn't find. I think it's more touchy because it's not a file but a namespace...

Please, how can I exclude the framework's class from my test ?

Thanks
Reply
#2

(03-17-2021, 11:30 AM)WebboyFR Wrote: Please, how can I exclude the framework's class from my test ?

By default  the framework's test classes are excluded.
How do you include it?

Here is CI 4.1.1 test results. No "CodeIgniter\Test\FeatureTestCase".
Code:
$ vendor/bin/phpunit --debug
PHPUnit 9.5.2 by Sebastian Bergmann and contributors.

Test 'ExampleDatabaseTest::testModelFindAll' started
Test 'ExampleDatabaseTest::testModelFindAll' ended
Test 'ExampleDatabaseTest::testSoftDeleteLeavesRow' started
Test 'ExampleDatabaseTest::testSoftDeleteLeavesRow' ended
Test 'ExampleSessionTest::testSessionSimple' started
Test 'ExampleSessionTest::testSessionSimple' ended
Test 'HealthTest::testIsDefinedAppPath' started
Test 'HealthTest::testIsDefinedAppPath' ended
Test 'HealthTest::testBaseUrlHasBeenSet' started
Test 'HealthTest::testBaseUrlHasBeenSet' ended


Time: 00:00.472, Memory: 22.00 MB

OK (5 tests, 6 assertions)

Generating code coverage report in Clover XML format ... done [00:00.157]

Generating code coverage report in HTML format ... done [00:00.261]

Generating code coverage report in PHP format ... done [00:00.001]


Code Coverage Report:  
  2021-03-17 17:49:56  
                       
Summary:              
  Classes: 20.00% (1/5)
  Methods: 16.67% (1/6)
  Lines:   20.45% (9/44)

Config\Database
  Methods: 100.00% ( 1/ 1)   Lines: 100.00% (  4/  4)
Reply
#3

Thanks.

I didn't install "code coverage driver"

Code:
PHPUnit 9.5.3 by Sebastian Bergmann and contributors.

Warning:       No code coverage driver available

Test 'App\Libraries\FooTest::testFooNotBar' started

It could be that ?
Reply
#4

After installed xdebug, I got this :
Code:
./vendor/bin/phpunit --debug
Cannot load Xdebug - it was already loaded
PHPUnit 9.5.3 by Sebastian Bergmann and contributors.

Test 'App\Libraries\FooTest::testFooNotBar' started
Test 'App\Libraries\FooTest::testFooNotBar' ended
Test 'App\TestUrlExists::testURLS' started
Test 'App\TestUrlExists::testURLS' ended
Test 'Warning' started
Test 'Warning' ended


Time: 00:00.238, Memory: 26.00 MB

There was 1 error:

1) App\TestUrlExists::testURLS
CodeIgniter\Exceptions\PageNotFoundException: Controller or its method is not found: \App\Controllers\Hypnotherapeute-toulouse::index

/opt/lampp/htdocs/hypno/system/CodeIgniter.php:1003
/opt/lampp/htdocs/hypno/system/CodeIgniter.php:349
/opt/lampp/htdocs/hypno/system/Test/FeatureTestTrait.php:182
/opt/lampp/htdocs/hypno/tests/App/UrlExistsTest.php:76

--

There was 1 warning:

1) Warning
No tests found in class "CodeIgniter\Test\FeatureTestCase".

ERRORS!
Tests: 3, Assertions: 2, Errors: 1, Warnings: 1.

Generating code coverage report in Clover XML format ... done [00:00.074]

Generating code coverage report in HTML format ... done [00:00.112]

Generating code coverage report in PHP format ... done [00:00]


Code Coverage Report:   
  2021-03-18 03:49:20   
                         
Summary:               
  Classes: 33.33% (2/6) 
  Methods: 33.33% (3/9) 
  Lines:   34.69% (17/49)

App\Controllers\BaseController
  Methods: 100.00% ( 1/ 1)   Lines: 100.00% (  2/  2)
App\Controllers\Home
  Methods:  33.33% ( 1/ 3)   Lines:  40.00% (  2/  5)
Config\Database
  Methods: 100.00% ( 1/ 1)   Lines: 100.00% (  4/  4)


What is that in my result ?
Code:
Test 'Warning' started
Test 'Warning' ended

It seems like I made "Warning" test...  Maybe a namespace problem ?

I think I've missed something...  Big Grin

This is the top of my Test Class file :
PHP Code:
<?php

namespace App;

use 
CodeIgniter\Test\FeatureTestCase;

class 
TestUrlExists extends FeatureTestCase
{
    public function setUp(): void
    
{
        parent::setUp();
    }

    public function tearDown(): void
    
{
        parent::tearDown();
    }
........ 
Reply
#5

(This post was last modified: 03-18-2021, 10:32 PM by kenjis.)

Can you show your phpunit.xml.dist?

(03-18-2021, 01:55 AM)WebboyFR Wrote: What is that in my result ?
Code:
Test 'Warning' started
Test 'Warning' ended

It seems like I made "Warning" test...  Maybe a namespace problem ?

You have "Warning" test case. Do you have it?
Reply
#6

phpunit.xml.dist :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        bootstrap="system/Test/bootstrap.php"
        backupGlobals="false"
        colors="true"
        convertErrorsToExceptions="true"
        convertNoticesToExceptions="true"
        convertWarningsToExceptions="true"
        stopOnError="false"
        stopOnFailure="false"
        stopOnIncomplete="false"
        stopOnSkipped="false"
        xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
    <coverage includeUncoveredFiles="true" processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
        <exclude>
            <directory suffix=".php">./app/Views</directory>
            <directory suffix=".php">./system</directory>
            <directory suffix=".php">./vendor</directory>
            <file>./app/Config/Routes.php</file>
        </exclude>
        <report>
            <clover outputFile="build/logs/clover.xml"/>
            <html outputDirectory="build/logs/html"/>
            <php outputFile="build/logs/coverage.serialized"/>
            <text outputFile="php://stdout" showUncoveredFiles="false"/>
        </report>
    </coverage>
    <testsuites>
        <testsuite name="App">
            <directory>./tests</directory>
            <exclude>./system/Test/FeatureTestCase.php</exclude>
        </testsuite>
    </testsuites>
    <logging>
        <testdoxHtml outputFile="build/logs/testdox.html"/>
        <testdoxText outputFile="build/logs/testdox.txt"/>
        <junit outputFile="build/logs/logfile.xml"/>
    </logging>
    <php>
        <server name="app.baseURL" value="http://hypnose.local"/>
        <!-- Directory containing phpunit.xml -->
        <const name="HOMEPATH" value="./"/>
        <!-- Directory containing the Paths config file -->
        <const name="CONFIGPATH" value="./app/Config/"/>
        <!-- Directory containing the front controller (index.php) -->
        <const name="PUBLICPATH" value="./public/"/>
        <!-- Database configuration -->
        <!-- Uncomment to provide your own database for testing-->
        <env name="database.tests.hostname" value="localhost"/>
        <env name="database.tests.database" value="hypno"/>
        <env name="database.tests.username" value="root"/>
        <env name="database.tests.password" value=""/>
        <env name="database.tests.DBDriver" value="MySQLi"/>
        <env name="database.tests.DBPrefix" value="tests_"/>
        
    </php>
</phpunit>



And no, I don't wrote any "Warning" test case.
Reply
#7

(This post was last modified: 03-19-2021, 02:28 AM by kenjis.)

Code:
        <testsuite name="App">
            <directory>./tests</directory>
            <exclude>./system/Test/FeatureTestCase.php</exclude>
        </testsuite>
PHPUnit looks for test case files in "./tests".

Your phpunit.xml.dist seems to be okay.

I don't know what's wrong.

<directory>./tests</directory>

It means the following:

<directory suffix="Test.php">./tests</directory>

You need to name ***Test.php for test case files.
Reply
#8

Thanks for the look kenjis.
Yes I tested to exclude the FeatureTestCase file in phpunit.xml, but I think its more a namespace problem or an autoinstanciation of the parent class with extends.

Maybe the problem is in these lines ?
PHP Code:
public function setUp(): void
    
{
        parent::setUp();
    }

    public function tearDown(): void
    
{
        parent::tearDown();
    
Reply
#9

(This post was last modified: 03-19-2021, 03:19 AM by kenjis.)

I think the setUp() and tearDown() are no problems.
But you can remove them, because you don't add any code
to the methods.

Code:
Test 'App\Libraries\FooTest::testFooNotBar' started
Test 'App\Libraries\FooTest::testFooNotBar' ended
Test 'App\TestUrlExists::testURLS' started
Test 'App\TestUrlExists::testURLS' ended
Test 'Warning' started
Test 'Warning' ended
The default configuration of PHPUnit looks for *Test.php.
So FooTest is okay, but why TestUrlExists and Warning?

Do you have `phpunit.xml`?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB