Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing multiple classes with PHPUnit

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?

like image 755
Tower Avatar asked Jan 01 '10 14:01

Tower


2 Answers

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.

like image 192
Michael Moussa Avatar answered Nov 12 '22 00:11

Michael Moussa


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
like image 33
Ben James Avatar answered Nov 11 '22 22:11

Ben James