Laravel 5 with Codeception

In this tutorial, I will show you how to replace default Laravel 5 testing suites with Codeception only.



Introduction

Continuing on my post from 2014: Getting started with Codeception. In this tutorial, I will show you how to install Codeception to work with Laravel 5 and remove the default PHPUnit testing suite.

Basically what we want to do is to replace the default PHPUnit testing suite with Codeception. I have done this for this website (Laravelista) because I find Codeception to be very easy to use and write tests.

Let's begin.

This is the current state of the repository at this moment a1f918027ae3a50f82fbcf68735bb68840cbefff.

Install Codeception

We will install Codeception as a development dependency using Composer from the command line:

composer require codeception/codeception --dev

Output:

$ composer require codeception/codeception --dev
Using version ^2.3 for codeception/codeception
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing symfony/dom-crawler (v3.3.0)
    Downloading: 100%

  - Installing symfony/browser-kit (v3.3.0)
    Downloading: 100%

  - Installing stecman/symfony-console-completion (0.7.0)
    Downloading: 100%

  - Installing psr/http-message (1.0.1)
    Downloading: 100%

  - Installing guzzlehttp/psr7 (1.4.2)
    Loading from cache

  - Installing guzzlehttp/promises (v1.3.1)
    Loading from cache

  - Installing guzzlehttp/guzzle (6.2.3)
    Loading from cache

  - Installing facebook/webdriver (1.4.1)
    Downloading: 100%

  - Installing behat/gherkin (v4.4.5)
    Loading from cache

  - Installing codeception/codeception (2.3.3)
    Downloading: 100%

This is the current state of the repository at this moment 4aa82f5956bd6fd7428c0ad3b8443ba53cb17839.

Default testing suite cleanup

First, we will remove the "phpunit/phpunit": "~5.7" dependency from composer.json. Then run composer update to reflect the change.

Now from the application root delete the file phpunit.xml, and delete the directory called tests.

This is the current state of the repository at this moment f3adbd705f06bc505b7381634f6ad936266272bc.

Bootstrapping Codeception

To bootstrap (finish installing/integrate) Codeception we need to execute this command:

vendor/bin/codecept bootstrap

Output:

$ vendor/bin/codecept bootstrap
 Bootstrapping Codeception

File codeception.yml created       <- global configuration
> Unit helper has been created in tests/_support\Helper
> UnitTester actor has been created in tests/_support
> Actions have been loaded
tests/unit created                 <- unit tests
tests/unit.suite.yml written       <- unit tests suite configuration
> Functional helper has been created in tests/_support\Helper
> FunctionalTester actor has been created in tests/_support
> Actions have been loaded
tests/functional created           <- functional tests
tests/functional.suite.yml written <- functional tests suite configuration
> Acceptance helper has been created in tests/_support\Helper
> AcceptanceTester actor has been created in tests/_support
> Actions have been loaded
tests/acceptance created           <- acceptance tests
tests/acceptance.suite.yml written <- acceptance tests suite configuration
 ---

 Codeception is installed for acceptance, functional, and unit testing

As you can see Codeception is installed for acceptance, functional, and unit testing. Also, the tests directory is recreated with the Codeception folder structure. You can change the structure if needed, but I find this to be the best option.

This is the current state of the repository at this moment 3b10d8383fc7fc69c9e305a16bc2ea28647ace76.

Installing Laravel5 module

For this tutorial, I will write a functional test that simply checks if the welcome page returns 200 HTTP code and if there is text on page Laravel.

To better integrate Laravel with Codeception there is a module called Laravel5. It enables you to use methods like:

  • amLoggedAs
  • amOnPage
  • amOnRoute
  • click
  • dontSee
  • fillField
  • have
  • see
  • seeCurrentRouteIs
  • seeInSession
  • seeLink
  • seePageNotFound
  • seeRecord

And many other useful helper methods.

Other than being able to use helpful methods, it comes with a variety of configuration options that you can tweak:

  • cleanup
  • run_database_migrations
  • database_migrations_path
  • run_database_seeder
  • database_seeder_class
  • environment_file
  • bootstrap
  • root
  • packages
  • vendor_dir
  • disable_exception_handling
  • disable_middleware
  • disable_events
  • disable_model_events
  • url

I have marked two options that I use the most.

This module is to be used for functional testing only. That means that we need to enable it in tests/functional.suite.yml. Make that file look like this:

actor: FunctionalTester
modules:
    enabled:
        - Laravel5
        - \Helper\Functional

Now run from the command line:

vendor/bin/codecept build

Awesome! Installation complete.

Optional: If you want to change the configuration options for the module you can do that like so:

actor: FunctionalTester
modules:
    enabled:
        - Laravel5:
            environment_file: .env.testing
            run_database_migrations: true
        - \Helper\Functional

This is the current state of the repository at this moment aaecc75e463ac1e8273a467878cd491bc5d979f6.

Your first functional Codeception test

To create a new functional test using the cept format, run this command:

vendor/bin/codecept generate:cept functional WelcomePage

Output:

$ vendor/bin/codecept generate:cept functional WelcomePage
Test was created in C:\repos\laravel-codeception\tests\functional\WelcomePageCept.php

You can find the test in \tests\functional\WelcomePageCept.php. Now modify the file so that it looks like this:

<?php
$I = new FunctionalTester($scenario);
$I->wantTo('view the welcome page');

$I->amOnPage('/');
$I->seeResponseCodeIs(200);
$I->see('Laravel');

We navigate to /, check if the response code is 200, and verify that we see text Laravel on the page.

Before we run the test there is one more thing to modify. Because our sample application does not use a database we need to modify our database values in the .env file:

DB_CONNECTION=sqlite
DB_DATABASE=:memory:

Remove all other keys starting with DB_.

Now, to run the test execute:

vendor/bin/codecept run functional

Output:

$ vendor/bin/codecept run functional
Codeception PHP Testing Framework v2.3.3
Powered by PHPUnit 6.2.1 by Sebastian Bergmann and contributors.

Functional Tests (1) ---------------------------------------------------------------------------------------------------
+ WelcomePageCept: View the welcome page (0.04s)
------------------------------------------------------------------------------------------------------------------------

Time: 514 ms, Memory: 16.00MB

OK (1 test, 2 assertions)

Success!

This is the current state of the repository at this moment 1c14715f10b506a46c076677eaf4069cbedd8d13.

You are now ready to start writing tests for your Laravel 5 application.

Comments