arrow_back
Back

Codeception: acceptance, functional, and API tests in PHP

Andrew Dorokhov Andrew Dorokhov schedule 1 min read
menu_book Table of Contents

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 test prefix are tests
  • _before method is executed before each test (like setUp in PHPUnit)
  • _after method is executed after each test (like tearDown in 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
code

Need Help with Development?

Happy to help — reach out via the contacts or go straight to my Upwork profile.

work View Upwork Profile arrow_forward