简体   繁体   中英

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. 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. In the each test case I would reference this SESSION var but it is not working for me. How do I deal with this? I don't want to have 2 versions of identical testcases.

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. Then when phpunit is executed, the sub-shell phpunit is executed in has inherited the PHP-scripts environment.

In your tests, when you need to access that TEST_ENVIRONMENT environment variable, you can do it then with

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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