Codeception

because tests can have frameworks too

Luka Mužinić

@lmuzinic

My first experience with testing

Testing 101

Why use a testing framework

You probably use framework when coding (unless you are Rasmus :))
so it is only logical to use it while writing tests

How to test a testing framework?

What is Codeception?

PHPUnit on steroids

Readable tests, BDD style

Describes actions from user's perspective

Integrates with most frameworks

Written in PHP on top of proven packages

Codeception 1.8


"require": {
    "php": ">=5.3.19",
    "behat/mink": "1.5.*",
    "phpunit/phpunit": "3.7.*",
    "facebook/webdriver": "0.3",
    "behat/mink-goutte-driver": "1.0.*",
    "behat/mink-selenium2-driver": "1.1.*",
    "monolog/monolog": "*",
    "symfony/finder": "~2.3",
    "symfony/console": "~2.3",
    "symfony/event-dispatcher": "~2.3",
    "symfony/yaml": "~2.3"
}

Quick start

~ wget http://codeception.com/codecept.phar -O /usr/local/bin/codeception
~ codeception bootstrap

Unit tests in action

~ codeception generate:phpunit unit XitiTranslate
~ codeception generate:test unit XitiTranslate

Unit tests in action

class XitiTranslateTest extends \Codeception\TestCase\Test {
    protected $codeGuy;
    protected $xitiTranslate = null;

    protected function _before() {
        $this->xitiTranslate = new XitiTranslate($data);
    }

    protected function _after() {
        unset($this->xitiTranslate);
    }

    public function testXitiTranslations() {
        $this->assertEquals('Hofer KG', $this->xitiTranslate->companyName());
        $this->assertEquals(2, $this->xitiTranslate->companyId());
        // ...

Unit tests in action

don't need to install PHPUnit

use modules to add integration testing

Functional/API tests in action

~ codeception generate:cept functional MobileApiIndustries

Functional/API tests in action

<?php
    $I = new TestGuy($scenario);
    $I->wantTo('test MobileApp API to return all necessary fields for industries');
    $I->haveHttpHeader('Fetch-Type', 'full');
    $I->haveHttpHeader('Api-Version', '2');

    $I->sendGET('/industries');

    $I->seeResponseCodeIs(200);
    $I->seeResponseIsJson();
    $I->seeResponseContains('{industries:[...]}');

Functional/API tests in action

with framework integration you don't need a web server

simulate request and response

Acceptance tests in action

~ codeception generate:cept acceptance HeaderNavigationCategories

Acceptance tests in action

<?php
    $I = new WebGuy($scenario);
    $I->wantTo('see that categories are in navigation');

    $I->amOnPage('/');
    $I->click("Branchen & Themen", "nav.main_nav_entities");
    $I->click("Alle Kategorien");
    $I->see("Bauen & Garten",".nav_panel .drop");
    $I->see("Sport & Fitness",".nav_panel .drop");

Acceptance tests in action

test any site, even live ones

uses browser, indifferent to framework used

describe testing actions in PHP

Run all the tests!

~ codeception run

Modules

AMQP, Db, Memcache, Cli, Filesystem

Symfony, Zend, Yii, Laravel, Kohana

Cli

$I->runShellCommand($command)
$I->seeInShellOutput($text)

Db

modules:
    enabled: [Db]
        config:
            Db:
                dsn: 'mysql:host=localhost;dbname=testdb'
                user: 'root'
                password: ''
                dump: 'tests/_data/dump.sql'
                populate: true
                cleanup: false
$I->seeInDatabase('users', array('username' => 'phpday'));
SELECT COUNT(*) FROM users WHERE username = 'phpday'; 

FileSystem

$I->seeFileFound('process.lock','app/lock');
$I->seeInThisFile(PID);

MemCache

$I->grabValueFromMemcached('key');

Codecption 2.0 is near

beta

RC

RC2

Changes in 2.0

Alternatives

Testify
FUnit

What now?

give Codeception a try

use it together with your clients

great way for a project newcomers

include testing culture in your organization

Against testing?!

You are loosing arguments every day thanks to Codeception!

Thx!

https://joind.in/11301