Introduction to codeception(Acceptance,Functional,Unit) tests.

In this article we are going to learn about Codeception Tests. What is Codeception test? How it works?

What is Codeception?
Codeception is full test framework for TDD and BDD styled test cases. That means we can write both traditional Unit Test formatted test as well as Behavior Driven Development formatted tests.
Codeception is build on PHP. So, we need to have PHP running in the PC as well as in the path. You may read these posts to install environments which is needed for codeception. 
Codeception contains mainly 3 suits.
1. Acceptance Test suite
2. Functional Test Suite
3. Unit Test Suite
We can add API test suits manually. So, codeception is complete testing framework. It has supports for different modules.. You can get detail module description from this link.
For different type of tests, codeception has different configurations you can get full configuration file lists from here.
How It works?
Codeception is build on
a. PHPUnit
b. Yii
c. Mink
d. Bahat
and several other modules as utilities..
Codeception (the BDD style) works like as user prospective Guy Format. Guy format refers to BDD style code. In Codeception there are mainly 3 type of Guy. WebGuy, TestGuy and CodeGuy. We can have another guy, named APIGuy. When we create a test case, we create a new guy object(of the guy class). The webguy responsible for acceptance test, testGuy for functional tests , codeGuy for unit tests and the apiGuy for API tests. Example : for webguy (I have used a free site from another blog and hosted locally, the site has a text box and a button, what ever we insert in the text box, when we click the button the site converts the input text into upper case letter. )
   1:  <?php
   2:  $I= new WebGuy($scenario);
   3:  $I->wantTo("TestMyFirstApp");
   4:  $I->amOnPage('toupper.html');
   5:  $I->see('Convert Me!');
   6:  $I->fillField('string','ShantonuSarker');
   7:  $I->click('Convert');
   8:  $I->amOnPage('toupper.php');
   9:  $I->see('To Upper!');
  10:  #$I->see('String converted: SHANTONUSARKER');
  11:  $I->click('Back to form');
  12:  $I->see('Convert Me!');
  13:  $I->fillField('string','');
  14:  $I->click('Convert');
  15:  $I->amOnPage('toupper.php');
  16:  $I->see('To Upper!');
  17:  $I->see('No string entered');
  18:  $I->reloadPage(); 

When we write tests in CEPT format(BDD Style), It actually compiled two times before execution. That means, first it will compiled with depended modules and again compiled during execution. So, if we write our custom functions in test case files, it will be run twice, so, it is not proper to write custom functions in the test case. Codeception has separate way to write custom module. Like as other module, we have to write our custom module and include in configuration file to enable that. Then if we build, it will accessible from our Test Files with Guy Class. 

There is another format named as CEST format which is actually PHPUnit format(where we have before and after function).


It is woks like PHPUnit. See my other posts describing CEST & CEPT format test cases.

We can run codeception acceptance tests in JS free PhpBrowser or with Selenium Server(any browser supported with selenium server). For functional test, phpbrowser is good. And for other test, we might not need any browser(as they are code level tests)

There are some basic rules for using codeception. Usually codeception tests are run along with main code base of the project that we need to work on.  This is because of dependency. That means, if codeception tests have dependency on the modules used in main code base, we have to use the tests with code. If not, we can have separate codebase. Usually, functional tests , unit tests and api tests are depend on main code base. So, when we are writing those we need to have code base . And we run the test in same server where our code in running. But , acceptance tests suite can be run independently even with a real browser. That means, if we do not have codebase of main project, we can still make test cases for that site and run. So, we can say like that
Acceptance Suites = Can run the test Externally(can be run from outside of the codebase)
Unit/Functional Suites = Should be run internally (with source code)

You may see codeception supporting modules where we can write tests from this post. We can write test for those modules.

Note ; Now a days, selenium IDE has a plug-in for Codeception Formatter which recode and convert tests in codeception format.