PHPUnit test suite include path

后端 未结 3 1791
迷失自我
迷失自我 2021-02-13 20:04

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:

相关标签:
3条回答
  • 2021-02-13 20:38

    First, I don't get how require_once('../StringCalculator.php'); works, it should rather be: require_once('../Application/StringCalculator.php');.

    Then, slashingweapon answer is good and it's the best IMO, however if you don't want that much trouble, you can specify your require_once to start from the directory of the current file:

    require_once(__DIR__ . '../Application/StringCalculator.php');
    
    0 讨论(0)
  • 2021-02-13 20:43

    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>
    
    0 讨论(0)
  • 2021-02-13 20:46

    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

    0 讨论(0)
提交回复
热议问题