4

I would like to run some unit Tests individually with PHPUnit, but I have certain classes separated from the Tests, since I am using the symfony framework, and I group the Tests and the Classes in different folders.

I would like to run the Tests individually like this:

php phpunit.phar MyTest.php

The problem is that the test file uses the classes from the controllers, and phpunit doesnt seem to be able to import the needed classes for the test.

This is not a problem to run all the tests together, thanks to phpunit.xml but when I want to run them individualy, its a problem.

How could I fix this?

4

3 回答 3

10

您必须指出phpunit您的phpunit.xml配置文件所在的位置(例如,因为它必须知道自动加载器)。如果你有默认的 symfony 2 结构,它将在 app 目录中,所以只需像这样运行你的测试(我假设你在项目根路径中):

phpunit -c app/ --filter="concreteTestPattern" src/Acme/DemoBundle/Tests/MyTest.php

编辑:

以上将运行名称与模式匹配的所有测试:/.*concreteTestPattern.*/

于 2013-06-05T07:52:01.767 回答
3

您将--filter在 PHPUnit 命令字符串中使用该参数。这只会运行与给定模式匹配的测试。如果你只传递了你想要运行的测试的完整名称,phpunit 应该只运行那个测试。

如果您有一个与测试关联的数据提供者并且只想运行一个测试用例,您还可以使用--filter <testName>::<testcase name>

于 2013-06-06T13:00:35.357 回答
1

PHPUnit 可以设置为使用配置文件执行。

在我们的 Symfony2 项目中,这个文件位于 app/phpunit.xml.dist。

由于该文件以 .dist 为后缀,因此您需要将其内容复制到名为 app/phpunit.xml 的文件中。

如果您使用的是 Git 等 VCS,则应将新的 app/phpunit.xml 文件添加到 VCS 忽略列表中。

您还会注意到配置指定了位于 app/bootstrap.php.cache 的引导文件。PHPUnit 使用此文件来获取测试环境设置。

我们可以通过从项目的根目录运行以下命令来执行此测试。-c 选项指定 PHPUnit 应该从 app 目录加载其配置。

$ phpunit -c 应用程序

于 2013-08-19T12:49:15.667 回答