Codeception with Yii Part 1 – Acceptance Tests

I will be working with a default, standard, bare Yii webapp generated from the command line – called yiicodecept.

> yiic webapp yiicodecept

!Important! For starters, delete the protected/tests directory – Codeception will create what it needs in there.

We generally follow Codeception install instructions here:
Summary: Save .phar to root/protected of webapp (yiicodecept/protected) and run

> php codecept.phar bootstrap

Bootstrap script will run:

Initializing Codeception in C:\apps\xampp\htdocs\yiicodecept\protected
File codeception.yml created <- global configuration
tests/unit created <- unit tests
tests/functional created <- functional tests
tests/acceptance created <- acceptance tests
tests/unit.suite.yml written <- unit tests suite configuration
tests/functional.suite.yml written <- functional tests suite configuration
tests/acceptance.suite.yml written <- acceptance tests suite configuration
tests/_bootstrap.php written <- global bootstrap file
Building initial Guy classes
Building Guy classes for suites: acceptance, functional, unit
WebGuy includes modules: PhpBrowser, WebHelper
WebGuy.php generated successfully. 47 methods added
TestGuy includes modules: Filesystem, TestHelper
TestGuy.php generated successfully. 12 methods added
CodeGuy includes modules: CodeHelper
CodeGuy.php generated successfully. 1 methods added
Bootstrap is done. Check out C:\apps\xampp\htdocs\yiicodecept\protected/tests directory

Create your first Codeception Acceptance test using the command line test generator

> php codecept.phar generate:cept acceptance Welcome

This will create a file called tests/acceptance/WelcomeCept.php

Now edit the test (WelcomeCept.php) to fit our application. We will be checking to make sure that when the user visits the home page, they are presented with the text “Welcome” somewhere on the page.

$I = new WebGuy($scenario);
$I->wantTo('ensure that frontpage works');

Configure Acceptance tests by editing the application URL in tests/acceptance.suite.yml (Make sure your .yml file does not contain any tabs – use spaces for indentation)

# acceptance.suite.yml
class_name: WebGuy
 enabled: [PhpBrowser, WebHelper]
   url: 'http://localhost/yiicodecept/index-test.php'

Run your test!

> php codecept.phar run

Or, for detailed output of each step:

> php codecept.phar run acceptance --steps

The result summary will let you know if anything went wrong, but be sure to read all of the text above to get a feel for what is going on.

OK (1 test, 1 assertion)

Now, for a more complex example, if you’re interested…

> php codecept.phar generate:cept acceptance Login
$I = new WebGuy($scenario);
$I->wantTo('test the login page');
$I->amGoingTo('sign in with invalid user/pw');
$I->submitForm('#login-form', array(
 'LoginForm' => array(
 'username' => 'xxyyxx',
 'password' => 'blargh'
$I->see('Incorrect username or password.');
$I->amGoingTo('sign in with valid user/pw');
$I->submitForm('#login-form', array(
 'LoginForm' => array(
 'username' => 'demo',
 'password' => 'demo'
$I->see('Logout (demo)');
$I->amGoingTo('log this user out');
$I->click('Logout (demo)');


2 replies on “Codeception with Yii Part 1 – Acceptance Tests”

in this example I am running everything from inside the protected directory, which is where codecept.phar is located

Leave a Reply

Your email address will not be published. Required fields are marked *