Rui Moreira
Rui Moreira

Reputation: 167

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.

Upvotes: 10

Views: 11434

Answers (3)

Jeff Puckett
Jeff Puckett

Reputation: 40861

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>

Upvotes: 8

Fredrik Sch&#246;ld
Fredrik Sch&#246;ld

Reputation: 1658

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

Upvotes: 0

Akash Panda
Akash Panda

Reputation: 320

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

Upvotes: 7

Related Questions