| layout | doc |
|---|---|
| title | 02-GettingStarted - Codeception - Documentation |
Let's take a look at Codeception's 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.
One of the main concepts of Codeception is representation of tests as actions of a person. We have a UnitTester, who executes functions and tests the code. We also have a FunctionalTester, a qualified tester, who tests the application as a whole, with knowledge of its internals. And an AcceptanceTester, a user that works with our application through an interface that we provide.
Actor classes are not written but generated from suite configuration. Methods of actor classes are generally taken from Codeception Modules. Each module provides predefined actions for different testing purposes, and they can be combined to fit the testing environment. Codeception tries to solve 90% of possible testing issues in its modules, so you don't have reinvent the wheel. We think that you can spend more time on writing tests and less on writing support code to make those tests run. By default AcceptanceTester relies on PhpBrowser module, which is set in tests/acceptance.suite.yml configuration file:
{% highlight yaml %}
class_name: AcceptanceTester modules: enabled: - PhpBrowser: url: http://localhost/myapp/ - \Helper\Acceptance
{% endhighlight %}
In this configuration file you can enable/disable and reconfigure modules for your needs.
When you change configuration, actor classes are rebuilt automatically. If Actor classes are not created or updated as you expect, try to generate them manually with build command:
{% highlight bash %}
$ php codecept.phar build
{% endhighlight %}
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 the following command:
{% highlight bash %}
$ php codecept.phar generate:cept acceptance Signin
{% endhighlight %}
A Scenario always starts with Actor class initialization. After that, writing a scenario is just like typing $I-> and choosing a proper action from the auto-completion list.
{% highlight php %}
{% endhighlight %}
Let's log in to our website. We assume that we have a 'login' page where we are getting authenticated by providing a username and password. Then we are sent to a user page, where we see the text Hello, %username%. Let's look at how this scenario is written in Codeception.
{% highlight php %}
wantTo('log in as regular user'); $I->amOnPage('/login'); $I->fillField('Username','davert'); $I->fillField('Password','qwerty'); $I->click('Login'); $I->see('Hello, davert'); ?>{% endhighlight %}
Before we execute this test, we should make sure that the website is running on a local web server. Let's open the tests/acceptance.suite.yml file and replace the URL with the URL of your web application:
{% highlight yaml %}
class_name: AcceptanceTester modules: enabled: - PhpBrowser: url: 'http://myappurl.local' - \Helper\Acceptance
{% endhighlight %}
After we configured the URL we can run this test with the run command:
{% highlight bash %}
$ php codecept.phar run
{% endhighlight %}
Here is the output we should see:
{% highlight bash %}
Acceptance Tests (1) ------------------------------- Trying log in as regular user (SigninCept.php) Ok
Time: 1 second, Memory: 21.00Mb
OK (1 test, 1 assertions)
{% endhighlight %}
Let's get a detailed output:
{% highlight bash %}
$ php codecept.phar run acceptance --steps
{% endhighlight %}
We should see a step-by-step report on the performed actions.
{% highlight bash %}
Acceptance Tests (1) ------------------------------- 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)
{% endhighlight %}
This simple test can be extended to a complete scenario of site usage. So by emulating the user's actions you can test any of your websites.
Give it a try!
Each suite has its own bootstrap file. It's located in the suite directory and is named _bootstrap.php. It will be executed before test suite. There is also a global bootstrap file located in the tests directory. It can be used to include additional files.
Codeception supports three test formats. Beside the previously described scenario-based Cept format, Codeception can also execute PHPUnit test files for unit testing, and Cest format.
Cest combines scenario-driven test approach with OOP design. In case you want to group a few testing scenarios into one you should consider using Cest format. In the example below we are testing CRUD actions within a single file but with a several test (one per each operation):
{% highlight php %}
amOnPage('/'); } function createPage(AcceptanceTester $I) { // todo: write test } function viewPage(AcceptanceTester $I) { // todo: write test } function updatePage(AcceptanceTester $I) { // todo: write test } function deletePage(AcceptanceTester $I) { // todo: write test } } ?>{% endhighlight %}
Such Cest file can be created by running a generator:
{% highlight bash %}
$ php codecept.phar generate:cest acceptance PageCrud
{% endhighlight %}
Learn more about Cest format in Advanced Testing section.
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. The same goes for suite configs. For example, the unit.suite.yml will be merged with unit.suite.dist.yml.
Tests can be started with the run command.
{% highlight bash %}
$ php codecept.phar run
{% endhighlight %}
With the first argument you can run tests from one suite.
{% highlight bash %}
$ php codecept.phar run acceptance
{% endhighlight %}
To run exactly one test, add a second argument. Provide a local path to the test, from the suite directory.
{% highlight bash %}
$ php codecept.phar run acceptance SigninCept.php
{% endhighlight %}
Alternatively you can provide the full path to test file:
{% highlight bash %}
$ php codecept.phar run tests/acceptance/SigninCept.php
{% endhighlight %}
You can execute one test from a test class (for Cest or Test formats)
{% highlight bash %}
$ php codecept.phar run tests/acceptance/SignInCest.php:anonymousLogin
{% endhighlight %}
You can provide a directory path as well:
{% highlight bash %}
$ php codecept.phar run tests/acceptance/backend
{% endhighlight %}
This will execute all tests from the backend dir.
To execute a group of tests that are not stored in the same directory, you can organize them in groups.
To generate JUnit XML output, you can provide the --xml option, and --html for HTML report.
{% highlight bash %}
$ php codecept.phar run --steps --xml --html
{% endhighlight %}
This command will run all tests for all suites, displaying the steps, and building HTML and XML reports. Reports will be stored in the tests/_output/ directory.
To learn all available options, run the following command:
{% highlight bash %}
$ php codecept.phar help run
{% endhighlight %}
To receive detailed output, tests can be executed with the --debug option.
You may print any information inside a test using the codecept_debug function.
There are plenty of useful Codeception commands:
generate:ceptsuite filename - Generates a sample Cept scenariogenerate:cestsuite filename - Generates a sample Cest testgenerate:testsuite filename - Generates a sample PHPUnit Test with Codeception hooksgenerate:phpunitsuite filename - Generates a classic PHPUnit Testgenerate:suitesuite actor - Generates a new suite with the given Actor class namegenerate:scenariossuite - Generates text files containing scenarios from testsgenerate:helperfilename - Generates a sample Helper Filegenerate:pageobjectsuite filename - Generates a sample Page objectgenerate:stepobjectsuite filename - Generates a sample Step objectgenerate:environmentenv - Generates a sample Environment configurationgenerate:groupobjectgroup - Generates a sample Group Extension
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 scenario.
- Next Chapter: AcceptanceTests >
- Previous Chapter: < Introduction