Running Codeception Acceptance Tests with Docker

Running Codeception Acceptance Tests with Docker

Simplify Your Testing Process: Running Codeception Acceptance Tests with Docker

In Lytvynov Production we love testing and we use Codeception a lot.

Codeception is a modern and powerful testing framework for PHP applications. It provides a flexible and intuitive syntax for writing functional, acceptance, and unit tests. Codeception can be used to test a wide range of applications, including web applications, APIs, and command-line applications.

One of the main advantages of Codeception is its modular architecture, which allows developers to easily extend and customize the framework to suit their specific testing needs. It also provides built-in support for popular testing frameworks such as PHPUnit, Selenium WebDriver, and REST API testing tools like Guzzle.

Codeception also provides a clear and comprehensive reporting system that makes it easy to identify and fix bugs and issues during the testing process. Overall, Codeception is a great choice for developers looking for a robust and flexible testing framework for PHP applications.

What is it acceptance tests?

Acceptance tests, also known as functional tests, are a type of software testing that evaluates the behavior of a software application from the user's perspective. Acceptance tests are typically performed to ensure that the application meets the requirements and expectations of its end-users.

Unlike unit tests, which focus on testing individual functions or methods in isolation, acceptance tests evaluate the entire application, including its user interface, user interactions, and data flows. Acceptance tests are typically performed on a fully functional version of the application, often referred to as a "build," that closely resembles the final product.

Acceptance tests are generally automated using testing frameworks such as Codeception, which provide a set of tools for simulating user interactions with the application and verifying that the application behaves as expected. This helps to ensure that the application is reliable, efficient, and user-friendly, and that it meets the needs of its intended audience.

Codeception simple acceptance tests
 

Let's say we want to login on our login page, so we need to creae a LoginCest.

We can do it manually or generate with a command 

php vendor/bin/codecept generate:cest acceptance Login

 

Here is the code

<?php

namespace App\Tests\acceptance;

use App\Tests\AcceptanceTester;

class LoginCest
{
    public function tryLogin(AcceptanceTester $I)
    {
        $email = 'example@gmail.com';
        $password = 'password-example';
        $I->amOnPage('/login');
        $I->fillField('_username', $email);
        $I->fillField('_password', $password);
        $I->click('Log in');
        $I->see('Dashboard');
    }
}

 

Codeception Acceptance tests with Php Browser

By default we have PhpBrowser, it allows us to run tests and that's enough if you need not see all effects related with front changes.

actor: AcceptanceTester
modules:
    enabled:
        - PhpBrowser:
            url: &url 'http://nginx_lp' #http://127.0.0.1:8080
        - \App\Tests\Helper\Acceptance

 

Codeception Acceptance tests with Web Driver

In case you would like to use more powerfull tull - Selenium - you need to install Web Driver.

PhpWebDriver is a PHP library that provides a simple and easy-to-use interface for automating web browsers using the WebDriver protocol. WebDriver is a protocol that defines a standardized way to interact with web browsers programmatically, allowing developers to automate web testing, scraping, and other browser-based tasks.

Using PhpWebDriver, developers can write PHP scripts that simulate user interactions with a web page, such as clicking buttons, filling out forms, and navigating between pages. PhpWebDriver supports a wide range of web browsers, including Chrome, Firefox, Safari, and Microsoft Edge.

To install

composer require --dev codeception/module-webdriver

 

And your config

actor: AcceptanceTester
modules:
    enabled:
        - WebDriver:
            url: &url 'http://nginx_lp' #http://127.0.0.1:8080
            host: chrome_lp #address to your selenium web driver, can be http://127.0.0.1:4444 if you use locally
            browser: chrome
        - \App\Tests\Helper\Acceptance

 

Now is the most tricky part - run selenium. 
You can run it locally and in this case the documentation is pretty good, jump to Local Seput (Selenium Server)

 

Or you can add selenium locally to your docker.

Selenium is an open-source testing framework that allows developers to automate web browsers and perform browser-based testing. It provides a suite of tools and libraries for interacting with web browsers in various programming languages, including Java, Python, and PHP. Selenium is widely used in the software development industry to ensure the quality and reliability of web applications. It is often used in conjunction with other testing tools, such as Codeception, to create comprehensive test suites that cover all aspects of an application's functionality.

docker-compose with selenium/standalone-chrome:latest example.

chrome_lp:
  container_name: chrome_lp
  image: selenium/standalone-chrome:latest
  hostname: chrome
  privileged: true
  shm_size: 2g
  networks:
    - lytvynov-production_lp
  depends_on:
    - php_lp
  ports:
    - "127.0.0.1:4444:4444"

The result you ll see in your terminal

 

In conclusion, if you are interested in learning more about our testing practices and how we can help ensure the success of your project, we invite you to visit our testing page at https://lytvynov-production.com/testing. There, you can find more information about our testing services, tools, and methodologies, as well as examples of our past projects and success stories.

 

Let’s start your project