pytest - How to execute a function after specific tests

Viewed 1753

I have some tests organized in several classes. I already have a test fixture with scope=class so that it would run before suite(class) of tests. However, I need to execute a function after some specific tests. Lets say I have 100 tests in a class, I already have a fixture that will execute a function before these tests, but I also want to run a function after 2-3 of these tests.

What is the best approach to achieve that? Can it be done with fixtures or anything else ?

3 Answers

If you know the specific tests you want to run after you could set up the tests in a .csv with different values for each test for Example (index) (column) (column) Tests Test# Complete? test 1 1 T test 2 2 F

import pandas as pd 
#makes panda read your file and sets the variable as the data in the file
data = pd.read_csv("filename.csv") 
#Gets the value from the column 'test#' and 'complete' and sets as a variable
Var1 = DataFrame.get_value("the number test you want to take", 'Complete?')
If Var1 = T 
   #Run the rest of your program here
#Then you can just repeat this for the other tests you want to check for 

This isn't the prettiest solution but it works

Related