Run Codeception Acceptance Test within two Integration Tests coded in simple PHPUnit

43 views Asked by At

Consider the following Integration test file extract based on PHPUnit:

...
/**
 * @return void
 */
public function test_first() {
    
    $this->assertTrue(true);
    
}

/**
 * @depends test_first
 * @return void
 */
public function test_acceptance() {

    $codecept = new Codecept();

    $codecept->run(
        suite: 'Acceptance',
        test : 'MyAcceptanceTests'
    );

    $ragg = $codecept->getResultAggregator();

    $this->assertTrue(
        condition: $ragg->wasSuccessfulAndNoTestIsUselessOrSkippedOrIncomplete()
    );
    
}

/**
 * @depends test_acceptance
 * @return void
 */
public function test_second() {
    
    $this->assertTrue(true);
    
}
...

I'm new to Codeception and the client wants to me to integrate browser-testing into their automated test stack. Precisely, they want to make sure that a specific page has the contents you should expect at the moment of a specific application within the state achieved in between two API requests (both tested via test_first and test_second, respectively, both working fine hence irrelevant for this question).

Using the syntax above, this seems to work, but I'm getting the warning:

1) /.../vendor/codeception/codeception/src/Codeception/Codecept.php:257
Undefined array key "seed"

So it seems that this is not the proper way to launch a Codeception Acceptance test from within PHP, as the warning above indicates that the Codecept instance used for the test has not been properly initiated ? Note that this produces a warning if you use PHP 8.3. If you use any version older than that, the script fails with an error, as the first argument of srand (called in the line + codeception file of the warning) must apparently be an int in older PHP versions.

I know the docs technically recommend you to run Codeception tests via command line, but the Codecept class is there to run Codeception tests via PHP, no?

UPDATE :

I've been digging within the docs of the concerned Codecept and related file, and found that srand() is used to initiate the pseudo-random number generator within Codeception. I've not researched why this is done, so my approach may be the wrong solution. So before posting this as an answer, I'd like to know if it's a proper solution.

If I'm properly informed and srand() behaves in PHP as it does in C, srand() will seed the pseudo-random number generator with the current timestamp in PHP if its default value (in PHP that's 0) is provided to its call. According to the Codecept constructor, the options property of a new instance gets initiated via:

$this->options = $this->mergeOptions($options);

The line of code causing the failure when running the Acceptance test via PHP, using the Codecept class, then is:

srand($this->options['seed']);

So I thought by simply providing the default solution for srand() within the Codecept constructor, this should solve the problem. Hence I replaced the line of my code above:

$codecept = new Codecept();

with:

$codecept = new Codecept(['seed' => 0 ]);

And this at least caused no more thrown errors / warnings. If this is the correct solution, what it does under the hood, and why srand() is even used in Codecept::runSuite(), no clue...??

0

There are 0 answers