Making PHPUnit ignore things?

非 Y 不嫁゛ 提交于 2019-12-21 12:41:18

问题


I have a PHPUnit Test class that I'd like to be ignored from a test run. I know I can do it by renaming it so that it doesn't contain the word Test in its filename, but I'd rather not do that since it muddies up the source control waters more than I'd like.

Does anyone have a suggestion?


回答1:


There are a couple of options for the phpunit command that can help define which tests should and should not be run :

$ phpunit --help
PHPUnit 3.4.0beta5 by Sebastian Bergmann.

Usage: phpunit [switches] UnitTest [UnitTest.php]
       phpunit [switches] <directory>
...
  --filter <pattern>       Filter which tests to run.
  --group ...              Only runs tests from the specified group(s).
  --exclude-group ...      Exclude tests from the specified group(s).
  --list-groups            List available test groups.
...

Those will probably not allow to specify exactly what you wanted... But they might help.

For more details, see The Command-Line Test Runner


Especially, I kind of like the group feature : just use a @group tag in the phpdoc of your tests, to re-group them (for instance, one group per "chunk of functionnality") ; and, after, you can use the --group or --exclude-group options on the command line to specify which tests should or shouldn't be run.

In your case, if you cannot modify the tests, maybe the --filter option can help, depending on how your tests are nammed (ie, if there is a way to identify those you want to run) :

--filter
Only runs tests whose name matches the given pattern. The pattern can be either the name of a single test or a regular expression that matches multiple test names.


Another solution, if you are not always changing the "groups" of tests to run, might be to use a test-suite that only includes the tests you want to run.

For instance, take a look at Composing a Test Suite Using XML Configuration.




回答2:


simply rename your method :

testFunctionality () { //blah }

to

ignore_testFunctionality () { //blah }




回答3:


I realise this is an old question with an accepted answer, so I hope I'm not intruding, but I just wanted to point out for completion's sake that it's possible to skip tests. Incomplete and skipped tests aren't executed by PHPUnit.

Of course, that's on a per-test level - however, you can consolidate that by putting it into the class' setUp().

Just a heads-up.




回答4:


I searched the entire PHPUnit manual and didn't find anything. You can ignore code for the purposes of test coverage, but that's about it. However, this ChangeLog (in an apparent fork) mentions that it will "Ignore test file names that are prefixed with ." I haven't tried that, but it's worth a shot. Or you could comment out the test. I'm assuming you'd like something like NUnit offers where it will report the ignored tests; I didn't find anything like that.




回答5:


I prefer using normal names for my functions and put in the function docBloc

/**
* @test
*/
public function getDetails()
{}

So, when I don't want this test to be run, I just remove that @test. Also, maybe you will find something useful here PHPUnit - The XML Configuration File I added in the "exclude" tag tests that I didn't want to be covered by phpunit.



来源:https://stackoverflow.com/questions/1413959/making-phpunit-ignore-things

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