Introduction to PyTest Fixtures
Introduction to PyTest Fixtures
What is PyTest?
PyTest is a popular testing framework for Python that makes it easy to write simple and scalable test cases. It allows you to write tests efficiently, run them in parallel, and generate detailed reports. PyTest is widely used in automation testing because of its simplicity and rich feature set.
What are PyTest Fixtures?
PyTest fixtures are functions that help in setting up test environments. They allow you to initialize resources before a test runs and clean up after the test completes. Fixtures make tests more reliable and reusable by providing a consistent test setup.
Why Use Fixtures?
- They help in setting up test preconditions.
- They improve code reusability by eliminating redundant setup code.
- They ensure a clean test environment by handling setup and teardown automatically.
Example of PyTest Fixture
Here’s a simple example demonstrating how to use fixtures in PyTest.
import pytest """ PyTest Tutorials - www.TestingDocs.com """ @pytest.fixture() def my_fixture(): print("\n I'm the fixture - setUp") yield print("I'm the fixture - tearDown") def test_first(my_fixture): print("First test - with fixture") def test_second(my_fixture): print("Second test - with fixture") def test_third(): print("Third test (* without fixture * )")
Explanation
The @pytest.fixture
decorator defines a fixture named my_fixture
You can mark a function with @pytest.fixture
to create fixtures. And to use fixture, we pass fixture name as a parameter to the test functions. In the above example, the first two tests receive the fixture called ‘my_fixture’, they have an argument with the fixture name, i.e. my_fixture
Test Output
============================= test session starts =============================
collecting ... collected 3 items
test.py::test_first
I'm the fixture - setUp
PASSED [ 33%]First test - with fixture
I'm the fixture - tearDown
test.py::test_second
I'm the fixture - setUp
PASSED [ 66%]Second test - with fixture
I'm the fixture - tearDown
test.py::test_third PASSED [100%]Third test (* without fixture * )
============================== 3 passed in 0.04s ==============================
Notice the test output, before executing test_first and test_second methods, code in my_fixture executed before any tests are executed, and tearDown (yield) got executed after each test execution. The last test ‘test_third’ was executed without using any fixture.
Fixtures provides an easy yet powerful way to setup and teardown resources. You can then pass these defined fixture objects into your test functions as input arguments.
Another example
Let’s consider another example as shown below:
import pytest
@pytest.fixture
def sample_data():
return {"name": "Alice", "age": 30}
def test_sample_data(sample_data):
assert sample_data["name"] == "Alice"
assert sample_data["age"] == 30
Explanation
- The
@pytest.fixture
decorator defines a fixture namedsample_data
. - The fixture returns a dictionary with sample data.
- The test function
test_sample_data
receives the fixture as an argument. - PyTest automatically injects the fixture data into the test function.
- The test assertions check if the data is correct.
PyTest fixtures provide a powerful way to manage test setup and teardown efficiently. By using fixtures, you can write cleaner and more maintainable test cases.