From the course: Testing Python Data Science Code
Unlock the full course today
Join today to access over 25,200 courses taught by industry experts.
Fixtures - Python Tutorial
From the course: Testing Python Data Science Code
Fixtures
- Sometimes you need to run code before and after every test function. For example, say we have an API client. This API client. Let's set up some logging and then its initialization code. And finally we need also to close it once we're done with it. The code inside the client is not interesting. It's just a mock client to show the example of how we test. So if you want to have something which runs before and after a function, we are going to use fixtures. Sometimes known also as setup and tear down function. Fixtures can be at several levels. By default it is going to happen on every function but you can set it also to be at the file level or the class level or the whole session level. We write fixtures inside conftest.py. This is a special file that the pytest is loading before running the test. So we mark our code with pytest.fixture. We do a setup by creating the client. Then we yield the client and now the function…