1

I have a scenario where there are 1000's of test cases in excel.
Is there a way to parameterize the test so that all the test cases run?

I have used pandas and I am getting all the test cases while iterating through:

def getCases():
    excel_file = 'path of excel'
    result = pd.read_excel(excel_file)
    count_row = result.shape[0]
    for i in range(count_row):
        r = result.iloc[i]
        return [r]

Each row in the excel table is a test case and the result returned is in the form of dictionary, which I want to pass as input to my test case.

I use the following fixture to pass these parameters to my test functions:

@pytest.fixture(params=PWC.getCases())
def getData(self, request):
    return request.param 

The problem is that after the first iteration, it is not reaching this code and my test case does not return to the getCases() function. How do I customize the params in pytest fixture so that I will be able to run all the cases?
Will I be able to pass a range inside getCases as parameter?

3
  • You have to use yield instead of return. Also, I'm not sure why you are making an array of the result - is this needed? Commented May 26, 2020 at 13:03
  • each row is a test case and the result returned is in the form of dictionary, i have to pass that dictionary as input to my test case. i wanted to wrap this dictionary into a list and send. Commented May 26, 2020 at 13:16
  • Ok, I'll put out that in an answer to show what I mean. Commented May 26, 2020 at 14:01

1 Answer 1

2

To be able to return the control to the function you have to use yield instead of return. Assuming each row contains both the input and the result data for the test, this would look something like this:

def getCases():
    excel_file = 'path of excel'
    result = pd.read_excel(excel_file)
    count_row = result.shape[0]
    for i in range(count_row):
        yield result.iloc[i]

@pytest.fixture(params=getCases())
def data(self, request):
    return request.param

def test_data(data):
    assert my_fct(data['input']) == data['result']

(assuming that there is only one input parameter in your tested function)

Sign up to request clarification or add additional context in comments.

3 Comments

I tried the above since I have a similar situation wherein each row in the csv table is a test case. It works fine, however the result is not written out for each step. Is there a way to do so?
Do you mean just verbose output like pytest -vv, or something else?
yes, I forgot the -vv flag. It did the trick. Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.