如何包装 PHPUnit 来控制命令行报告?

发布于 2024-09-27 16:07:18 字数 382 浏览 0 评论 0原文

所以我有很多 PHPUnit 测试(它们实际上是作为 PHPUnit 测试运行的 Selenium 测试)。当我从命令行运行它们时,测试完成时我会收到此类报告:

..E..F..E.FF...

然后我必须等到所有测试完成运行,然后它才会告诉我错误和失败的内容。我希望能够控制这一点,以便我可以做一些更有用的报告。例如:

testLogin ....... passed
testFoobar ...... failed
    - Failed asserting that foo = true on line 123
testBazbat ...... passed

如何控制 PHPUnit 显示结果的方式?

So I've got a lot of PHPUnit tests (they are actually Selenium tests running as PHPUnit tests). When I run them from the command line, I get this sort of reporting as the tests complete:

..E..F..E.FF...

Then I have to wait until all the tests finish running before it will tell me the errors and what failed. I would like to be able to control this so I can do some more useful reporting. For example:

testLogin ....... passed
testFoobar ...... failed
    - Failed asserting that foo = true on line 123
testBazbat ...... passed

How can I get control over how PHPUnit displays the results?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

不交电费瞎发啥光 2024-10-04 16:07:18

PHPUnit 有一些命令行参数来控制输出格式。对您来说最有用的是 --testdox 和 --tap

它们的工作方式如下:

]> phpunit --tap FooTest.php 
TAP version 13
not ok 1 - Failure: FooTest::test_add
  ---
  message: fark
  severity: fail
  ...
ok 2 - FooTest::test_exists
ok 3 - FooTest::test_show_html
ok 4 - FooTest::test_show_array
ok 5 - FooTest::test_show_empty
ok 6 - FooTest::test_find
1..6


]> phpunit --testdox FooTest.php 
PHPUnit 3.5.0 by Sebastian Bergmann.

Foo
 [ ] test add
 [x] test exists
 [x] test show html
 [x] test show array
 [x] test show empty
 [x] test find

正如您所看到的 --testdox 不会显示失败原因,它的作用就像一种规范生成器一样。但是 --tap 已经非常接近了。

您始终可以编写自己的测试侦听器 - 一个实现 PHPUnit_Framework_Testlistener 接口的自定义类(具有 startTest、endTest、addFailure、addError 等方法;名称非常不言自明,当您的测试套件时发生的事件将调用相应的代码运行)。

此类代码使用 xml 配置文件连接到 phpunit。

可以在此处查看此类自定义侦听器的一个很好的示例: http:// /raphaelstolt.blogspot.com/2010/06/growling-phpunits-test-status.html

PHPUnit has a few command line parameters to control the output format. The most useful ones for your are --testdox and --tap

They work like this:

]> phpunit --tap FooTest.php 
TAP version 13
not ok 1 - Failure: FooTest::test_add
  ---
  message: fark
  severity: fail
  ...
ok 2 - FooTest::test_exists
ok 3 - FooTest::test_show_html
ok 4 - FooTest::test_show_array
ok 5 - FooTest::test_show_empty
ok 6 - FooTest::test_find
1..6


]> phpunit --testdox FooTest.php 
PHPUnit 3.5.0 by Sebastian Bergmann.

Foo
 [ ] test add
 [x] test exists
 [x] test show html
 [x] test show array
 [x] test show empty
 [x] test find

As you can see --testdox does not show the failure reason, its ment to be used like a kind of specification generator. But --tap comes pretty close.

And you can always write your own test listener - a custom class that implements PHPUnit_Framework_Testlistener interface (has methods like startTest, endTest, addFailure, addError etc; the names are pretty self-explanatory, respective code will be called for events that happen when your testsuite runs).

Such code is hooked up into phpunit using the xml configuration file.

One good example of such custom listener can be viewed here: http://raphaelstolt.blogspot.com/2010/06/growling-phpunits-test-status.html

殤城〤 2024-10-04 16:07:18

使用该

--printer path/to/MyTestListener

选项指向您的自定义 MyTestListener 类
http://www.phpunit.de/manual/3.6/en /textui.h​​tml#textui.clioptions

或者可以在 phpunit.xml 配置文件中使用 printerClass 属性进行设置,

请参阅 http://www.phpunit.de/manual/3.6/en/appendixes.configuration.html#appendixes.configuration

这是如何制作自己的打印机类的说明
http://www.phpunit .de/manual/current/en/extending-phpunit.html#extending-phpunit.examples.SimpleTestListener.php

如果您不想从头开始,只需重命名并修改默认打印机
https://github.com/sebastianbergmann/phpunit/blob/master /PHPUnit/TextUI/ResultPrinter.php

use the

--printer path/to/MyTestListener

option to point to your custom MyTestListener class
http://www.phpunit.de/manual/3.6/en/textui.html#textui.clioptions

or it can be set in your phpunit.xml config file with the printerClass property

see http://www.phpunit.de/manual/3.6/en/appendixes.configuration.html#appendixes.configuration

Here's the instructions how to make your own printer class
http://www.phpunit.de/manual/current/en/extending-phpunit.html#extending-phpunit.examples.SimpleTestListener.php

If you don't want to start from scratch just rename and modify the default printer
https://github.com/sebastianbergmann/phpunit/blob/master/PHPUnit/TextUI/ResultPrinter.php

丶视觉 2024-10-04 16:07:18

如果您想要漂亮的 HTML 报告页面,请尝试使用 phingphpunitreport 任务。有关屏幕截图,请参阅我的博客条目。对于较大的测试套件非常有帮助。

If you want pretty HTML report pages, try to use phing's phpunitreport task. For Screenshots, see my blog entry. Very helpful for larger test suites.

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