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 - File Execution
Pytest - File Execution
In this chapter, we will learn how to execute single test file and multiple test files. We already have a test file test_square.py created. Create a new test file test_compare.py with the following code −
def test_greater(): num = 100 assert num > 100 def test_greater_equal(): num = 100 assert num >= 100 def test_less(): num = 100 assert num < 200
Now to run all the tests from all the files (2 files here) we need to run the following command −
pytest -v
The above command will run tests from both test_square.py and test_compare.py. The output will be generated as follows −
test_compare.py::test_greater FAILED test_compare.py::test_greater_equal PASSED test_compare.py::test_less PASSED test_square.py::test_sqrt PASSED test_square.py::testsquare FAILED ================================================ FAILURES ================================================ ______________________________________________ test_greater ______________________________________________ def test_greater(): num = 100 > assert num > 100 E assert 100 > 100 test_compare.py:3: AssertionError _______________________________________________ testsquare _______________________________________________ def testsquare(): num = 7 > assert 7*7 == 40 E assert (7 * 7) == 40 test_square.py:9: AssertionError =================================== 2 failed, 3 passed in 0.07 seconds ===================================
To execute the tests from a specific file, use the following syntax −
pytest <filename> -v
Now, run the following command −
pytest test_compare.py -v
The above command will execute the tests only from file test_compare.py. Our result will be −
test_compare.py::test_greater FAILED test_compare.py::test_greater_equal PASSED test_compare.py::test_less PASSED ============================================== FAILURES ============================================== ____________________________________________ test_greater ____________________________________________ def test_greater(): num = 100 > assert num > 100 E assert 100 > 100 test_compare.py:3: AssertionError ================================= 1 failed, 2 passed in 0.04 seconds =================================Advertisements