简体   繁体   English

PHPUnit 测试需要在“/test”目录中吗?

[英]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?使用 PHPUnit 时,测试是否需要在 /tests 目录中? How does PHPUnit know that a test is a "test"? PHPUnit 如何知道测试是“测试”? 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?测试是否需要在 /tests 目录中?

No.不。

How does PHPUnit know that a test is a "test"? PHPUnit 如何知道测试是“测试”?

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).文件名需要以Test.php ,类需要从PHPUnit_Framework_TestCase扩展(继承树的某个点)。

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) (PHPUnit 不会在覆盖范围内显示您的测试类,而是显示基类和辅助类)

Convention is to name the test classes and files by appending Test to those of the classes you're testing.约定是通过将Test附加到您正在测试的类中来命名测试类和文件。 For example My_Cool_User in My/Cool/User.php would be tested with My_Cool_UserTest in My/Cool/UserTest.php .例如My_Cool_UserMy/Cool/User.php将与测试My_Cool_UserTestMy/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.通过使用命名约定,您可以告诉 PHPUnit 如何查找与常规代码混合的所有测试。 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.默认情况下,PHPUnit 遵循上述内容,因此如果您将其指向名为myproject的文件夹,它将在其中查找以Test.php结尾的所有文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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