Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include file in phpunit test?

I'm having some trouble including a file in a phpunit test. For example: when I execute the following code in PhpStorm I get the expected output.

Code:

class NifvalidationTest extends PHPUnit_Framework_TestCase
{
    public function test_apiRequest()
    {
        $result = 1+1;
        $this->assertEquals(2, $result);
    }
}

Output:

Testing started at 16:58 ...
PHPUnit 5.2.12 by Sebastian Bergmann and contributors.



Time: 120 ms, Memory: 11.50Mb

OK (1 test, 1 assertion)

Process finished with exit code 0

But when I need to access a method from another class using the include, I dont get the expected output. Just as an example, when I execute the following code:

class NifvalidationTest extends PHPUnit_Framework_TestCase
{
    public function test_apiRequest()
    {
        include('/../nifvalidation.php');
        $result = 1+1;
        $this->assertEquals(2, $result);
    }
}

I get this instead of the expected output:

Testing started at 17:05 ...
PHPUnit 5.2.12 by Sebastian Bergmann and contributors.


Process finished with exit code 0

Any ideas on why the include is breaking the test?

Note 1: In the above example I dont need to include the file, but I need in another tests.

Note 2: The path to the file 'nifvalidation.php' is correct.

like image 562
Rui Moreira Avatar asked May 24 '16 16:05

Rui Moreira


People also ask

What is the PHPUnit XML configuration file used to organize tests?

The <testsuite> Element.

What is PHPUnit XML file?

PHPUnit uses XML file for configuration. This configuration file can be added to your version control system so all developers get the same output. These settings will help you make sure your tests work the way you want.

What is assertion in PHPUnit?

PHPUnit assertTrue() Function The assertTrue() function is a builtin function in PHPUnit and is used to assert whether the assert value is true or not. This assertion will return true in the case if the assert value is true else returns false. In case of true the asserted test case got passed else test case got failed.


3 Answers

You can use the bootstrap flag when invoking the tests from the command line to include any files, define constants, and load all your variables, classes, etc.

--bootstrap <file>        A "bootstrap" PHP file that is run before the tests.

For example, create an autoload.php which defines the constants and includes your files, then you can invoke it from the command line as such:

phpunit --bootstrap autoload.php testsFolder/NifvalidationTest

For a more automated approach, you can also create a phpunit.xml file which contains the bootstrap information:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="autoload.php">
    <testsuites>
        <testsuite name="Nifvalidation Test Suite">
            <directory>./testsFolder</directory>
        </testsuite>
    </testsuites>
</phpunit>

In this specific case, based on your comments about the contents of nifvalidation.php the script exits because PS_VERSION is not defined. If you're doing isolated unit tests that simply require one dummy constant defined, then you can define that to be present in your test environment. Then you can simply bootstrap your nifvalidation.php file.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../nifvalidation.php">
    <testsuites>
        <testsuite name="Nifvalidation Test Suite">
            <directory>./testsFolder</directory>
        </testsuite>
    </testsuites>
    <php>
        <const name="PS_VERSION" value="whatever you need here"/>
    </php>
</phpunit>
like image 119
Jeff Puckett Avatar answered Oct 14 '22 23:10

Jeff Puckett


I think your include path is wrong. Your structure may be somewhat like this
ParentDir
  -> nifvalidation.php
  -> testsFolder
     -> NifvalidationTest.php

instead of

include('/../nifvalidation.php')

use

include(dirname(__FILE__)."/../nifvalidation.php");
like image 38
Akash Panda Avatar answered Oct 14 '22 21:10

Akash Panda


Use require() or require_once() instead of using include()

like image 25
Fredrik Schöld Avatar answered Oct 14 '22 21:10

Fredrik Schöld