Introduction
At CitrusLeaf, we always believe that testing is as important as development in a software’s lifecycle. That’s why we start every project by writing test-cases before we start its development. As soon each module gets completed, we start writing it’s unit test cases using CodeCeption. More recently, we upped our game by writing Acceptance Test Cases using CodeCeption. Acceptance or user acceptance testing (UAT) is a type of testing which ensures that the application requirements meet business needs. Although a time-consuming process, it increases the overall quality of the software and makes sure that it’s behaving like it should in real-world.
Meet CodeCeption
It is a simple, efficient yet powerful testing framework which can perform API, Unit, Functional and Acceptance testing. Based on the popular unit testing tool PHPUnit, it enables user-centric testing and Behavior Driven Development. Codeception collects and shares best practices and solutions for testing PHP web applications. With a flexible set of included modules, tests are easy to write, easy to use and easy to maintain. Codeception encourages developers and QA engineers to concentrate on testing and not on building the test suite.
Requirements
- PHP 5.4+
- cURL with it’s PHP extension
- Composer for dependency management
Additional Requisite
Installation
You can install CodeCeption either by using Git, Composer or by installing the PHAR globally. Following are the installation steps using Git:
Composer installation-
- Run the following command in a new directory, after installing Composer (globally)-
$composer require codeception/codeception --dev - Initialize the testing environment-
$php vendor/bin/codecept bootstrap
or if installed globally by composer
$codecept bootstrap
This command will generate the configuration file ‘codeception.yml’ and default test-suits (acceptance, unit and functional test-cases folder).
Commands
Before starting with CodeCeption, let’s have a quick glance at some of the useful commands-
- $codecept run – Runs all the tests which include acceptance, unit and functional test-cases.
- $codecept run acceptance –steps – Runs all acceptance tests and print step by step execution.
- $codecept run acceptance MyCept – Runs only MyCept Acceptance test
- $codecept run tests/acceptance/MyCest.php:^logint$ –debug – This will run only ‘login’ method of ‘MyCest’ class.
- $codecept generate:cept suite Login – Generates acceptance test class for Login test case
Further, CodeCeption’s test methods are divided into 4 groups:
- Interaction with page: fillField(), selectOption(), submitForm(), click() etc.
- Assertions. see(), seeElement(), seeInCurrentUrl(), seeCheckboxIsChecked() etc. You can add a suffix to all these methods and use it when you need it.
- Cookie methods: dontSeeCookie().
- Comment and description of test scenarios: wantTo()
Writing acceptance test
- First, let’s start the selenium server with Chrome Drive with the command-
$java -Dwebdriver.chrome.driver=.//chromedriver -jar./selenium-server-standalone-3.14.0.jar
- Open your favourite text editor and open the project’s directory.
- Edit the acceptance.suite.yml file and set the URL, and browser details. For example-
actor: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://www.example.com/
- \Helper\Acceptance
- Below is a simple Login Acceptance Test in PHP:
Class LoginCest {
public function login(AcceptanceTester $I) {
$I->amOnPage('/login');
$I->fillField('username', 'User');
$I->fillField('password', 'qwerty');
$I->click('LOGIN');
$I->see('Welcome, User!');
}
}
- Running the test:
$codecept run acceptance LoginCest
As soon as you run the test, you’ll see the browser automatically opening http://example.com/login, filling out the form, confirming that it sees “Welcome, User!” and exits. If there’s any failure in this assertion, the test will fail and the terminal will show the appropriate line number to fix.
All in all, we’re are enjoying CodeCeption at CitrusLeaf. CodeCeption has made it really easy to write better software by taking up the major hard work in generating test suits.
This marks the end of part 1 of the Codeception series of articles. More blog posts are coming up. Stay tuned!