将参数传递给 PHPUnit

2022-01-25 00:00:00 php phpunit

我开始编写 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.

我在想象这样的事情:

phpunit.bat -X johns_laptop unittest.php

phpunit.bat -X johns_laptop unittest.php

或在 alpha 服务器上:

or on the alpha server:

phpunit -X alpha unittest.php

phpunit -X alpha unittest.php

在测试中,如果是X"(或其他)参数,我将能够获取该值,并且例如知道这台机器的应用程序根路径是什么.

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?

推荐答案

一种方法是检查 $argv 和 $argc.比如:

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];
    }
}

然后你可以像这样调用你的 phpunittest:

Then you can call your phpunittest like this:

phpunit EnvironmentTest.php my-computer

相关文章