views:

310

answers:

2

This might be a stupid question, but I can't seem to make it to work.

I'm using PHPUnit to test. Currently I have two classes in a file called Tests.php:

class XTest extends PHPUnit_Framework_TestCase {...}
class YTest extends PHPUnit_Framework_TestCase {...}

However, I'm unable to run both classes. I'm running the following command on Windows:

php "C:\Program Files (x86)\PHP\phpunit" Tests

And it tries to run a test class called "Tests". Instead, I'd like it to run "XTest" and "YTest" and all that are on the file. How could I run multiple test classes easily?

+2  A: 

The PHPUnit Docs explain the arguments the command line test runner expects.

In your case, you're providing Tests, which means PHPUnit looks for a class Tests in a file Tests.php.

With this knowledge, it's easy to see that the best way to organise your tests will be to write one test class per file, with the filenames equal to TestClassName.php.

However, if for some reason you don't want to do that, you can provide an extra argument to tell the test runner which file the test class is declared in:

php "C:\Program Files (x86)\PHP\phpunit" XTest Tests.php
php "C:\Program Files (x86)\PHP\phpunit" YTest Tests.php
Ben James
But is there an easy way to run multiple tests? I don't want to run 200 different commands to test 200 classes...
rFactor
Sure, if you give the test runner a directory as the argument, it will traverse the directory recursively running all the tests it finds.
Ben James
A: 

Putting all of your tests under the same directory and asking PHPUnit to traverse them recursively would work, but if you have your tests under different directories or only want to run specific portions of specific test classes, then the @group annotation might be what you're looking for.

When you execute your tests, you can use the php "C:\Program Files (x86)\PHP\phpunit" --group <insert_name_of_group_to_which_xtests_and_ytests_belong> and PHPUnit will only execute those tests that have @group insert_name_of_group_to_which_xtests_and_ytests_belong in their PHPDoc.

Mike