Tutorials / Recipes

Data-driven testing from CSV, JSON or a database

parameters= accepts a plain function, not just a list, so the data can come from anywhere: a CSV export, a JSON fixture, a database query. Test Junkie calls the function when the run starts and runs the test once per item it returns.

Test data from a function

test_account_provisioning.pypython
import csv

import requests
from test_junkie.decorators import Suite, beforeClass, afterClass, test


def load_test_accounts():
    # accounts.csv:
    # username,plan
    # alice,free
    # bob,pro
    with open("accounts.csv", newline="") as source:
        return list(csv.DictReader(source))


@Suite()
class AccountProvisioningSuite:

    @beforeClass()
    def open_session(self):
        self.session = requests.Session()  # once, before all the accounts below

    @afterClass()
    def close_session(self):
        self.session.close()  # once, after every account was checked

    @test(parameters=load_test_accounts)  # the function itself, not load_test_accounts()
    def account_gets_plan_limits(self, parameter):
        # runs once per row: {"username": "alice", "plan": "free"}, ...
        account = provision_account(self.session, parameter["username"], parameter["plan"])
        assert account["plan"] == parameter["plan"]
One session, many parameters
@beforeClass and @afterClass run once per suite run, not once per test parameter, so the session above is shared by every account check instead of being rebuilt for each one.
When the data can't be loaded
If the function raises (a missing file, a database that's down), the test is reported as ignored with the function's error, and the rest of the run goes on.

Parameterizing the suite itself

The same works on @Suite(). Use it when the setup itself depends on the data, e.g. logging in as a different account for each suite run:

test_account_permissions.pypython
import json

from test_junkie.decorators import Suite, beforeClass, test


def load_roles():
    with open("roles.json") as source:
        return json.load(source)  # [{"user": "viewer@shop.example", "can_edit": false}, ...]


@Suite(parameters=load_roles)
class PermissionsSuite:

    @beforeClass()
    def log_in(self, suite_parameter):
        self.session = log_in_as(suite_parameter["user"])  # once per role

    @test()
    def edit_button_matches_role(self, suite_parameter):
        # declares suite_parameter, so it runs once per role
        page = open_product(self.session)
        assert page.edit_button_visible() == suite_parameter["can_edit"]

A test only runs once per suite parameter if it declares suite_parameter; one that doesn't runs once in total. Multi-layer parametrization covers combining both levels.