简体   繁体   中英

do PHPUnit tests need to be inside a “/test” directory?

when using PHPUnit, is it required for tests to be inside of a /tests directory? How does PHPUnit know that a test is a "test"? Does it parse the file and look for method names, or use some sort of naming convention of files?

it required for tests to be inside of a /tests directory?

No.

How does PHPUnit know that a test is a "test"?

Via reflection (and by the user specifying a directory to look into).

Does it parse the file and look for method names, or use some sort of naming convention of files?

So in short:

Filename needs to end in Test.php , class needs to extends from PHPUnit_Framework_TestCase (some point down the inheritance tree).

It is good practice to seperate your test code from your production code though. Usually it mirrors the folder structure. But there is nothing stopping you from doing it differently. Just consider that it's easier to generate statistics (metrics) for your production code.

Maybe you want to have some automatically checked coding rules for your production code and others for your tests.

You want to generate code coverage for your code and have it include all the production code but not all the tests. (PHPUnit will not show your test classes in the coverage anyways but base and helper classes)

Convention is to name the test classes and files by appending Test to those of the classes you're testing. For example My_Cool_User in My/Cool/User.php would be tested with My_Cool_UserTest in My/Cool/UserTest.php .

I prefer to separate the tests in their own directory, but this isn't required. By using a naming convention you can tell PHPUnit how to find all the tests mixed in with your regular code. By default PHPUnit follows the above, so if you point it to a folder called myproject it will look for all files ending in Test.php within it.

进入您的设置和目录,然后选择并验证您的测试和源位置是否已添加。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM