Home » Php » php – Run custom code after Codeception suite has finished

php – Run custom code after Codeception suite has finished

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am aware of the _bootstrap.php file that’s used to set up the testing enviroment, etc., but I’m looking for a way to run some code after the entire test suite has finished.

Note that I’m not looking for a way to run code after a single class, i.e. something like _after, but after all classes.

Is there a way to achieve this?

How to&Answers:

Actually managed to solve this myself, here’s how, if anyone is interested.

I created a new helper class inside _support.

<?php

class DataHelper extends \Codeception\Module
{
    public function _beforeSuite()
    {
        // Set up before test suite
    }

    public function _afterSuite()
    {
        // Tear down after test suite
    }
}

You can then enable this as a module in any suite configuration (the .yml files), like this:

modules:
    enabled:
        - DataHelper

Answer:

@Sacha’s solution is specially useful if you want to share the same methods accross all suites.

If you’re looking for a way to define the methods for a specific suite (or if you want a different method per suite), you can define those methods directly in the suite Helper class.

For instance, if you want to define a _afterSuite method for the Acceptance Suite, just go to support/AcceptanceHelper.php and define those methods there. Eg:

<?php
namespace Codeception\Module;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

class AcceptanceHelper extends \Codeception\Module
{
    public function _afterSuite() {
        die('everything done');
    }
}