php 将参数传递给 PHPUnit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2094970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:59:59  来源:igfitidea点击:

Passing parameters to PHPUnit

phpphpunit

提问by dl__

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.

我开始编写 PHPUnit 测试,我希望这些测试可以在开发人员的机器和我们的服务器上运行。开发人员机器的设置与服务器不同,甚至彼此不同。

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.

要在这些不同的地方运行,似乎是运行测试的人必须指明它正在运行的位置。然后测试可以查找它运行的机器的正确配置。

I'm imagining something like:

我在想象这样的事情:

phpunit.bat -X johns_laptop unittest.php

phpunit.bat -X johns_laptop unittest.php

or on the alpha server:

或在 alpha 服务器上:

phpunit -X alpha unittest.php

phpunit -X alpha unittest.php

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.

在测试中,如果 'X'(或任何它是)参数,我将能够获得该值,并且知道,例如,这台机器的应用程序根路径是什么。

It doesn't look like the command line allows for that - or have I missed something?

看起来命令行不允许这样做 - 或者我错过了什么?

采纳答案by Paolo

One way would be for you to inspect $argv and $argc. Something like:

一种方法是让您检查 $argv 和 $argc。就像是:

<?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];
    }
}

Then you can call your phpunittest like this:

然后你可以像这样调用你的 phpunittest:

phpunit EnvironmentTest.php my-computer

回答by Mark Theunissen

You can use PHPUnit's --bootstrap switch for this.

为此,您可以使用 PHPUnit 的 --bootstrap 开关。

--bootstrap <file>       A "bootstrap" PHP file that is run before the tests.

Then, make a bootstrap.php file that contains variables:

然后,创建一个包含变量的 bootstrap.php 文件:

$port = 4445;

In your tests, you can grab those values:

在您的测试中,您可以获取这些值:

global $port;
$this->setPort($port);

Then run:

然后运行:

phpunit --bootstrap boot.php MyTest.php

回答by scribu

An elegant way to pass variables to both bootstrap files as well as to test files is by using environment variables:

将变量传递给引导文件和测试文件的一种优雅方式是使用环境变量:

export MY_ENV_VAR="some value"

phpunit all

Then, in your PHP files, you can access it like this:

然后,在您的 PHP 文件中,您可以像这样访问它:

getenv('MY_ENV_VAR')

Source: http://blog.lysender.com/2010/10/phpunit-passing-environment-variable-to-your-application/

来源:http: //blog.lysender.com/2010/10/phpunit-passing-environment-variable-to-your-application/

回答by Victor

As Jasir already mentioned, a one line solution would be to set environment variable before phpunit call.

正如 Jasir 已经提到的,单行解决方案是在 phpunit 调用之前设置环境变量。

On Linux:

在 Linux 上:

X=alpha phpunit unittest.php

On Windows probably:

在 Windows 上可能:

set X=johns_laptop && phpunit.bat unittest.php

And inside your script use

在你的脚本中使用

getenv('X')

to read the value

读取值

回答by Charles

I don't think answers above solve my same problem.

我不认为上面的答案解决了我同样的问题。

The accepted answer is not perfect. In this way, custom options should always be put to the end of the parameters list, and there is no indicator to tell that they are custom options. If the number of custom options which I need is not fixed, I should code a lot to parse custom options with regular expressions or something like that.

接受的答案并不完美。这样,自定义选项应始终放在参数列表的末尾,并且没有指示符表明它们是自定义选项。如果我需要的自定义选项的数量不固定,我应该编写很多代码来使用正则表达式或类似的东西解析自定义选项。

The environment variables solution is good, but not natural. Looks weird.

环境变量解决方案很好,但不自然。看起来很奇怪。

VAR1=aaa VAR2=bbb VAR3=ccc ./phpunit-custom-option CustomOptionTest.php

The shell script plus setUp() solution share the same weakness with the accepted one. May be you should code a lot to parse the file and handle unpredictable numbers of custom options.

shell 脚本加上 setUp() 解决方案与公认的解决方案共享相同的弱点。可能您应该编写大量代码来解析文件并处理不可预测数量的自定义选项。

I don't think the bootstrap script is the correct solution. It could be used to handle dirty works automatically, with doing same things every time but not dealing with change parts good.

我不认为引导脚本是正确的解决方案。它可以用来自动处理脏活,每次都做同样的事情,但不能很好地处理变化的部分。

I don't like all the above answers.

我不喜欢以上所有答案。

And I have no good idea myself too. But maybe what I've done could give you inspiration. I've forked the phpunit project on GitHub, and modified code a little, and made it to support the custom option feature.

我自己也没有什么好主意。但也许我所做的可以给你灵感。我在 GitHub 上 fork 了 phpunit 项目,并稍微修改了代码,使其支持自定义选项功能。

enter image description here

在此处输入图片说明

Modified version of phpunit, could accept custom options like this:

phpunit 的修改版本,可以接受这样的自定义选项:

./phpuint-custom-option --custom var1=value1 --custom var2=value2 CustomOptionTest.php

And in the test, you can visit the custom options by accessing the super global variables $_SERVER

并且在测试中,可以通过访问超级全局变量$_SERVER来访问自定义选项

<?php

class CustomOptionTest extends PHPUnit_Framework_TestCase {

    public function testCustomOption() {
        $this->assertEquals('value1', $_SERVER['var1']);
        $this->assertEquals('value2', $_SERVER['var2']);
    }
}

and you can find my code here, and download the modified version here(by click the "view the full file" link on the page).

你可以找到我的代码在这里,并下载修改后的版本在这里(通过点击页面上的“查看完整的文件”链接)。

FYI. this articleis the similar solution.

供参考。这篇文章是类似的解决方案。

回答by Frug

All the solutions here are valid for the question, but there is yet another way that might be simpler for some situations. Phing will take arguments passed in the form -Dargument=value

这里的所有解决方案都适用于该问题,但还有另一种方法可能在某些情况下更简单。Phing 将采用以形式传递的参数-Dargument=value

So using phing -Dtest=MyTest.class.php

所以使用 phing -Dtest=MyTest.class.php

You can then use phing conditionals to handle these arguments:

然后,您可以使用 phing 条件来处理这些参数:

<if>
    <isset property="test" />
    <then>
        <property name="testFile" value="${test}" />
    </then>
    <else>
        <property name="testFile" value="AllTests.php" />
    </else>
</if>
<exec command="phpunit --bootstrap myTestFile/bootstrap.php- myTestFolder/${testFile}"
      passthru="true" returnproperty="phpunitreturn" />

回答by ehed

I struggled with this exact issue, and came up with a kind of hacky-yet-convenient solution: I write parameters to a file on disk and retrieve them in the setUp()method:

我在这个确切的问题上苦苦挣扎,并提出了一种简单但方便的解决方案:我将参数写入磁盘上的文件并在setUp()方法中检索它们:

public function setUp() {
    $this->setBrowser('firefox');
    $this->base_url = file_get_contents("selenium_host");
    $this->setBrowserUrl($this->base_url);
}

Rather than calling phpunitor paratestdirectly, I have a shell script to launch the tests. This one invokes paratestand lets me specify the number of processes as well as the host I'd like the tests to run against.

我有一个 shell 脚本来启动测试,而不是直接调用phpunitparatest。这个调用paratest并让我指定进程数以及我希望测试运行的主机。

run_all_tests.sh

run_all_tests.sh

if [  ] 
then
    threads=
else
    threads=5
fi
if [  ]
then
    echo  > selenium_host
else
    echo 'http://defaulthost.com' > selenium_host
fi

vendor/bin/paratest -p$threads -f --colors TestSuite.php

Then, to run with 7 threads against http://adifferenthost.com:

然后,针对http://adifferenthost.com使用 7 个线程运行:

./run_all_tests.sh 7 'http://adifferenthost.com'

./run_all_tests.sh 7 'http://adifferenthost.com'

回答by confirmator

Passing arguments on the command line would make sense if you want to vary the test parameters per test run. Running host-specific tests on different machines is not the best justification for this solution.

如果您想改变每次测试运行的测试参数,在命令行上传递参数是有意义的。在不同的机器上运行特定于主机的测试并不是这个解决方案的最佳理由。

For that, the PHPUnit configuration filemay prove to be more suitable. It gives you control over host- and even request-specific variables including manipulating php.inisettings as well as defining constants, global variables, $_ENV, $_SERVER, and even $_GET, $_POST, etc. This is all done under the <php>node of the configuration file, see Setting PHP INI settings, Constants and Global Variables

为此,PHPUnit 配置文件可能更合适。它可以让你控制主机和甚至请求特定的变量,包括操作php.ini设置以及定义常量,全局变量,$_ENV$_SERVER,甚至$_GET$_POST等,这下全部完成<php>了配置文件的节点,请参见设置PHP INI设置, 常量和全局变量

Symfony2 uses this approach and provides both a phpunit.xml.dist(default config) and a phpunit.xmlwith your unit tests. The latter is gitignored to allow you to customize it for each machine without affecting the repo. You would then run your tests with:

Symfony2 使用这种方法并为您的单元测试提供phpunit.xml.dist(默认配置)和一个phpunit.xml。后者是 gitignored 以允许您为每台机器自定义它而不影响 repo。然后,您将使用以下命令运行测试:

phpunit -c /path/to/phpunit.xml

回答by erenon

If you would like to run tests on remote machine, use ssh then run it. On locale machine you only have to cd to your root dir, then run phpunit.

如果您想在远程机器上运行测试,请使用 ssh 然后运行它。在语言环境机器上,您只需 cd 到您的根目录,然后运行 ​​phpunit。

user@local:/path/to/your/project$ phpunit
user@remote:/var/www/project$ phpunit

Edit: You are talking about a machine dependent configuration. (What kind of conf btw?) My solution is to put these config under the same, not versioncontrolled place, then read/parse it runtime, in the needed set up methds for example.

编辑:您正在谈论依赖于机器的配置。(顺便说一句,什么样的 conf?)我的解决方案是将这些配置放在同一个,而不是版本控制的地方,然后在运行时读取/解析它,例如在需要的设置方法中。