简体   繁体   English

我如何在超过1个网站上执行phpunit测试

[英]How can i execute phpunit tests on more than 1 website

I have created a set of testcases created using phpunit and selenium that I execute on a website. 我创建了一组用phpunit和Selenium创建的测试用例,并在网站上执行。 But now, there is a 2nd website for which I must execute the same tests. 但是现在,有一个第二个网站,我必须对其执行相同的测试。 The only difference is the url to access the website. 唯一的区别是访问网站的网址。

I have tried using a SESSION variable that is set to different value each time phpunit runs. 我尝试使用SESSION变量,每次phpunit运行时将其设置为不同的值。 In the each test case I would reference this SESSION var but it is not working for me. 在每个测试用例中,我都将引用此SESSION变量,但它对我不起作用。 How do I deal with this? 我该如何处理? I don't want to have 2 versions of identical testcases. 我不想有2个相同测试用例的版本。

What I tried: 我试过的

session_start();
    $environments = array('www.test1.com', 'www.test2.com');
    $arrlength = count($environments);

    for($x = 0; $x < $arrlength; $x++) {
        $_SESSION['TEST_ENVIRONMENT'] = $environments[$x]; // Set session variable = environment
        phpunit -c phpunit.xml // xml file containing the testcases
        unset($_SESSION['TEST_ENVIRONMENT']); //Unset only TEST_ENVIRONMENT index in session variable
    }

Instead of session variables I suggest you make use of environment variables . 建议您使用环境变量来代替会话变量 Environment variables allow you to tailor the environment a program or script runs in. 环境变量使您可以定制运行程序或脚本的环境。

They also have less side-effects then sessions which might not even work on the command-lone . 与会话相比,它们的副作用要小一些,而会话可能甚至无法在命令行上运行 Also environment variables are more directly accessible within your test-suite. 此外,环境变量可以在测试套件中更直接地访问。 And they fulfill your need to pass a value to your test-suite. 他们满足了您将价值传递给测试套件的需求。

So let's see an example: 因此,让我们看一个例子:

$environments = array('www.test1.com', 'www.test2.com');
foreach ($environments as $environment) {
    putenv(sprintf("TEST_ENVIRONMENT=%s", $environment));
    passthru('phpunit -c phpunit.xml');
}

The putenv php function is used here to set the TEST_ENVIRONMENT environment variable. putenv PHP函数在这里用来设置TEST_ENVIRONMENT环境变量。 Then when phpunit is executed, the sub-shell phpunit is executed in has inherited the PHP-scripts environment. 然后当执行phpunit时,在已继承PHP脚本环境的情况下执行子外壳phpunit。

In your tests, when you need to access that TEST_ENVIRONMENT environment variable, you can do it then with 在测试中,当您需要访问该TEST_ENVIRONMENT环境变量时,可以使用

getenv("TEST_ENVIRONMENT");

or 要么

$_ENV["TEST_ENVIRONMENT"];

As you can see there is no need to start a session etc. and access is pretty straight forward. 如您所见,无需启动会话等,访问非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM