Is there a way to control tests order in Codeception?

Viewed 4228

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.

4 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
    }
}

You can create a batch script that runs the tests in any order you like, e.g.,

cd \path\to\test\dir
codecept run unit
codecept run acceptance

or

cd \path\to\test\dir
codecept run unit
codecept run acceptance XycCept.php
codecept run acceptance AbcCept.php

See the "Run" command on this page.

To select a specific suite, or specify order to run the different test-suites you can use the following syntax (no spaces):

codecept run unit,functional,acceptance
Related