Writing testscript effectively in Pyxis
TDD is a method that help improving product quality, promoting simplicity and enhancing flexibility. Implementing TDD may be different between organization, but writing test script that effectively detects bugs is always the central concept of TDD. However, writing good test script is not always an easy job to do as developer may envolve various problems. It is said that “The cost for writing test script should be equal or even greater than cost for implementing the code itself”. However, there are ways to make the situations easier and I have come up with my own tips. In this article, I want to share my expericence when writing test script in Pyxis project.
1) Dealing with fixture
Fixture is a very nice concept of test script. It help making necessary data for the script without using the real database. The way fixture works in test script can be summaried in 3 step.
- Create necessary table for the script to run. (Setup process)
- Insert neccesary records. (Setup process)
- Delete tables and its data. (Clean up process)
As we can see, everytime the script is called, it must prepare a lot of data and this consumes considerable amount of time. From my own statistics, this process takes about 80% of total time for running the test script and it expands time for writing test. If we are able to skip this process, it will greatly save our time. And this is my method to do that:
Step 1. Prevent clean up process
- Add the following function to the test script
public function stopCleanUp () { die(); }
- Run the script
Visit the test database, we can see a fixture’s tables created here.
Step 2. Use created database instead of fixture.
- Remove fixtures declaration.
Change
public $fixtures = array('app.bulkprofile', 'app.bulkad', 'app.bulkimage');
Into
public $fixtures = array();
- Remove function stopCleanUp
- Run the script normally.
As we can see, the script return the same result, but expanding time is much shorter.
2) Dealing with cookie and session.
In every project that requires authentication, dealing with cookie and session is unavoidable. Fortunately, with the support of mock objects, we can easily bypass this situation.
In Pyxis project, in order for most functions in test controller to run, we need to inject these functions:
- getProfileId().
- getUserId().
- getUserRole().
By adding this code to setup function.
$methods = array('getRequestParams', 'getProfileId', 'getUserId', 'getUserRole');
$this->BulkController = $this->getMock('BulkController',$methods);
$this->BulkController->expects($this->any()) ->method('getProfileId') ->will($this->returnValue('38')); $this->BulkController->expects($this->any()) ->method('getUserId') ->will($this->returnValue('1')); $this->BulkController->expects($this->any()) ->method('getUserRole') ->will($this->returnValue('1'));
3) Dealing with post data.
In testing controller, each function require appropriate POST data to run properly.
In Pyxis controller, POST data is retrieved through function getRequestParams();
Injecting POST data can also be done by using mock object. This should be added at the top of each test function.
public function testBulkTab1() { $params = array( 'targeting' => '[null,{"s1Tab":1,"edit":2,"campaign_group_id":"3743"' ,'currency' => 'JPY' ,'mode' => 'ADAds' ,'timeout' => 120000 ,'out' => 'json' ); $this->BulkController->expects($this->any()) ->method('getRequestParams') ->will($this->returnValue($params)); }