Stop Test Suite after N Test Failures in Pytest
Last Updated :
04 Nov, 2023
The Python package which makes applications easy to write small, readable tests, and can scale to support complex functional testing is known as Pytest. The Pytest can have various checks, which keep running until it encounters the first failure or error. But if the user wants the test suite to run until it encounters N failures, he can use the maxfail function. In this article, we will describe how to stop the test suite after N failures in Pytest.
Syntax
pytest main.py -v –maxfail N
or
pytest main.py –maxfail=N
Here,
- N: It states the number of failures after which the user wants the test suite to stop.
Stop Test Suite after N Test Failures in Python Pytest
Example 1: In this example, we have created a program that has four test cases, checking floor, equality, subtraction, and square root. We will try stopping the program after N test cases.
Python3
import math
def test_floor():
num = 7
assert num = = math.floor( 6.34532 )
def test_equal():
assert 50 = = 49
def test_difference():
assert 99 - 43 = = 57
def test_square_root():
val = 8
assert val = = math.sqrt( 81 )
|
Run the Program
Now, we will set N as 3 and stop the test suite after 3 test failures in Pytest. We will run the following command in the terminal:
pytest main2.py --maxfail=3
Output:
Example 2: In this another example, we have defined a string, then we have created 3 unit test cases on it, for removing substrings G, e and o. Further, we execute command to stop test suite after 2 test failures in Pytest.
Python3
string_match = "Geeks For Geeks"
def test_remove_G():
assert string_match.replace( 'G' ,'') = = "eks For eeks"
def test_remove_e():
assert string_match.replace( 'e' ,'') = = "Gaks For Gaks"
def test_remove_o():
assert string_match.replace( 'o' ,'') = = "Geeks For Geeks"
|
Run the Program
Now, we will set N as 2 and stop the test suite after 2 test failures in Pytest. We will run the following command in the terminal:
pytest main.py --maxfail=2
Output
Share your thoughts in the comments
Please Login to comment...