2

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.

Hector Ordonez
  • 1,044
  • 1
  • 12
  • 20

1 Answers1

5

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.

Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
  • 4
    Does not work for me. Instead of `$I` defined as a `AcceptanceTester` I got an configuration array inside of `_beforeSuite` method – FelikZ Sep 29 '15 at 16:06
  • $settings array is passed into _beforeSuite. if you want $I then you do this $I = $this;. – dwenaus Nov 01 '16 at 18:06