Friday, June 14, 2013

Setup and Write Yii Test Cases

First you need to know below things to start running a test case.

Business Problem:
Generally developers develop a web application or standalone application then they give the build to testers to test it. So testers understand all features of the build and plan to test it but when the build is so big then testers feel uncomfortable to test large builds. These scenarios come more than once before the build is live. So eventually a developer or tester come to a point of automating the whole thing which brings us to the point of writing a test suite.
One can start a test suite by using third party components or write individual test cases specifically for their business.

Here we are interested only in specific test cases which are related to business so we talk about only those in further discussions.

Solution:
Yii framework provides a base platform where in developers can test their features by writing separate code which tests the real code.
While creating a new project yiic tool will also generate necessary folder structure where in we can put test related files. (I guess you people used that tool)
For more information Yii Testing

Steps involved
  1. Installation/prerequisites
  2. Setup project for testing
  3. Write Test Cases
  4. Run Test Cases

Setting up the environment:

My environment is something different when compared to others.
  • We have code in windows and work on code development in eclipse
  • We share the folder and then map the folder to a Linux share.
  • We then symlink the code folder so sites
  • Use Apache as web server to serve the requests.
On whole we develop code in windows using eclipse and use Linux to host the code. We use share folders to sync the changes from windows to Linux.

Installation


 For testing we don't require that complex setup


Get PHP runtime
Goto below url
http://php.net/downloads.php
Download 5.3.x version for compatibility reasons (I have that version)
My file looks something like this 'php-5.3.26-Win32-VC9-x86.zip'
Extract the archive.

Get PEAR package manager
http://pear.php.net/go-pear.phar
Download the phar file.
Copy the go-pear.phar file to 'C:\Users\<user>\Downloads\php-5.3.26-Win32-VC9-x86' directory
Now open command prompt (I'm using power shell for all operations) and execute go-pear.phar using php runtime
Note: From this point I consider shell as your command shell whether it is command prompt or power shell
.\php.exe .\go-pear.phar

 If it asks for 'local' or 'system' then go for system

To select all options press enter

To install registry keys execute
.\PEAR_ENV.reg

Install Selenium RC Server as documented in Yii
http://selenium.googlecode.com/files/selenium-server-standalone-2.33.0.jar

Well if version changes we can always look at here http://docs.seleniumhq.org/download/ to get latest ones

Note: A working java environment should be there for Selenium Server to run because its a jar file.



Now install PHPUnit
.\pear.bat channel-discover pear.phpunit.de
.\pear.bat install PHPUnit

Install SeleniumTest
.\pear.bat install phpunit/PHPUnit_Selenium

If you run into any errors like "No releases available for package "pear.phpunit.de/PHPUnit_Selenium"" then run below command
.\pear.bat clear-cache


Setup project for testing

Goto directory <your web application folder>/protected/tests
Open WebTestCase.php and change the TEST_BASE_URL in eclipse or text editor
define('TEST_BASE_URL','http://192.168.0.32/<my web application>/index-test.php/');

Note: I have already mentioned that my environment is different. In your cases it my be localhost or 127.0.0.1

After that open the url and see if there are any other errors with main config with index-text.php
(I have to do some other config because we have more things in index.php which aren't copied to index-test.php)


Write Test Cases

Yii made this easy by providing some basic test cases where we can understand them and implement the logic to our scenarios.

For sample test cases open below file

protected/tests/functional/SiteTest.php

One can start editing those test cases or write their own.
I have written my own for my specific requirement. Here is the code snippet for your reference
public function testContact()
{
    $this->open('?r=site/contact');
    $this->assertTextPresent('Contact Us');
    $this->assertElementPresent('name=ContactForm[firstName]');
    $this->type('name=ContactForm[firstName]','tester');
    $this->assertElementPresent('name=ContactForm[lastName]');
    $this->type('name=ContactForm[lastName]','beta');
    $this->type('name=ContactForm[email]','demo.123@gmail.com');
    $this->type('name=ContactForm[purpose]','other');
    $this->type('name=ContactForm[body]','PHPUnit test cases sample');
    $this->click("//input[@value='Submit']");
    $this->waitForTextPresent('Thank you for contacting us.');
}
To change test case preferences open phpunit.xml file and change as per your requirements.
If you feel you don't want to test in Internet Explorer then you can remove Internet Explorer browser node or you can add another browser to your configuration


Running Test Cases

Now start running test cases against PHPUnit using Selenium Remote Control Server

Before that make sure that we are running an instance of Selenium RC Server
For this case open another shell and type this command to run it
java -jar .\selenium-server-standalone-2.33.0.jar

 Leave that window because it will throw log information which may be useful.

Open another shell window for running test cases

Change directory to your project folder
phpunit.bat .\functional\SiteTest.php

Note: Add phpunit.bat location to PATH in environment variables to access it anywhere. It it suggested to add

Note: This is how I made PHPUnit to work on my workstation and run test cases of my project. Your experiences may vary. This post is purely meant for informational purposes and I don't guarantee that everything mentioned here works as expected.

For more information browse below links
Yii Testing
PHPUnit Documentation
PHPUnit Manual

No comments:

Post a Comment