Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to control tests order in Codeception?

I just started using Codeception after years of writing unit tests in plain PHPUnit. One thing that is bugging me, that I can't find a way to control the order in which the tests are invoked.

In pure old PHPUnit I was building the test structure manually like this:

$suite = new PHPUnit_Framework_TestSuite();
$suite->addTest('MyFirstTest');
$suite->addTest('MySecondTest');

and the test would be invoked in the order which they were added to the suite. Codeception on the other hand seems to be iterating through directories and running every test it can find.

I would like to be able to control the order of the tests on two levels:

  1. The order in which different kind of tests are invoked (i.e. I would like to run unit tests before acceptance tests)
  2. I would like to control the order of tests invoked in specific test type (in similar manner the PHPUnit builds suites)

Ad. 2: Let's say I have two tests in acceptance directory:

AbcCept.php
WebGuy.php
XyzCept.php

I want to be able to run the XyzCept.php before AbcCept.php. Is this even possible?

And to anticipate picky comments: yes, I know that tests should be able to run in any order, and not depend on each other, but that's not what I'm asking.

like image 242
Maciej Sz Avatar asked Dec 21 '13 18:12

Maciej Sz


People also ask

How do you skip tests in Codeception?

The codeception library is built on top of phpunit, so to skip test there is a function markTestSkipped . Every code after this function will not be executed. It is good to add message explaining why test was skipped. Skipped tests will be marked with capital letter S .

Does Codeception use selenium?

Codeception is very flexible framework that you can use to write your Selenium tests.

What is Cest Codeception?

Cest is a common test format for Codeception, it is “Test” with the first C letter in it. It is scenario-driven format so all tests written in it are executed step by step. Unless you need direct access to application code inside a test, Cest format is recommended.


2 Answers

For the ones who are searching for a proper solution and bump into this issue.
I believe, what you truly want to achieve, is that a specific test ran before another test. The order itself is probably not that important. In order to do that, you can add the @dependency annotation to a test.
Full documentation can be found here: https://codeception.com/docs/07-AdvancedUsage#Dependencies. Example:

class ModeratorCest {

    public function login(AcceptanceTester $I)
    {
        // logs moderator in
    }

    /**
     * @depends login
     */
    public function banUser(AcceptanceTester $I)
    {
        // bans user
    }
}
like image 178
Bob Claerhout Avatar answered Sep 20 '22 06:09

Bob Claerhout


Files get sorted by name (I assume we are talking about files from the same directory). In other words if you need to run the test XyzCept.php before AbcCept.php you re-name the XyzCept.php to, let's say, AazCept.php.

like image 26
akond Avatar answered Sep 22 '22 06:09

akond