Pytest Tutorial
Selected Reading
- Pytest - Discussion
- Pytest - Useful Resources
- Pytest - Quick Guide
- Pytest - Conclusion
- Pytest - Summary
- Test Execution Results in XML
- Pytest - Run Tests in Parallel
- Stop Test Suite after N Test Failures
- Pytest - Xfail/Skip Tests
- Pytest - Parameterizing Tests
- Pytest - Conftest.py
- Pytest - Fixtures
- Pytest - Grouping the Tests
- Substring Matching of Test Names
- Execute a Subset of Test Suite
- Pytest - File Execution
- Pytest - Starting With Basic Test
- Identifying Test files and Functions
- Pytest - Environment Setup
- Pytest - Introduction
- Pytest - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Pytest - Conftest.py
Pytest - Conftest.py
We can define the fixture functions in this file to make them accessible across multiple test files.
Create a new file conftest.py and add the below code into it −
import pytest @pytest.fixture def input_value(): input = 39 return input
Edit the test_span_by_3_6.py to remove the fixture function −
import pytest def test_spanisible_by_3(input_value): assert input_value % 3 == 0 def test_spanisible_by_6(input_value): assert input_value % 6 == 0
Create a new file test_span_by_13.py −
import pytest def test_spanisible_by_13(input_value): assert input_value % 13 == 0
Now, we have the files test_span_by_3_6.py and test_span_by_13.py making use of the fixture defined in conftest.py.
Run the tests by executing the following command −
pytest -k spanisible -v
The above command will generate the following result −
test_span_by_13.py::test_spanisible_by_13 PASSED test_span_by_3_6.py::test_spanisible_by_3 PASSED test_span_by_3_6.py::test_spanisible_by_6 FAILED ============================================== FAILURES ============================================== ________________________________________ test_spanisible_by_6 _________________________________________ input_value = 39 def test_spanisible_by_6(input_value): > assert input_value % 6 == 0 E assert (39 % 6) == 0 test_span_by_3_6.py:7: AssertionError ========================== 1 failed, 2 passed, 6 deselected in 0.09 seconds ==========================
The tests will look for fixture in the same file. As the fixture is not found in the file, it will check for fixture in conftest.py file. On finding it, the fixture method is invoked and the result is returned to the input argument of the test.
Advertisements