What is codeception, how to install it, and how to use it?

Author
By Darío Rivera
Posted On in Codeception

In most cases, tests do not ensure that your application works 100% as it should. It is impossible to predict all possible scenarios, especially when dealing with complex applications. However, tests can cover the most important and sensitive parts of an application and at least test that it works as predicted.

Codeception is a testing framework that differs from others in the levels it uses to test your application (in addition to relying on PHPUnit and Behat). In Codeception, acceptance, functional, and unit tests can be created.

Types of Tests

Let's see the paradigms that Codeception uses for the tests.

Acceptance Tests

Acceptance tests test how an end user, customer, or non-technical person uses an application and expects it to behave in a certain way. This person has no idea about the framework, database, web server, or programming language that was used to create the application, or why it does not work as expected.

These tests comprise complex scenarios from the user's perspective, starting a session through the browser, clicking a button, filling out a form, etc. Example:

$I = new AcceptanceTester($scenario);
$I->amOnPage('/');
$I->click('Sign Up');
$I->submitForm('#signup', ['username' => 'MilesDavis', 'email' => '[email protected]']);
$I->see('Thank you for Signing Up!');

Functional Tests

These tests are performed without the need to run a server. Accordingly, we have the possibility to directly see the errors or exceptions of the application, the tests run fast, we can view data in the database, etc.

With functional tests, you can evaluate the data that an HTML response returns, the data that is saved in the database, whether an email was sent or not, etc. Example:

$I = new FunctionalTester($scenario);
$I->amOnPage('/');
$I->click('Sign Up');
$I->submitForm('#signup', ['username' => 'MilesDavis', 'email' => '[email protected]']);
$I->see('Thank you for Signing Up!');
$I->seeEmailSent('[email protected]', 'Thank you for registration');
$I->seeInDatabase('users', ['email' => '[email protected]']);

Unit Tests

Unit tests test isolated pieces of code. Some parts of the code cannot always be fully covered with the other two types of tests seen. If you can do unit tests, you test part of your code that is loosely coupled and testable.

function testSavingUser()
{
    $user = new User();
    $user->setName('Miles');
    $user->setSurname('Davis');
    $user->save();
    $this->assertEquals('Miles Davis', $user->getFullName());
    $this->unitTester->seeInDatabase('users', ['name' => 'Miles', 'surname' => 'Davis']);
}

Installation

composer require --dev codeception/codeception
php vendor/bin/codecept bootstrap
vendor/bin/codecept generate:cept acceptance HomePage

In Laravel, we can run the following command to start a PHP server.

Start a PHP server

user$ php -S localhost:8888
[Thu May 26 16:24:31 2022] PHP 7.4.29 Development Server (http://localhost:8888) started

Start a Laravel server

user$ php artisan serve
Starting Laravel development server: http://127.0.0.1:8000

Creating the First Test

You must configure the URL where your server is running in the acceptance.suite.yml file.

$I = new AcceptanceTester($scenario);
$I->wantTo('verify that the homepage is working on');
$I->amOnPage('/');
$I->see('My Blog');

You must configure the URL where your server is running in the acceptance.suite.yml file.

actor: AcceptanceTester
modules:
    enabled:
        - PhpBrowser:
            url: http://127.0.0.1:8000

Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.