Unit testing with Robot Framework
The problem
You are implementing a robot for automating manual accounting tasks. One of the tasks is calculating the net income and inserting that into the accounting system.
Currently, the net income calculation is done manually. There are several issues with this approach:
- It's slow.
- It's error-prone.
- The errors are caught much later, causing a lot of pain and backtracking.
The solution
You decide to code a Python library for doing the calculation. Implementing it does not take too long:
See How to write your own Robot Framework libraries in Python for more information.
accounting.py
Next, you implement the accounting robot. You import the accounting library to do the heavy number crunching:
tasks.robot
:
Proving the solution works: The manual way
How do you prove the calculation you implemented works correctly right now? How about later? It would suck majorly if the calculation were incorrect.
Sure, you can manually test the calculation logic, but all those manual tests need to be completed again if the implementation needs to be modified later. There are several issues with this approach, too:
- It's slow.
- It's error-prone.
- The errors are caught much later, causing a lot of pain and backtracking.
You talk the talk, but do you walk the walk?
There's much talk about the importance of testing. Unfortunately, sometimes we talk the talk but don't actually walk the walk. Talking is easier than walking, I guess.
Since you are a proud professional, you roll up your sleeves and decide to implement real tests! First, you need a testing framework...
Luckily your chosen stack already includes one of the most proven testing solutions out there: Robot Framework!
You create a new robot file for the test cases:
tests.robot
:
You decide to keep the test-related stuff separate from the production implementation. You create the following test_robot.yaml
configuration file:
See Robot YAML configuration format for more information.
This configuration file is exactly the same as the production
robot.yaml
, but instead of callingtasks.robot
, you calltests.robot
!
You open your terminal and tell RCC to execute the test tasks:
See RCC toolchain for more information.
Great success!:
You open the log file and see human-readable assertions:
The calculations work! And now you have a way to prove it. The tests assure that you can do modifications to the logic with the knowledge that the tests will warn you if you accidentally broke something! Such a peace of mind.
You pat yourself on the back, close the lid of your laptop, pour a cup of your favorite beverage, and start surfing Reddit! It's great being an automation professional!
For more extensive real-life examples, see the tests for Robot Framework Browser library.