Installation
composer require --dev codeception/codeception
For using any kind of asserts in our tests we need to install the corresponding module:
composer require --dev codeception/module-asserts
Verify is a very tiny wrapper for PHPUnit assertions, that are aimed to make tests a bit more readable.
With BDD assertions influenced by Chai, Jasmine, and RSpec your assertions would be a bit closer to natural language.
composer require --dev codeception/verify
Module to use Codeception with Yii2 Framework:
composer require --dev codeception/module-yii2
Writing a test
File tests/unit/models/MyTest.php:
<?php
namespace tests\unit\models;
use Codeception\Test\Unit;
class MyTest extends Unit
{
protected function _before()
{
// Before tests
}
protected function _after()
{
// After tests
}
public function testSomething()
{
verify([1, 2, 3, 4])->equals([1, 2, 3]);
}
}
Inside a class:
- all public methods with
testprefix are tests _beforemethod is executed before each test (likesetUpin PHPUnit)_aftermethod is executed after each test (liketearDownin PHPUnit)
Checks
// $variable
verify($variable)->true();
verify($variable)->false();
verify($variable)->null();
verify($variable)->notNull();
verify($variable)->empty();
verify($variable)->notEmpty();
Run tests
php vendor/bin/codecept run
Andrew Dorokhov