在命令行上将参数发送到PHP Unit [英] Send parameter to PHP Unit on the command line
问题描述
我想使用命令行将参数发送到PHP Unit.
I want to send a parameter to PHP Unit using the command line.
例如
./phpunit --foo='bar' AllTests
我该怎么做?
我能够实现的最接近目标是使用以下内容:
The closest I was able to achieve my objective is using the following:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<php>
<env name="foo" value="bar"/>
</php>
</phpunit>
然后我可以使用$_ENV['foo']
访问该变量.
I can then access the variable using $_ENV['foo']
.
但是,我想使用命令行发送此变量.
However, I want to send this variable using the command line.
推荐答案
使用phpunit.xml
显然是用于恒定的环境变量,并且使用它来将更改的参数传递给测试会有点麻烦.
Using phpunit.xml
is obviously for constant environmental variables and using it for passing changing parameters to tests is a bit of an overhead.
您可以执行以下操作之一(请参阅将参数传递给PHPUnit 讨论):
What you can do is one of the following (see Passing parameters to PHPUnit for the discussion):
示例: FOO=bar ./phpunit AllTests
优点:很简单.
缺点:取决于环境;需要记住变量的名称(如果有很多的话就不那么简单了);没有明显的支持/必需参数的文档.
Cons: Depends on environment; requires remembering names of the variables (which is not that simple if there are many); no obvious documentation on supported/necessary parameters available.
示例: ./phpunit AllTests bar
优点:微不足道;独立于环境;对PHPUnit参数没有限制.
Pros: Trivial; independent from environment; no limitations to PHPUnit parameters.
缺点::如果有多个参数,尤其是大多数参数是可选的,那将是一件痛苦的事情.没有关于预期参数的明显文档.
Cons: Will be a pain if there are more than several arguments, especially if most of them are optional; no obvious documentation for expected arguments.
示例: . run.sh AllTests bar
其中,run.sh
查看所提供的参数并将其导出到环境中.
Example: . run.sh AllTests bar
where run.sh
looks at the provided arguments and exports them into the environment.
优点:实施起来还是比较琐碎的;添加预期参数列表的文档;添加错误处理(例如,如果bar
是必需参数,但未提供).
Pros: Still more or less trivial to implement; adds documentation of the expected argument list; adds error handling (e.g. in case bar
is a required parameter, but is not provided).
缺点::runner中的PHPUnit参数是硬编码的;取决于环境.
Cons: PHPUnit parameters inside runner are hardcoded; dependent on the environment.
示例: ./phpunit --foo='bar' AllTests
优点:完全满足您的要求.
缺点:实施起来并不容易;需要进行分叉,这使其严重依赖于当前PHPUnit版本的CLI.
Cons: Not so trivial to implement; requires forking which makes it strongly dependent on CLI of the current PHPUnit version.
示例: run.sh --foo=bar --coverage-html=baz
其中run.sh
调用一些run.php
,该c10随后通过解析器运行命令参数,构建用于运行测试的命令并执行该操作.
Example: run.sh --foo=bar --coverage-html=baz
where run.sh
calls some run.php
which in its turn runs command arguments through parser, builds the command for running tests and does that.
优点:现在,您可以执行任何操作并添加所需的任何参数.您可以实现自己的记录器,也可以在多线程中运行测试.
Pros: Now you can do whatever you like and add any parameters you need. You can implement your own logger, you can run tests in multithread etc.
缺点:难以实施;有时需要维护;强烈依赖于PHPUnit CLI.
Cons: Hard to implement; sometimes needs maintenance; is strongly dependent on PHPUnit CLI.
这篇关于在命令行上将参数发送到PHP Unit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!