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

  • 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

Into

 

  • 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.

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.

Add a Comment

Your email address will not be published.