In unittest, I can setUp variables in a class and then the methods of this class can chose whichever variable it wants to use...
class test_class(unittest.TestCase):
def setUp(self):
self.varA = 1
self.varB = 2
self.varC = 3
self.modified_varA = 2
def test_1(self):
do_something_with_self.varA, self.varB
def test_2(self):
do_something_with_self_modified_varA, self.varC
So in unittest, it was easy to put bunch of tests together that could go under one class and then use many different variables (varA and varB) for different methods. In pytest, I created a fixture in conftest.py instead of a class in unittest, like this...
@pytest.fixture(scope="module")
def input1():
varA = 1
varB = 2
return varA, varB
@pytest.fixture(scope="module")
def input2():
varA = 2
varC = 3
return varA, varC
I feed this input1 and input2 to my functions in a different file (let's say test_this.py) for two different functions. Here are the questions based on information above...
Since I can't just declare local variables in conftest.py as I can't simply import this file. Is there a better way of declaring different variables here that can be used in different functions in test_this.py ? I have five different configurations in my actual testing for these variables, defining that many different fixtures in conftest.py and use them as function argument in five different functions in test_this.py sounds painful, I would rather go back to unittest class structure, define my variables and pick and choose what I want
Should I just declare global variables in test_this.py and use them in the functions the way I want ? Seems a bit not pythonic. This variables are only used by the functions in this file.
Let's say I have test_that.py and test_them.py as well. If I have some shared variables between these different files, how would I declare them ? just create a file calle variables.py in the directory where all these test files are and do an import whenever I need ? This way I can keep all data in a separate.
Is it my impression that pytest discourages using a class to organize your functions ? Every example I read online, it all seem to employ bunch of functions with fixtures only. What is a configuration of defining class and methods and organize tests in pytest ?
I have a test scenario where I have to use result of one function into another. With pytest, I have an assert that is at the end of a function not a return so I won't be able to use this function as a fixture. How do I accomplish this ? I know this is not a good practice that my one test relies on another but is there a work around ?
Thanks in advance for your answers.