Pytest parameters external data file
We can move the data of the test-cases into external CSV or Excel file. Let someone els update and maintain the cases.
a, b, expected
1, 1, 2
2, 3, 5
-1, 1, 0
-1, -1, -2
Our code then reads the data from the data-file and uses that to fill the parameters.
import mymath
import pytest
import csv
filename = "cases.csv"
cases = []
with open(filename, "r") as fh:
reader = csv.reader(fh)
next(reader, None) # skip first row
for line in reader:
cases.append(tuple(map(int, line)))
#cases.append(list(map(int, line)))
#cases.append(list(map(float, line)))
print(cases)
@pytest.mark.parametrize("a,b,expected", cases)
def test_add(a, b, expected):
assert mymath.add(a, b) == expected