Innovate anywhere, anytime withruncode.io Your cloud-based dev studio.
Django

Django Acceptance Testing Automation with Robot Framework

2022-07-25

Testing is a typical phase in the Web application development flow because a Web application is made of multiple layers of logic starting from HTTP request handling, to request data validation and processing, to template rendering.

But the testing phase of a Django Application development, it’s really easy. We use the Unittest module built into the python standard library to write the test cases to simulate requests, insert the test data and inspect the application’s output.

You can perform automated testing in your Django application using different tools. Few of them are,

  • Writing the unit test cases using python’s test client
  • Writing the unit test cases using the selenium
  • Writing the behavioural test cases using the Django-behave framework
  • Writing the generic test cases for acceptance testing using the Robot framework

In this blog, let's try to learn what is Robot framework and how to write simple test cases using a robot framework.

Robot Framework is one of the best testing automation frameworks for acceptance testing. Which can be very useful in acceptance of test-driven development. This framework uses a keyword-driven testing approach, and it has easy-to-use tabular test data syntax, which is a very effective way to achieve user acceptance testing. This framework is having an extensive amount of test libraries implemented either with Python or Java. The testers can create new higher-level keywords from existing ones using the same syntax that is used for creating test cases. The robot framework is implemented using Python, and it can run also on Jython (JVM) and IronPython (.NET).

Write your first test case using Robot framework:

You can install robot framework with the following command

pip install robotframework

Starting from Robot Framework 3.0, tests are executed using the robot script and results post-processed with the robot script: The following is the syntax to run the test cases

robot tests.robot
robot output.xml

The other way to run tests is executing the installed robot module is directly using Python's -m command line option.

python -m robot tests.robot
python -m robot.rebot output.xml

Writing your test cases in robot framework:

As we discussed in the beginning of this blog post Robot framework uses a keyword-driven testing approach, and It has easy-to-use tabular test data syntax. Lets us start with a simple example of user account creation and sign in process. Following are the two cases that we try in this example.

  • User can create an account and log in
  • User cannot log in with the bad password
*** Test Cases ***

User can create an account and log in
    Create Valid User    fred    P4ssw0rd
    Attempt to Login with Credentials    fred    P4ssw0rd
    Status Should Be    Logged In 

User cannot log in with bad password
    Create Valid User    betty    P4ssw0rd
    Attempt to Login with Credentials    betty    wrong
    Status Should Be    Access Denied

In the above example, the syntax is more like natural English language rather than complex programming language style syntaxes.
You can save this with .robot extension and execute the test case using

robot myfirsttest.robot
robot output.xml

As discussed in this blog post, keywords are the major essentials of this framework. The keywords can be created keywords that can come from two sources.

  • Library keywords come from imported test libraries
  • User keywords can be created using the same tabular syntax that is used for creating test cases.

We can discuss more about keywords, variables in coming blog posts on Robot framework.