Search code examples
yii2codeceptionacceptance-testing

Yii2 acceptance tests. Rollback all changes that were made in acceptance test after each test


I need to revert all changes that were made in database by acceptance test after each acceptance test.

As described in Yii2 docs, you should set transaction: false for Yii2 module in acceptance.suite.yml. And you should use cleanup: true and transaction: true for Db module in codeception.yml.

Actual: changes that acceptance test did in database are not reverted.

Expected: changes that acceptance test did in database are reverted.

codeception.yml

namespace: common\tests
actor_suffix: Tester
paths:
    tests: tests
    output: tests/_output
    data: tests/_data
    support: tests/_support
settings:
    bootstrap: _bootstrap.php
    colors: true
    memory_limit: 1024M
modules:
    config:
        Yii2:
            configFile: 'config/test-local.php'
        Db:
            dsn: '%TEST_DB_TYPE%:host=%TEST_DB_HOST%;port=%TEST_DB_PORT%;dbname=%TEST_DB_NAME%'
            user: '%TEST_DB_USERNAME%'
            password: '%TEST_DB_PASSWORD%'
            cleanup: true
            transaction: true
params:
    - config/params-local.php

acceptance.suite.yml

suite_namespace: frontend\tests\acceptance
actor: AcceptanceTester
modules:
  enabled:
    - WebDriver
    - Yii2
  config:
    Yii2:
      part: init
      transaction: false
      cleanup: true
    WebDriver:
      browser: chrome
      url: 'http://mysite.dev/'
      port: 9515 # ChromeDriver port
      window_size: 1920x1080
      clear_cookies: true
      restart: true
      capabilities:
        chromeOptions:
          args: ["--headless", "--disable-gpu"]
          binary: "/usr/bin/google-chrome-stable"

extensions:
  enabled:
  - Codeception\Extension\RunProcess:
    - chromedriver --url-base=/wd/hub

Solution

  • Yii2 module has no effect on execution of your application code in acceptance tests.

    It is impossible to know what changes were made to database by your application, so your only option is to restore database from backup.

    In order to have database in a known state before test you can load SQL file by using dump option of DB module, but don't do that for your production database.