将变量传递给PhpUnit [英] Pass variable to PhpUnit

查看:78
本文介绍了将变量传递给PhpUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用phpunit开发了一组测试用例,用于测试开发站点和生产站点.唯一的区别是域名.如何将域从命令行传递给phpunit测试用例,以使我不需要修改每个文件?

I developed a set of test cases using phpunit used for testing both development site and the production site. The only difference is the domain name. How can I pass the domain from command line to phpunit test cases so that I do not need to modify every file?

推荐答案

在您的phpunit-bootstrap.php中,您可以执行以下操作:

In your phpunit-bootstrap.php you can do something like this:

$site = getenv('DOMAIN');

并使用

<php>
  <env name="DOMAIN" value="http://production.com"/>
</php>

在您的phpunit.xml文件中 如文档中所示 .

in your phpunit.xml file as shown in the documentation.

这种方法的缺点是您需要拥有两个不同的xml文件.

The downside of this approach is that you need to have two different xml files.

其他选项正在phpunit周围使用自定义包装脚本,例如@DavidHarkness显示的效果很好

Other options are using a custom wrapper script around phpunit like @DavidHarkness showed which tends to work out well

或者,如果您不想以自动化方式(或同时使用这两种方式)运行这些测试,请执行以下操作:

or, if you don't want to run those tests in an automated way (or use both approaches) to do something like:

$site = getenv('DOMAIN');
if(!$site) {
    echo "Enter Domain: ";
    $site = fgets(STDIN);
}

让跑步者检查环境,如果那里什么都没有,请询​​问您的域名.

and have the runner check the environment and if nothing is there ask you for the domain.

php可以从外界获取输入的几乎所有方式都一样.

The same goes for pretty much every way that php can take in input from the outside world.

<php>
  <define name="DOMAIN" value="http://production.com"/>
</php>

在反正使用常量的情况下也可以使用.

also works in case you are using a constant anyways, for example.

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

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