Codeception LogoCodeception

Modern PHP testing for everyone.

Quick Start

Write and execute a test for an existing app
in less then 5 minutes!

Acceptance Testing

Selenium WebDriver, PhpBrowser. Data cleanup. Continuous Integration. Remote CodeCoverage.

Functional Testing

Symfony2, Laravel, Yii, Phalcon, Zend Framework, Kohana, Databases, REST, SOAP, CodeCoverage.

API Testing

REST, SOAP, XML-RPC via PHPBrowser or PHP Frameworks.

And Unit Testing powered by PHPUnit.

In Action

Features

  • Selenium WebDriver integration
  • Elements matched by name, CSS, XPath
  • Symfony2, Laravel4, Yii, Phalcon, Zend Framework integration
  • PageObjects and StepObjects included
  • BDD-style readable tests
  • Powered by PHPUnit
  • API testing: REST,SOAP,XML-RPC
  • Facebook API testing
  • Data Cleanup
  • HTML, XML, TAP, JSON reports
  • CodeCoverage and Remote CodeCoverage
  • Parallel Execution

Questions you wanted to ask


Do I really need this?

If you develop complex web applications with forms, then you need to use a testing framework. A testing framework allows you to test all links, forms, pages, APIs, and anything else in just minutes using automated test scripts.


Why should I use this instead of PHPUnit?

PHPUnit has very limited features for functional testing with Selenium or other backends. Codeception is PHPUnit on steroids. Everything you need for testing is built-in, and 'just works' out of the box. No more pain in configuring Selenium, data cleanup, writing XPaths, or fixtures.


It looks just like Behat

Unlike Behat, Codeception tests are written in PHP. They are flexible and easy to write. You can use variables and operators, CSS and XPath locators in your tests. These features allow you to build a solid test automation platform for testing your web application. Codeception tests are simple and readable for your developers, managers, and QA team.


We are planning to use Selenium IDE. Why Codeception?

Codeception works great with Selenium. But with Codeception you can write your tests in PHP. The main reason is: Selenium IDE tests are tightly bound to XPath locators. If you ever change anything in the layout, tests will fail. Codeception locators are more stable. You can use names, labels, button names and CSS to match elements on page. It's much easier to support the Codeception test then Selenium's. Codeception can clean data between tests, can check database values, and can generate code coverage reports.


Is Codeception a tool for testing legacy projects?

Sure, you can use Codeception for black-box testing of your legacy applications. But in the same manner you can start testing modern web application as well. With modules that integrates with all popular PHP frameworks you can start writing functional tests in seconds. Unit tests? Write them as you do in PHPUnit with some enhancement. Codeception keeps your tests in one place, makes them structured and readable.

Blog

Examples...

Acceptance Test

PhpBrowser Db

<?php
  $I = new AcceptanceTester($scenario);
  $I->wantTo('create wiki page');
  $I->amOnPage('/');
  $I->click('Pages');
  $I->click('New');
  $I->see('New Page');
  $I->fillField('title', 'Hobbit');
  $I->fillField('body', 'By Peter Jackson');
  $I->click('Save');
  $I->see('page created'); // notice generated
  $I->see('Hobbit','h1'); // head of page of is our title
  $I->seeInCurrentUrl('pages/hobbit');
  $I->seeInDatabase('pages', array('title' => 'Hobbit'));
?>

This test can be executed inside Mozilla Firefox using Selenium Webdriver, or any browser. You can also execute this test without any browser by using a PHP browser emulator based on CURL.

Read more about Acceptance Tests →

This test can be also executed as a functional test in one of the popular PHP frameworks. Codeception has modules that support Symfony2, Zend Framework and Kohana.

Read more about Functional Tests →


API Test

PhpBrowser REST

<?php
  $I = new ApiTester($scenario);
  $I->wantTo('create a new user by API');
  $I->amHttpAuthenticated('davert','123456');
  $I->haveHttpHeader('Content-Type','application/x-www-form-urlencoded');
  $I->sendPOST('/users', array('name' => 'davert' ));
  $I->seeResponseCodeIs(200);
  $I->seeResponseIsJson();
  $I->seeResponseContainsJson(array('result' => 'ok'));
?>

This test can be executed inside PhpBrowser or as a functional test of one of the most popular PHP frameworks.

Read more about API Tests →


Unit Test

Unit Db

<?php
  use \Codeception\Util\Stub;
  class UserTest extends \Codeception\TestCase\Test
  {
    public function testUserSave() {
      $user = Stub::make('User');
      $user->setName('davert');
      $this->assertEquals('davert', $user->getName());
      $user->save();
      $this->tester->seeInDatabase('users', array('name' => 'davert'));
    }
  }
?>

PHPUnit test format with some useful helpers. In this test we check that value was saved to database with one simple command 'seeInDatabase'.

Read more about Unit Tests →

AspectMock

A side-project of Codeception for unit testing any PHP code. AspectMock most powerful mocking framework that can stub and mock practically anything. Even static methods, can be tested now. Use it for legacy or untestable code.

<?php;
  // mocking static method calls
  $this->assertEquals('users', User::tableName());
  test::double('User', ['tableName' => 'fake_users']);
  $this->assertEquals('fake_users', User::tableName());
  // mocking class method calls
  test::double('User', ['getEmail' => 'davert@mail.ua']);
  $user = new User;
  $this->assertEquals('davert@mail.ua', $user->getEmail());
?>
Install AspectMock →