16

I want to run something before all the tests inside a particular Cest, and then clean it up after all tests have run, similar to the setUpBeforeClass and tearDownAfterClass method in PHPUnit.

Is there a method to do something like this in Codeception?

Casteurr
  • 956
  • 3
  • 16
  • 35

4 Answers4

8

I have a crude solution for this problem for now, before Codeception guys give you reliable method for this.

Just create another Actor above all your existing actors (test cases) like this:

class MyCest
{

function _before(AcceptanceTester $I)
{
    $I->amOnPage('/mypage.php');
}

public function _after(AcceptanceTester $I)
{

}

function beforeAllTests(AcceptanceTester $I,\Page\MyPage $myPage,\Helper\myHelper $helper){
    //Do what you have to do here - runs only once before all below tests
    //Do something with above arguments
}

public function myFirstTest(AcceptanceTester $I){
    $I->see('Hello World');
}

function afterAllTests(){
    //For something after all tests
}
}

You can put the function beforeAllTests as public but not protected nor should start with "_", for it to run before all your tests.

Another bunch of functions which will only run once before all tests begin which should be instantiated in /tests/_support/Helper/Acceptance.php for acceptance and so on. In this you can call the function :

// HOOK: used after configuration is loaded
public function _initialize()
{
}

// HOOK: before each suite
public function _beforeSuite($settings = array())
{
}

For more functions , go to : https://codeception.com/docs/06-ModulesAndHelpers#Hooks

VK RT
  • 131
  • 1
  • 5
3

From Codeception point of view, Cest class is just a bunch of Cept scenarios. There is no object scope and no before/after class hooks.

My advice is to use Test format instead and use PhpUnit hooks.

Test format extends PHPUnit_Framework_TestCase so setUpBeforeClass should work.

Naktibalda
  • 13,705
  • 5
  • 35
  • 51
1

You can attach new helper in functional.suite.yml:

class_name: FunctionalTester
modules:
    enabled:
      - tests\components\helpers\MyHelper

In helper you can use _before and _after methods:

class FixtureHelper extends \Codeception\Module
{
    /**
     * Method is called before test file run
     */
    public function _before(\Codeception\TestCase $test)
    {
        // TODO: Change the autogenerated stub
    }

    /**
     * Method is called after test file run
     */
    public function _after(TestCase $test)
    {
        // TODO: Change the autogenerated stub
    }
}

TestCase methods can help you determine necessity execute _before and _after.

Onedev_Link
  • 1,981
  • 13
  • 26
  • I'm not sure I follow. Aren't _before and _after run before and every test case? How do I differentiate between cests using this method? – Casteurr Apr 27 '16 at 11:38
  • @Casteurr you need write some code. `TestCase` have a `getTestFullName`. It's may be helpful. – Onedev_Link Apr 27 '16 at 11:41
  • That seems way too complicated. I am in need of something like this https://phpunit.de/manual/current/en/fixtures.html#fixtures.sharing-fixture – Casteurr Apr 27 '16 at 11:47
  • The method in your link works perfectly in Codeception unit tests. Leave out the first line and use something like this as the next line: class YourClassTest extends \Codeception\Test\Unit – Bob Ray Nov 24 '19 at 05:54
1

Depending on what you mean by "run something" and "clean it up", you can use PHP standard Constructor and Destructor.

This solution seems clearer to me, but keep in mind that you haven't access to AcceptanceTester $I, and Scenario $scenario from there, so use it when you don't need them.

class YourCest
{
    private Faker\Generator $faker;
    private string $email;   

    public function __construct()
    {
        // "Run something" here
        $this->faker = Faker\Factory::create();
        $this->email = $this->faker->email;
    }
    
    public function __destruct()
    {
        // "and then clean it up" there
    }

    public function tryToTest(AcceptanceTester $I)
    {
        // Do your tests here
    }
}
Demis Palma ツ
  • 7,669
  • 1
  • 23
  • 28