How to use pytest fixtures in Django testing

Harsh Bhimjyani
1 min readFeb 18, 2017

--

When we are using unittest in Django, they have their own setUp and tearDown methods to simplify tests and have more code re-usability.
But what if we are using pytest

Lets assume you are writing any type of test where you need to write 2 or more functions which needs some code re-usability or may be some sort of a setUp before test starts.

We can setup the fixture by adding the pytest.fixture decorator and also we need to add that as a parameter in our test methods. I know the code is weird but you get the idea right?
Now since we have added a class here, you can also use something like this self.something = SomeMagic() and omit the return statement.

Leave a like if you find this useful.

--

--