PHPUnit 测试是否需要位于“/test”内?目录?

发布于 2024-12-05 20:09:48 字数 89 浏览 1 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

余生共白头 2024-12-12 20:09:48

测试需要位于 /tests 目录内吗?

不。

PHPUnit 如何知道测试是“测试”?

通过反射(并由用户指定要查看的目录)。

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).

失去的东西太少 2024-12-12 20:09:48

它是否解析文件并查找方法名称,或使用某种文件命名约定?

简而言之:

文件名需要以 Test.php 结尾,class需要从 PHPUnit_Framework_TestCase 扩展(继承树中的某个点)。

不过,将测试代码与生产代码分开是一种很好的做法。通常它反映文件夹结构。但没有什么可以阻止你以不同的方式去做。只需考虑一下,为您的生产代码生成统计信息(指标)会更容易。

也许您希望为您的生产代码使用一些自动检查的编码规则,并为您的测试使用其他规则。

您希望为代码生成代码覆盖率,并使其包含所有生产代码,但不是所有测试。 (PHPUnit 无论如何都不会在覆盖范围中显示您的测试类,而是显示基类和辅助类)

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)

感悟人生的甜 2024-12-12 20:09:48

约定是通过将​​ 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 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.

动听の歌 2024-12-12 20:09:48

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

Go into your settings and directories, and select and verify your test and source locations are added.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文