Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit test suite include path

Using phpunit and I am having some trouble with include paths, not for phpunit itself, but for my code and tests directory.

I have the following code structure:

Application
  -StringCalculator.php

tests
  -StringCalculatorTest.php

Inside my StringCalculatorTest.php i have a require statement:

require_once('../StringCalculator.php');

Running phpunit StringCalculatorTest.php from inside the tests folder works perfectly.

However, when i then introduce a phpunit.xml configuration file in the root directory i.e.

Application
  -StringCalculator.php

tests
  -StringCalculatorTest.php

phpunit.xml

the include path is screwed. I have to replace the require_once to

require_once('StringCalculator.php');

What is the correct way to set include paths between the application and the test directory?

like image 274
user1783931 Avatar asked Oct 29 '12 21:10

user1783931


People also ask

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

The <testsuites> Element This element is the root for one or more <testsuite> elements that are used to configure the tests that are to be executed.

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.

Is PHPUnit a framework?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. PHPUnit 9 is the current stable version. PHPUnit 10 is currently in development.


2 Answers

The best place to set your PHP include path is in your bootstrap file. Usually, your phpunit.xml file will include a bootstrap attribute:

<phpunit backupGlobals="true"
     backupStaticAttributes="false"
     bootstrap="bootstrap.php"
     cacheTokens="true"
     colors="true"
     ... and so on ...
</phpunit>

Then in your bootstrap file you can set include paths, include important files, etc..

set_include_path(get_include_path() . PATH_SEPARATOR . '../my/sources');

The config file is covered in Appendix C of the PHPunit docs.

EDIT: Link updated

like image 144
slashingweapon Avatar answered Oct 09 '22 09:10

slashingweapon


I know the question already been answer
This answer is for future visitor

According to phpunit documentation, u can use includePath directive in your phpunit.xml for your inclusion path

<php>
  <includePath>/path/to/ur/project</includePath>
</php>
like image 43
slier Avatar answered Oct 09 '22 10:10

slier