How to wrap PHPUnit to control command line reporting?

对着背影说爱祢 提交于 2019-12-24 11:45:01

问题


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?


回答1:


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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/3937046/how-to-wrap-phpunit-to-control-command-line-reporting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!