How to enable/disable modules in Codeception?

 In this article, we are going to see how to enable different modules in codeception.

As we know codeception has different modules like selenium, selenium2, webdriver, PHP browser, facebook, Symfony1 etc, so when we need those, we have to activate.
Then, why we need those?. Actually these modules increases the functionality inside of Guy classes of codeception. That means, more type of function when we need, we need to active associate modules. Some modules are socially used of integration, running tests, API tests etc.

Example : if we need to apply Wait in out test case, we can not use PhpBrowser module, we have to use webdriver/selenium2/selenium which contains wait(along with different conditional wait)
Enabling a Module :
Step 1 : Include the module in configuration YML file :  As we know we have
acceptance.suite.yml , for acceptance test configuration
functional.suite.yml , for functional test configuration and
unit.suite.yml for unit test configuration.
In every YML, we will see module enable area where we can enable module by writing the module name (in Image , it is for acceptance test suit )
image

We have to remind that we need to provide configuration for specific module also. Like if we are activating selenium
enabled:
        - Selenium2
        - WebHelper
    config:
        Selenium2:
            url: 'http://google.com
            browser: firefox
            host : localhost
            port : 4444
            delay : 2000

   
Here is the full list of available modules.

image

But Before enabling module, you should read why you need the modules. Some modules are needed for better way of test execution, some are for framework specific testing and integration,  and some are for different type of testing.

Step 2 : Build the full test suit : From Command Line
php codecept.phar build

or, if you are using composer
<path to codecept>/codecept build

Disabling :
And, for deactivation , just comment it out of the module name and build the test suit again.(it is better you delete the name, but commenting is faster way) . Example : in here PhpBrowser module will be disabled
enabled:
        #- PhpBrowser
        - Selenium2
        - WebHelper



Example for enabling modules for acceptance testing (in acceptance.suite.yml  file)
1. Enabling Selenium2 module
enabled:
        - Selenium2
        - WebHelper
    config:
        Selenium2:
            url: 'http://localhost/myapp/'
            browser: firefox
            host : localhost
            port : 4444
            delay : 2000

2. Enabling PHPBrowser module
enabled:
        - PhpBrowser
        - WebHelper
    config:
        PhpBrowser:
            url: 'http://localhost/myapp/'
3. Enabling Custom module
enabled:
- YourModuleName
- WebHelper
config:
YourModuleName:
url: 'http://localhost/myapp/'

<or any extra parameter that you want>

4. Enabling Webdriver module
enabled:
    - WebDriver
   config:
      WebDriver:
         url: 'http://localhost/'
         browser: firefox
         wait: 10
         capabilities:
             unexpectedAlertBehaviour: 'accept'

If we want to active Phalcon1 module for functional test suit, it will be like this in functional.suite.yml
enabled:
    - FileSystem
    - TestHelper
    - Phalcon1
    config:
        Phalcon1
            bootstrap: 'app/config/bootstrap.php'
            cleanup: true
            savepoints: true

And , after editing the any YML file , you must build . If you do not build , it will effect on execution only but you will not get benefit for writing test case with extra functionality
Note : Among webdriver, selenium, selenium2, webdriver is latest and having most options to work with. I mainly work with webdriver module for acceptance testing.
Thanks …:)

How to create PHPUnit Test Case in Codeception?

In this article we are going to see very simple command to generate PHPUnit test case in CodeCeption.
As we know, codeception build on PHP Unit, so it support all type of PHP Unit test case. That means, the test case will contain setup and teardown method for test initialization and closure functions.
And, as it is a PHP Unit format, so there will not be any Guy initialization. Just raw PHP Unit format test steps can be executed. Yes, we can use Guy Class, but should not be included as we have different format for those Guy class based test cases.
Beside, those Guy Class based test cases needs two time compilation, but PHP Unti test case need only single time compilation.
Like as other format, PHPUnit type test case In needs to use Test as post fix of a test case which is understood by codeception. That means, if our test case name is SugnUp, so if we are writing PHPUnit type test case, then it will be named as SignUpTest.php.
What is PHP Unit Format :
Like as other unit test case, PHP Unit will also have setup/tear down. Here is sample PHP Unit test case structure :
   1:  <?php
   2:  class SignUpTest extends \PHPUnit_Framework_TestCase
   3:  {
   4:      protected function setUp()
   5:      {
   6:          // Initialization methods
   7:      }
   8:      protected function tearDown()
   9:      {
  10:          //Closure activities/ methods
  11:      }
  12:      // tests
  13:      public function testMe()
  14:      {
  15:      }
  16:  }


So, now lets see the command. When we need to generate a PHP format format test case:

php codecept.phar generate:phpunit <suitename> <testname>
[if you use composer, then : <path to codecept>/codecept generate:phpunit <suitename> <testname> ]

For example, if we want to generate an Acceptance test case named as SignUp in phpunit format , then
php codecept.phar generate:phpunit acceptance SignUp

image

We can see a file generated SignUpTest.php in the tests/acceptance/ folder and it will follow the configuration of acceptance.suite.yml

So, we can generate phpunit format test case and we can ready to write codes.

Note : Usually, we create PHP Unit test cases for Unit Testing, not acceptance testing. And, php unit test is more familiar to developer rather than traditional tester or BDD testers.

Thanks…:)







What is Cest Test Case format in Codeception? How to create Cest Test Case?

In this article we are going to see How can we create Cest test case in codeception?

As we know , codeception has 3 type of tests, we have to define what type of test we are going to build.

What is Cest format ?
This actually traditional PHPUnit/Junit format. It is better for developer or unit testers to read. Even, personally I like this type of test case as I am habituated in writing Unit Tests cases. We can group test steps, having a private function as common test steps. This is helpful when you have long test steps.

When we need to create PHPUnit type test case In codeception, we need to use Cest as post fix of a test which is understand by codeception command. That means, if our test case name is SugnUp, so if we are writing BDD type test case, then it will be named as SignUpCest.php.

Cest Format :
This type of test cases takes the Guy ( either WebGuy/ TestGuy/ TestGuy) class as parameter inside of a test method.

Like :
   1:  <?php
   2:  use \WebGuy; 
   3:  class SignUpCest
   4:  {
   5:      public function _before()
   6:      {
   7:          //similar to setup/initial of phpunit or Junit 
   8:      }
   9:      public function _after()
  10:      {
  11:          //similar to teardown/closure of phpunit/junit 
  12:      }
  13:      // tests
  14:      public function tryToTest(WebGuy $I) {
  15:          $I->wantTo("Test Registration Page");
  16:          $I->amOnPage('/');
  17:      }
  18:  } 



So, now lets move on to command. When we need to generate a Cest format Acceptance test case(acceptance is a type of test suit) :
php codecept.phar generate:cest <suitename> <testname>

[if you use composer, then : <path to codecept>/codecept generate:cest <suitename> <testname> ]

For example, if we want to generate an Acceptance test case named as SignUp in cest format , then

php codecept.phar generate:cest acceptance SignUp


image


We can see a file generated SignUpCest.php in the tests/acceptance/ folder and it will follow the configuration of acceptance.suite.yml
So, we can generate cest format test case and we can ready to write codes.

Note : It is more of developer friendly test case format, some time feels complex in reading but it eliminates repetitive test steps.

Thanks…:)







What is Cept format test case in Codeception? How to create Cept Test cases?

In this article we are going to see How can we create Cept test case in codeception?
As we know , codeception has 3 type of tests, we have to define what type of test we are going to build.
So, what is Cept format ?
In codeception, we use BDD type test steps to write test case. What is BDD type? BDD refers to Behavior Driven Development. BDD type test first introduced in Gherkin with Cucumber. But in here, we will write similar test cases so, we are referring that as BDD type test case.
In codeception, when we need to declare BDD type test case, we need to use Cept as post fix of a test which is understand by codeception command. That means, if our test case name is SugnUp, so if we are writing BDD type test case, then it will be named as SignUpCept.php.

Cept Format :
This type of test case start with Guy ( either WebGuy/ TestGuy/ TestGuy).
Like :
$I = new WebGuy($scenario);
$I->wantTo('travelling through site');
$I->amOnPage('/”);

So, now lets move on to command. When we need to generate a Cept format Acceptance test case(acceptance is a type of test suit) :

php codecept.phar generate:cept <suitename> <testname>

[if you use composer, then : <path to codecept>/codecept generate:cept <suitename> <testname> ]

For example, if we want to generate an Acceptance test case named as SignUp in cept format , then

php codecept.phar generate:cept acceptance SignUp 

image

Then,  a file will be create SignUpCept.php in the tests/acceptance/ folder and it will follow the configuration of acceptance.suite.yml

So, we can generate cept format test case and we can ready to write codes.

Thanks…:)