Humble_PrOgRaMeR
Humble_PrOgRaMeR

Reputation: 741

PyTest parameterised test with csv data

In many PyTest scripts example with parameterised data such as

@pytest.mark.parametrize("input1, input2, output", [(5, 5, 10), (7, 5, 12)])
def test_add(input1, input2, output):
    assert input1 + input2 == output, "failed"

annotation. However, there is a requirement for me to fetch cvs/xlsx data in PyTest parameterise for multiple test_ methods.

Lets say I have table in CSV as

input1 input2 output
5 5 10
7 5 12

Can anyone suggest the detailed solution for reading the data from CSV and use it in above test method?

Upvotes: 0

Views: 467

Answers (1)

Manish
Manish

Reputation: 514

You could try using pytest-csv-params which you can install using pip install pytest-csv-params

Then you could import data from your csv as shown below

from pytest_csv_params.decorator import csv_params

@csv_params(
    data_file="/path to csv/addition.csv",
    data_casts={
        "input1": int,
        "input2": int,
        "output": int,
    },
)
def test_addition(input1, input2, output):
    assert input1+ input2 == output, "failed"

More information could be found here link

Upvotes: 1

Related Questions