Skip to content
Advertisement

Passing parameters to PHPUnit

I’m starting to write PHPUnit tests and I’d like the tests to be run from developers machines as well as from our servers. Developers machines are set up differently than the servers and even differently from each other.

To run in these different places it seems to be the person that runs the test is going to have to indicate where it’s being run. The test can then look up the proper config of the machine it’s running on.

I’m imagining something like:

phpunit.bat -X johns_laptop unittest.php

or on the alpha server:

phpunit -X alpha unittest.php

In the test I would be able to get the value if the ‘X’ (or whatever it is) parameter and know, for example, what the path to the app root is for this machine.

It doesn’t look like the command line allows for that – or have I missed something?

Advertisement

Answer

One way would be for you to inspect $argv and $argc. Something like:

<?php

require_once 'PHPUnit/Framework/TestCase.php';

class EnvironmentTest extends PHPUnit_Framework_TestCase {
    public function testHasParam() {
            global $argv, $argc;
            $this->assertGreaterThan(2, $argc, 'No environment name passed');
            $environment = $argv[2];
    }
}

Then you can call your phpunittest like this:

phpunit EnvironmentTest.php my-computer
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement