All posts in Development

How to Integrate Codeception’s c3.php into Laravel4 – Step by Step

(Before we start here, if you’d like to see this, and other technical posts like this, done on video in the future, please say so in the comments below. Thanks!)

After banging my head against a wall trying to integrate Codeception’s c3.php into Laravel for code coverage, I realized it wasn’t as hard as I had thought. ┬áRegardless, here’s my post on how I did it.


You’ll need xdebug installed with remote_enable option turned on.

Here we go

I was doing functional tests, so here’s an example functional.suite.yml with the coverage element added. You’ll want to add that to any/all your test .yml files.

# Codeception Test Suite Configuration

# suite for functional (integration) tests.
# emulate web requests and make application process them.
# Include one of framework modules (Symfony2, Yii2, Laravel4) to use it.

class_name: FunctionalTester
    enabled: [Filesystem, FunctionalHelper, Laravel4, Db]
            cleanup: true
        remote: true

And then you add the coverage element with attributes as you need to configure into codeception.yml, which should also be in the root of your project.

Here’s mine:

actor: Tester
    tests: tests
    log: tests/_output
    data: tests/_data
    helpers: tests/_support
    bootstrap: _bootstrap.php
    colors: true
    memory_limit: 1024M
    enabled: true
    remote: true
        - app/*
        - app/config/*
        - app/database/*
        - app/lang/*
        - app/storage/*
        - app/tests/*
            dsn: 'mysql:host=localhost;dbname=eg_testing'
            user: 'homestead'
            password: 'secret'
            dump: tests/_data/dump.sql

Put the c3.php file in the root of your project (not the app directory). Then, make sure you require it in your /public/index.php like this:

 * Adding c3.php for code coverage during codeception tests
 * ref:

require __DIR__.'/../c3.php';

If you ran a test right now, it’s supposed to create a directory called c3tmp, but sometimes it doesn’t do it. So, I created my own c3tmp directory inside the root of the project and then did chmod 0777 c3tmp to make 100% certain there weren’t permissions problems writing to it.

Run a test using the --coverage flag. I ran codecept run functional --coverage.

Voila. You should see something like this screenshot.

Codeception c3 code coverage

If you want a pretty HTML report… because I like pretty HTML things… You can instead run codecept run functional --coverage-html.

Then go to your /tests/_output/coverage folder and open index.html in a browser.

Ta da!

Codeception HTML code coverage in Laravel

Time to stop writing this post and start writing more tests for better coverage.