From the course: Unit Testing in Django

Unlock the full course today

Join today to access over 25,200 courses taught by industry experts.

Being lazy: pytest fixtures

Being lazy: pytest fixtures - Django Tutorial

From the course: Unit Testing in Django

Being lazy: pytest fixtures

- [Leticia] In the last video, we created two tests for a single endpoint, with tons of code being reused. As you can imagine, we can do better, right? For instance, whenever we need a logged user, we need to add at least two lines of code. What we can do with pytest is actually create a fixture, which is a function that we can reuse in the tests. We can do that by creating a normal function that has the Django client as a parameter. Let's do this on the top of the file, and let's create a function called logged_user that receives client. And then here we can copy this code. And let's return the user. Now the only thing we need to do to make sure this is a fixture is add a decorator. So pytest.fixture. The decorator will transform this regular function into a useful input for pytest. All we need to do is add this fixture as part of the input for our test. So in here we can say we are going to receive client and we're…

Contents