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
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
- PHP environment
- PEAR package manager
- Apache Server (I don’t cover this step here)
- Selenium RC Server to test the code
- PHPUnit to test PHP test cases
- PHPUnit_Selenium
Get PHP runtime
Goto below urlhttp://php.net/downloads.phpDownload 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.pharDownload the phar file.Copy the go-pear.phar file to C:Users<user>Downloadsphp-5.3.26-Win32-VC9-x86
directoryNow open command prompt (I’m using power shell for all operations) and execute go-pear.phar using php runtimeNote: From this point I consider shell
as your command shell whether it is command prompt or power shell
|
|
If it asks for ’local’ or ‘system’ then go for system
To select all options press enter
To install registry keys execute
|
|
Install Selenium RC Server as documented in Yii
http://selenium.googlecode.com/files/selenium-server-standalone-2.33.0.jarWell 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
|
|
Install SeleniumTest
|
|
If you run into any errors like “No releases available for package “pear.phpunit.de/PHPUnit_Selenium”” then run below command
|
|
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
|
|
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 scenariosFor 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
|
|
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
|
|
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
|
|
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