Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 36 ยท Projects and Practice

Project: a data-driven calculator suite

Test a Python library with a data-driven Robot Framework suite: templates, boundary tables, error cases, a custom library and a runnable table-driven checker.

Not every project involves a browser. A pure logic component, such as a calculator, a price engine or a validator, is the perfect training ground for data-driven testing: no timing problems, no environment, only inputs and expected outputs. In this project you test a small calculator library with tables of data, boundary values and error cases.

Robot Framework cannot run here, so the suite is read-only. The library under test and a table-driven checker run as Python stand-ins, so you can edit the tables and watch results.

The library under test

Python
# Calculator.py
class Calculator:
    def __init__(self):
        self.result = 0

    def add(self, a: float, b: float) -> float:
        self.result = a + b
        return self.result

    def divide(self, a: float, b: float) -> float:
        if b == 0:
            raise ZeroDivisionError("Cannot divide by zero")
        self.result = a / b
        return self.result

    def percent(self, amount: float, rate: float) -> float:
        if not 0 <= rate <= 100:
            raise ValueError("Rate must be between 0 and 100")
        return round(amount * rate / 100, 2)

Because it is a Python class with type hints, importing it as a Robot library is one line. Every public method is a keyword (Add, Divide, Percent). See custom libraries.

The Robot suite

TEXT
*** Settings ***
Library          Calculator.py
Test Template    Add Should Give

*** Test Cases ***                      A        B        Expected
Two small numbers                       1        2        3
Negative numbers                        -4       -6       -10
Zero is neutral                         0        9        9
Decimals                                0.1      0.2      0.3
Large numbers                           1e15     1        1000000000000001
TEXT
*** Keywords ***
Add Should Give
    [Arguments]    ${a}    ${b}    ${expected}
    ${actual}=    Add    ${a}    ${b}
    Should Be Equal As Numbers    ${actual}    ${expected}    precision=9

Should Be Equal As Numbers with precision avoids the classic floating-point trap: in many languages 0.1 + 0.2 is 0.30000000000000004, not 0.3. Never compare floats with plain equality.

Data-driven calculator tests

Typical values The normal cases.
Boundaries 0, 100, just outside.
Errors Divide by zero.
Precision Floats need tolerance.

One keyword, many rows; add a case by adding a line.

1 / 2

Expected errors

Failure cases deserve their own template, using Run Keyword And Expect Error (the message is matched, and * is a wildcard) or Run Keyword And Expect Error ZeroDivisionError: *:

TEXT
*** Test Cases ***
Division Rules
    [Template]    Divide Should Fail With
    5     0     Cannot divide by zero
    0     0     Cannot divide by zero

Percent Boundaries
    [Template]    Percent Should Give
    200    0      0
    200    100    200
    200    12.5   25
    200    101    ERROR Rate must be between 0 and 100

Write the keywords yourself as practice.

A table-driven checker you can run

This stand-in contains the real calculator class, a tolerance helper and a table of cases, including expected errors. Run it and read the results. Then add your own rows.

Python Stand-in: calculator and its tables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the last Division row: 1 / 3 is not exactly 0.3333333333, but within the relative tolerance it passes. In Robot you would set precision the same way.

Why plain float comparison fails

Here is the trap made visible, so you remember why tolerance matters.

Python Stand-in: float comparison

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Extend the project

  1. Add multiply and power. For each, write a table with typical values, zero, negatives and a very large result.
  2. Put the rows in a CSV and load them with DataDriver (see external data).
  3. Generate boundary rows from the rule "rate is 0 to 100" (see generated data).
  4. Tag error cases negative and run only them with --include negative.
  5. Write a listener that prints the name of each failed table row.

What you practiced

  • One keyword, many rows.
  • Boundary tables and error tables.
  • Float tolerance.
  • Python libraries as keywords.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why use precision with Should Be Equal As Numbers?

    Binary floating point cannot represent values like 0.1 exactly, so compare within a tolerance.
  2. 2. Which rows belong in a boundary table for a rate of 0 to 100?

    Test exactly on each edge and just outside it.
  3. 3. How can a template test check an error case?

    That keyword passes only when the inner keyword fails with the expected message.
  4. 4. What is the advantage of table-driven tests for logic code?

    Steps are written once; adding coverage means adding data.

Summary

  • Pure logic suits data-driven testing: no timing, only inputs and expectations.
  • Use Test Template with a keyword per kind of check.
  • Cover typical, boundary and error rows; compare floats with tolerance.
  • Expected errors are tested with Run Keyword And Expect Error.
  • Grow the tables, not the number of keywords.

Next: Practice exercises and interview questions