PHPUnit 测试是否需要位于“/test”内?目录?
使用 PHPUnit 时,测试是否需要位于 /tests 目录中? PHPUnit 如何知道测试是“测试”?它是否解析文件并查找方法名称,或者使用某种文件命名约定?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不。
通过反射(并由用户指定要查看的目录)。
No.
Via reflection (and by the user specifying a directory to look into).
phpunit myTestStuff.php
PHPUnit_Framework_Test
扩展的类简而言之:
文件名需要以
Test.php
结尾,class需要从PHPUnit_Framework_TestCase
扩展(继承树中的某个点)。不过,将测试代码与生产代码分开是一种很好的做法。通常它反映文件夹结构。但没有什么可以阻止你以不同的方式去做。只需考虑一下,为您的生产代码生成统计信息(指标)会更容易。
也许您希望为您的生产代码使用一些自动检查的编码规则,并为您的测试使用其他规则。
您希望为代码生成代码覆盖率,并使其包含所有生产代码,但不是所有测试。 (PHPUnit 无论如何都不会在覆盖范围中显示您的测试类,而是显示基类和辅助类)
phpunit myTestStuff.php
PHPUnit_Framework_Test
So in short:
Filename needs to end in
Test.php
, class needs to extends fromPHPUnit_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)
约定是通过将
Test
附加到您正在测试的类和文件来命名测试类和文件。例如,My/Cool/User.php
中的My_Cool_User
将使用My/Cool/UserTest.php
中的My_Cool_UserTest
进行测试代码>.我更喜欢将测试放在自己的目录中,但这不是必需的。通过使用命名约定,您可以告诉 PHPUnit 如何查找与常规代码混合的所有测试。默认情况下,PHPUnit 遵循上述内容,因此如果您将其指向名为
myproject
的文件夹,它将在其中查找以Test.php
结尾的所有文件。Convention is to name the test classes and files by appending
Test
to those of the classes you're testing. For exampleMy_Cool_User
inMy/Cool/User.php
would be tested withMy_Cool_UserTest
inMy/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 inTest.php
within it.进入您的设置和目录,然后选择并验证您的测试和源位置是否已添加。
Go into your settings and directories, and select and verify your test and source locations are added.