- 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 - Xfail/Skip Tests
In this chapter, we will learn about the Skip and Xfail tests in Pytest.
Now, consider the below situations −
A test is not relevant for some time due to some reasons.
A new feature is being implemented and we already added a test for that feature.
In these situations, we have the option to xfail the test or skip the tests.
Pytest will execute the xfailed test, but it will not be considered as part failed or passed tests. Details of these tests will not be printed even if the test fails (remember pytest usually prints the failed test details). We can xfail tests using the following marker −
@pytest.mark.xfail
Skipping a test means that the test will not be executed. We can skip tests using the following marker −
@pytest.mark.skip
Later, when the test becomes relevant we can remove the markers.
Edit the test_compare.py we already have to include the xfail and skip markers −
import pytest @pytest.mark.xfail @pytest.mark.great def test_greater(): num = 100 assert num > 100 @pytest.mark.xfail @pytest.mark.great def test_greater_equal(): num = 100 assert num >= 100 @pytest.mark.skip @pytest.mark.others def test_less(): num = 100 assert num < 200
Execute the test using the following command −
pytest test_compare.py -v
Upon execution, the above command will generate the following result −
test_compare.py::test_greater xfail test_compare.py::test_greater_equal XPASS test_compare.py::test_less SKIPPED ============================ 1 skipped, 1 xfailed, 1 xpassed in 0.06 seconds ============================Advertisements