Are you confused about pytest's fixtures?
Do you struggle to understand them?
#100daysOfCode #pytest #Python
This one is for you
Do you struggle to understand them?
#100daysOfCode #pytest #Python
This one is for you
pytest's fixtures are functions decorated with "pytest.fixture" decoratorThey can return value or produce side effects like creating/removing a database.
They can be located inside http://conftest.py or inside the test file.
To use the value returned from the fixture function inside a test you need to add a parameter with the same name as the fixture function.
You can run part of the fixture before the test function (before yield) and the part after (after yield).For example:
* create ES index before test function
* remove ES index after test function
A fixture can accept parameters when using factoriesIt returns the factory method instead of value.
You can use the factory method inside a test to get a value based on provided parameters
For example, create users with different usernames
By default, fixtures run before/after each test - scope='function'.You can change the scope of the fixture using the *scope* parameter:
* 'module' - before/after all tests in the module
* 'session' - before first and after the last test in the test suite
You can provide multiple different fixtures to the same test by using* fixture's params argument
* pytest's request
An example
pytest also provides built-in fixtures like:- tmp_path: pathlib.Path object to a temporary directory unique to each test function
- request: information on the executing test function
- caplog: Control logging and access log entries
- ...
Examples
You can read more in their official docshttps://docs.pytest.org/en/latest/fixture.html?highlight=fixtures
Read on Twitter