Pass variable to PhpUnit(将变量传递给 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将变量传递给 PhpUnit


基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01