将参数传递给PHPUnit [英] Passing parameters to PHPUnit

查看:63
本文介绍了将参数传递给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

这篇关于将参数传递给PHPUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆