How to run Data driven test cases in a loop in Python/Pytest with dynamic Def for test cases Name in Loop

Hi,
I have Test cases and test data in an excel sheet with screenshot as attached.

I have crated a python /Pytest script for reading test cases and data form Excel sheet . Now ned help to make "def test_module1(): " a dynamic to read from test cases data sheet from top column and capture to get Pytest benefits.

Test cases and data

Shouldn’t be too hard but it sounds like you don’t know enough python to do this. Where did this requirement come from? Who’s asking you to do this?

I admit that I have no clue what you’re asking for. Out of curiosity, can you (or @zedshaw, @gpkesley? you do a lot of testing, no?) explain this to me?

Hi Zedshaw,

I admit i am not in much python but liked Pytest features and trying to do in a demo for Test automation… in one of my data driven approach I want to pick up test case id in a loop from excel in pytest format “Test_ID No 001” etc. So I want to read the ID from Excel and instead of defining a new function(Def) for each test. I want it this in a for loop.

I can attach excel to explain more but can’t…

Thanks a lot for the interest and trying to solve my problem.

Regards
Manojsaxena
saxena.manojatgmail

Pytest has the parametrize data driven approach to I assume you want to map tour excel testcases to examples in @parametrize.

This decorator already loops over the test data for you. So all you need to do is import you excel document into the format.

Check out pytest patametrize and also read about fixtures.

But test automation should really be for a goal and what you describing doesn’t sound goal orientated, but rather automating a human process. What are you trying to do?

Its common for test data to be created in Excel and the people try to import that data (test cases) into a single test scenario with lots of data permutations.

Think of an insurance quote that is the same form but different data might generate a different premium.

So data driven tests exercise combinations with know results. But the actual test flow is usually very similar.

If the data analysis is good, a small number of test cases and data can provide really good functional system coverage.

1 Like

So, that’s not too, but I think you’ll want to just export it to CSV and use this:

https://docs.python.org/3/library/csv.html

There are libraries to read excell files, but this library would be the easiest way to do it.

Then, you’ll do the following:

  1. Open the CSV file which gives you the rows.
  2. For each row in the file.
  3. Perform the test using the columns of the row.

I suggest writing a small python script that does 1 and 2, once you learn that then write another python script that just does some Pytest, and finally, write another that combines what you learned.

You might have to learn quite a bit of python to make this happen, but you now have a reason to learn it, so just take it slow.

Hi,
This is Palak, @gpkesley, I am also expecting such answer, lots of confusions is clear

Thanks