I want to send a parameter to PHP Unit using the command line.
e.g.
./phpunit --foo='bar' AllTests
How can I do this?
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>
I can then access the variable using $_ENV['foo']
.
However, I want to send this variable using the command line.
Using phpunit.xml
is obviously for constant environmental variables and using it for passing changing parameters to tests is a bit of an overhead.
What you can do is one of the following (see Passing parameters to PHPUnit for the discussion):
Example: FOO=bar ./phpunit AllTests
Pros: Trivial.
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.
Example: ./phpunit AllTests bar
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.
Example: . run.sh AllTests bar
where run.sh
looks at the provided arguments and exports them into the environment.
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).
Cons: PHPUnit parameters inside runner are hardcoded; dependent on the environment.
Example: ./phpunit --foo='bar' AllTests
Pros: Does exactly what you want.
Cons: Not so trivial to implement; requires forking which makes it strongly dependent on CLI of the current PHPUnit version.
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.
Cons: Hard to implement; sometimes needs maintenance; is strongly dependent on PHPUnit CLI.
You can run any command with environment variable set:
foo=bar ./phpunit AllTests
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With