Richard Cross
Richard Cross

Reputation: 474

What is the proper way to use fixtures with Acceptance tests in Codeception?

I am using Yii2, and want to include fixtures with my acceptance tests.

With unit tests you add the fixture trait to the class, then return an array of fixtures with the fixtures method, then call $this->loadFixtures() from the _before() method, and $this->unloadFixtures() from the _after() method. This works perfectly.

With Cept acceptance tests, classes are not used, so I have no idea how you would use fixtures with this process.

With Cest files I tried adding the fixtureTrait to the Cest class, and it didn't work. Should I even be adding the fixture trait to Cest files?

Therefore, what is the proper way to use fixtures with Acceptance tests?

Upvotes: 1

Views: 1555

Answers (1)

Richard Cross
Richard Cross

Reputation: 474

After doing a lot of googling, I have found a satisfactory solution.

With Cept files, as they dont use classes, you have to include the Yii2 module in the YAML configuration, BUT only the fixtures part.

In acceptance.suite.yml:

modules:
    enabled:
        - WebDriver
        - Yii2:
            part:
                - init
                - fixtures
            transaction: false

I am using selenium, so I have also enabled the WebDriver module. I have also found it necessary to set transaction: false, otherwise my logins didn't work.
(Probably due to the transaction data not being available to the web request)

Then I have to run the build command to make the $I->haveFixtures() method available.

codeception build

I can now include fixtures as a part of the Cept files thus:

use tests\fixtures\DBAddressFixture;
use tests\fixtures\DBCustomerFixture;
use tests\fixtures\OAuthClientsFixture;

/* @var $scenario Codeception\Scenario */
$I = new AcceptanceTester($scenario);
$I->wantTo("Check that the account information page exists and works");

$I->haveFixtures([
    'customer' => DBCustomerFixture::className(),
    'address' => DBAddressFixture::className(),
    'auth' => OAuthClientsFixture::className()
]);

Regarding Cest Files. Apparently it should be as simple as adding the _fixtures() method to the Cest class, although I havent tried this.

Here are some Useful links:

Codeception Yii2 Fixtures Documentation

CodeCeption Fixtures Issue 4099

Similar StackOverflow issue 45881907

Yii2 Fixtures Documentation

Upvotes: 3

Related Questions