Skip to content

Latest commit

 

History

History
205 lines (131 loc) · 7.56 KB

02-GettingStarted.md

File metadata and controls

205 lines (131 loc) · 7.56 KB

Getting Started

Let's take a look into Codeception architecture. We assume that you already installed it, and bootstrapped your first test suites. Codeception has generated three of them: unit, functional, and acceptance. They are well described in the previous chapter. Inside your /tests folder you will have three config files and three directories with names corresponding to these suites. Suites are independent groups of tests with a common purpose.

Actors (Guys)

One of the main concepts of Codeception is representation of tests as actions of a person. By default we call this person a Guy (but you may choose different naming on bootstrap). We have a CodeGuy, who executes functions and tests the code. We also have a TestGuy, a qualified tester, who tests the application as a whole, with knowledge of its internals. And a WebGuy, a user who works with our application through an interface that we provide.

Each of these Guys are PHP classes along with the actions that they are allowed to do. As you can see, each of these Guys have different abilities. They are not constant, you can extend them. You can create new Guys If you like, but one Guy belongs to one suite. You can see the Guy classes inside the suite directories.

Guy classes are not written, they are generated by the build command:

$ php codecept.phar build

Guy classes are generated from suite configuration. When you change configuration guy classes will be rebuilt automatically.

Writing a Sample Scenario

By default tests are written as narrative scenarios. To make a PHP file a valid scenario, its name should have a Cept suffix.

Let's say, we created a file tests/acceptance/SigninCept.php

We can do that by running command:

$ php codecept.phar generate:cept acceptance Signin
<?php
$I = new WebGuy($scenario);
?>

A Scenario always starts with Guy class initialization. After that, writing a scenario is just like typing $I-> and choosing a proper action from the auto-completion list.

Let's sign in to our site. We assume that we have a 'login' page where we are getting authenticated by login and password. Then we are moved to a user page, where we see the text Hello, %username%. Let's look at how this scenario is written in Codeception.

<?php
$I = new WebGuy($scenario);
$I->wantTo('log in as regular user');
$I->amOnPage('/login');
$I->fillField('Username','davert');
$I->fillField('Password','qwerty');
$I->click('Login');
$I->see('Hello, davert');
?>

Before we execute this test, we should make sure that the site is running on a local web server. Open the tests/acceptance.suite.yml file and replace the URL with the URL of your web application:

config:
    PhpBrowser:
        url: 'http://myappurl.local'

If you don't have a web server running, you can use the PHP Built-in Web Server which is available in PHP 5.4.

After you set the proper URL, you can run this test with the command:

$ php codecept.phar run

In the output you should see:

Suite acceptance started
Trying log in as regular user (SigninCept.php) - Ok

Suite functional started

Suite unit started


Time: 1 second, Memory: 21.00Mb

OK (1 test, 1 assertions)

Let's get a detailed output:

$ php codecept.phar run acceptance --steps

We should see a step-by-step report on the performed actions.

Suite acceptance started
Trying to log in as regular user (SigninCept.php)
Scenario:
* I am on page "/login"
* I fill field "Username" "davert"
* I fill field "Password" "qwerty"
* I click "Login"
* I see "Hello, davert"
  OK


Time: 0 seconds, Memory: 21.00Mb

OK (1 test, 1 assertions)

That was a very simple test that you can reproduce for your own site. By emulating the user's actions you can test all of your site the same way.

Give it a try!

Modules and Helpers

The actions in Guy classes are taken from modules. With the build command described above, Codeception emulates multiple inheritance. Modules are designed to have one action performed with one method. According to the DRY principle, if you use the same scenario components in different modules, you can combine them and move them to a custom module. By default each suite has an empty module, which can extend Guy classes. They are stored in the _helpers directory.

Bootstrap

Each suite has its own bootstrap file. It's located in the suite directory and is named _bootstrap.php. It will be executed before each test.

Write any setup preparations for the suite there.

Test Formats

Codeception supports three test formats. Besides the previously described scenario-based Cept format, Codeception can also execute PHPUnit test files for unit testing, and class-based Cest format. They are covered in later chapters. There is no difference in the way the tests of either format will be run in the suite.

Configuration

Codeception has a global configuration in codeception.yml and a config for each suite. We also support .dist configuration files. If you have several developers in a project, put shared settings into codeception.dist.yml and personal settings into codeception.yml. Same goes for suite configs. For example, the unit.suite.yml will be merged with unit.suite.dist.yml.

Running Tests

Tests can be started with the run command.

$ php codecept.phar run

With the first argument you can run tests from one suite.

$ php codecept.phar run acceptance

To run exactly one test, add a second argument. Provide a local path to the test, from the suite directory.

$ php codecept.phar run acceptance SigninCept.php

Alternatively you can provide full path to test file:

$ php codecept.phar run tests/acceptance/SigninCept.php

You can provide a directory path as well:

$ php codecept.phar run tests/acceptance/backend

Which will execute all tests from backend dir.

To execute a group of tests that are not stored in the same dir you can organize them in groups.

Reports

To generate JUnit XML output you can provide --xml option, and --html for HTML report.

$ php codecept.phar run --steps --xml --html

This command will run all tests for all suites, displaying the steps, and building HTML and XML reports. Reports will be store in tests/_log/ directory.

And to learn all available options:

$ php codecept.phar help run

Debugging

To receive detailed output tests can be executed with --debug option. You may print any information inside a test using codecept_debug function.

Generators

There are plenty of useful Codeception commands.

  • generate:cept suite filename - Generates a sample Cept scenario.
  • generate:cest suite filename - Generates a sample Cest test.
  • generate:test suite filename - Generates a sample PHPUnit Test with Codeception hooks.
  • generate:phpunit suite filename - Generates a classic PHPUnit Test.
  • generate:suite suite guy - Generates a new suite with the given Guy class name.
  • generate:scenarios suite - Generates text files containing scenarios from tests.

Conclusion

We took a look into the Codeception structure. Most of the things you need were already generated by the bootstrap command. After you have reviewed the basic concepts and configurations, you can start writing your first scenarios.