Testomatio - Test Management for Codeception
Let’s take a look at Codeception’s architecture. We’ll assume that you have 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 Introduction. Inside your /tests folder you will have three .yml
config files and three directories: Unit
, Functional
, Acceptance
.
Codeception follows simple naming rules to make it easy to remember (as well as easy to understand) its method names.
Actions start with a plain english verb, like “click” or “fill”. Examples:
$I->click('Login');
$I->fillField('#input-username', 'John Dough');
$I->pressKey('#input-remarks', 'foo');
Assertions always start with “see” or “dontSee”. Examples:
$I->see('Welcome');
$I->seeInTitle('My Company');
$I->seeElement('nav');
$I->dontSeeElement('#error-message');
$I->dontSeeInPageSource('<section class="foo">');
Grabbers take information. The return value of those is meant to be saved as a variable and used later. Example:
$method = $I->grabAttributeFrom('#login-form', 'method');
$I->assertSame('post', $method);
One of the main concepts of Codeception is the 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. Lastly we have an “AcceptanceTester”, a user who works with our application in a real browser.
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 to 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 the PhpBrowser module, which is set in the tests/Acceptance.suite.yml
configuration file:
actor: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: 'http://localhost/myapp/'
In this configuration file you can enable/disable and reconfigure modules for your needs.
When you change the configuration, the actor classes are rebuilt automatically. If the actor classes are not created or updated as you expect,
try to generate them manually with the build
command:
php vendor/bin/codecept build
Codeception has its own testing format called “Cest” (a combination of “Codecept” and “Test”). To start writing a test we need to create a new Cest file. We can do that by running the following command:
php vendor/bin/codecept generate:cest Acceptance Signin
This will generate the SigninCest.php
file inside the tests/Acceptance
directory. Let’s open it:
<?php
namespace Tests\Acceptance;
use Tests\Support\AcceptanceTester;
class SigninCest
{
public function _before(AcceptanceTester $I)
{
}
// tests
public function tryToTest(AcceptanceTester $I)
{
}
}
We have _before
and _after
methods to run some common actions before and after a test. And we have a placeholder method tryToTest
.
If we want to test a signin process it’s a good start to test a successful signin. Let’s rename this method to signInSuccessfully
.
<?php
namespace Tests\Acceptance;
use Tests\Support\AcceptanceTester;
final class SigninCest
{
public function signInSuccessfully(AcceptanceTester $I): void
{
$I->amOnPage('/login');
$I->fillField('Username', 'davert');
$I->fillField('Password', 'qwerty');
$I->click('Login');
$I->see('Hello, davert');
}
}
This scenario can probably be read by everybody, even non-developers. If you just remove all special characters, this test transforms into plain english text:
I amOnPage '/login'
I fillField 'Username', 'davert'
I fillField 'Password', 'qwerty'
I click 'Login'
I see 'Hello, davert'
Codeception generates this text representation from PHP code by executing:
php vendor/bin/codecept generate:scenarios Acceptance
These generated scenarios will be stored in your tests/Support/Data/scenarios/Functional
directory in *.txt
files.
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 fill in the URL of your web application:
actor: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: 'http://myappurl.local'
After configuring the URL we can run this test with the run
command:
php vendor/bin/codecept run
This is the output we should see:
Acceptance Tests (1) -------------------------------
âś” SigninCest: sign in successfully (0.00s)
----------------------------------------------------
Time: 00:00.019, Memory: 12.00 MB
OK (1 test, 1 assertion)
Let’s get some detailed output:
php vendor/bin/codecept run Acceptance --steps
We should see a step-by-step report on the performed actions:
Acceptance Tests (1) -------------------------------
SigninCest: Login to website
Signature: SigninCest.php:signInSuccessfully
Test: tests/Acceptance/SigninCest.php:signInSuccessfully
Scenario --
I am on page "/login"
I fill field "Username" "davert"
I fill field "Password" "qwerty"
I click "Login"
I see "Hello, davert"
PASSED
----------------------------------------------------
Time: 00:00.019, Memory: 12.00 MB
OK (1 test, 1 assertion)
This simple test can be extended to a complete scenario of site usage, therefore, by emulating the user’s actions, you can test any of your websites.
To run more tests, create a public method for each of them. If your tests share common setup actions, put them into the _before()
method:
<?php
namespace Tests\Acceptance;
use Tests\Support\AcceptanceTester;
final class TaskCrudCest
{
public function _before(AcceptanceTester $I): void
{
// will be executed at the beginning of each test
$I->amOnPage('/task');
}
}
Learn more about the Cest format in the Advanced Testing chapter.
Codeception allows execution of user stories in Gherkin format in a similar manner as it is done in Cucumber or Behat. Please refer to the BDD chapter to learn more.
Codeception has a global configuration file 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:
php vendor/bin/codecept run
With the first argument you can run all tests from one suite:
php vendor/bin/codecept run Acceptance
To limit tests run to a single class, add a second argument. Provide a local path to the test class, from the suite directory:
php vendor/bin/codecept run Acceptance SigninCest.php
Alternatively you can provide the full path to the test file:
php vendor/bin/codecept run tests/Acceptance/SigninCest.php
You can further filter which tests to run by appending a method name to the class, separated by a colon:
php vendor/bin/codecept run tests/Acceptance/SigninCest.php:^anonymousLogin$
You can provide a directory path as well. This will execute all Acceptance tests from the backend
dir:
php vendor/bin/codecept run tests/Acceptance/backend
Using regular expressions, you can even run many different test methods from the same directory or class.
For example, this will execute all Acceptance tests from the backend
dir beginning with the word “login”:
php vendor/bin/codecept run tests/Acceptance/backend:^login
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.
php vendor/bin/codecept run --steps --xml --html
This command will run all tests for all suites, displaying the steps, and building HTML and XML reports. The reports will be stored in the tests/_output/
directory.
Learn more about available reports.
To receive detailed output, tests can be executed with the --debug
option.
Learn more about debugging.
There are plenty of useful Codeception commands:
generate:cest
suite filename - Generates a sample Cest testgenerate:test
suite filename - Generates a sample PHPUnit Test with Codeception hooksgenerate:feature
suite filename - Generates Gherkin feature filegenerate:suite
suite actor - Generates a new suite with the given Actor class namegenerate:scenarios
suite - Generates text files containing scenarios from testsgenerate:helper
filename - Generates a sample Helper Filegenerate:pageobject
suite filename - Generates a sample Page objectgenerate:stepobject
suite filename - Generates a sample Step objectgenerate:environment
env - Generates a sample Environment configurationgenerate:groupobject
group - Generates a sample Group ExtensionWe have taken a look into Codeception’s 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.