Writing and executing test cases
In this recipe, we will learn how to define and run tests using the pytest
library.
Getting ready
We will use the pytest
module. We should install the module, adding it to our requirements.txt
file as follows:
$ echo "pytest==5.4.1" >> requirements.txt
$ pip install -r requirements.txt
We will use the file test_case.py. You can download it from the GitHub repository at https://github.com/PacktPublishing/Python-Automation-Cookbook-Second-Edition/tree/master/Chapter12/tests.
How to do it...
- Check the file
tests/test_case.py
, which contains the definition of four tests:LIST = [1, 2, 3] def test_one(): # Nothing happens pass def test_two(): assert 2 == 1 + 1 def test_three(): assert 3 in LIST def test_fail(): assert 4 in LIST
- Run
pytest
to run the test file:$ pytest tests/test_case.py =====================. test session starts ====================== platform...