Skip to content
Advertisement

How to best structure conftest and fixtures in across multiple pytest files

Let’s say I have 3 lists of DataFrames containing different data that I want to run the same test cases on. How do I best structure my files and code so that I have one conftest.py (or some sort of parent class) that contains all the test cases that each list needs to run on, and 3 child classes that have different ways of generating each list of DataFrames but run the same test cases?

This is how I am currently constructing it.

JavaScript

dfs will not be modified throughout the test suite, so I want to treat this like a setup.

TestTwo and TestThree are the same thing except it will be get_list_of_dfs_somewhere("two") and get_list_of_dfs_somewhere("three")

Any tips on how to efficiently structure this would be appreciated!

Advertisement

Answer

In case if you need to run the same test case but with different data you can use the parametrize function. So, let’s say this is you test:

JavaScript

And you need to run it 3 times. One for each data frame you have.
To do that you can put all the data you need into a list.
But first, let’s create the classes (I’ve simplified them a bit):

JavaScript

Now, let’s create the file with tests:

JavaScript

The result is:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement