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.
import pytest
class TestOne:
# this method usually takes 10 mins to run
# so we want this to run once and use the same Dict for all test cases
dfs: Dict[str, pd.DataFrame] = get_list_of_dfs_somewhere("one")
def test_dfs_type(self):
assert isinstance(self.dfs, dict)
def test_another_one(self):
assert ...
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!
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: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):
Now, let's create the file with tests:
The result is: