Hector Ordonez
Hector Ordonez

Reputation: 1104

How to hook into beforeSuite event with Codeception for API Acceptance tests?

I am building API Acceptance tests with Codeception.

I am familiar with Unit tests there and I used the setUp method in those classes for all logic required before running all the tests of the class.

However I didn't find anything like this for Acceptance Tests.

Notice that I am using the "Class" approach, not the procedural way.

So I have a class like this...

class ResourceCest {
    public function _beforeSuite(ApiTester $I)
    {
        // Ideally this would work, but it doesn't.
    }

    public function _before(ApiTester $I) 
    {
        $I->am('Api Tester');
    }
    public function somethingThatIWantToExecute(ApiTester $I)
    {
        $I->sendGet('something');
        // etc
    }
}

I can make a method like setUp, but then Codeception executes it as a test and thus outputting something when running the tests.

Upvotes: 2

Views: 2885

Answers (1)

Luís Cruz
Luís Cruz

Reputation: 14970

You shouldn't define the _beforeSuite inside your Cest classes. Instead, you should use the Helper class inside _support.

Assuming you have a suite called api, you should have a ApiHelper.php class inside _support. There, you can define your methods, for instance:

<?php
namespace Codeception\Module;

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

class ApiHelper extends \Codeception\Module
{
    public function _beforeSuite($I) {
        var_dump($I);
        die();
    }
}

This should do the trick.

Upvotes: 5

Related Questions