1. View all available parameters of pytest
We can view all available parameters through pytest -h.
As can be seen from the figure, there are many parameters of pytest. The following is a summary of some commonly used parameters:
-s: Output debugging information, including information printed by print.
-v: Display more detailed information.
-n=num: Enable multi-threaded or distributed running of test cases. Requires the pytest-xdist plugin module to be installed.
-k=value: If the nodeid of the use case contains the value value, the use case is executed.
-m=markname: Execute the use cases marked by @pytest.mark.labelname.
-x: Stop the test execution of the current thread as long as one test case execution fails.
–maxfail=num: Same as -x function, except that the number of use case failures can be customized.
–reruns=num: Rerun failed cases num times. Requires the pytest-rerunfailures plugin module to be installed.
2. Parameter -s
Parameter -s, output debugging information, including the information printed by print.
Command line input: pytest -s
Inside the file:
if __name__ == '__main__':
pytest.main(["-s"])
3. Parameter -v
Parameter -v, output more detailed information.
Command line input: pytest -v
Inside the file:
if __name__ == '__main__':
pytest.main(["-v"])
4. Parameter -n=num
Parameter -n, enable multi-threaded or distributed running test cases. The pip install pytest-xdist plugin module needs to be installed.
Command line input: pytest -vs -n=2
Inside the file:
if __name__ == '__main__':
pytest.main(["-vs", "-n=2"])
5. Parameter -k=value
Parameter -k=value, the use case is executed if the nodeid of the use case contains the value value.
Command line input: pytest -vs -k=01
The value of -k supports Chinese~
6. Parameter -m=tag name
Parameter -m=label name, execute the use case marked by @pytest.mark.label name.
Execute a single specified mark case
pytest -m=hign or pytest -m highn
Execute the use case marked A or B
pytest -m="hign or smoke" or pytest -m "hign or smoke"
Execute the use case where there are both A mark and B mark
pytest -m="hign and smoke" or pytest -m "hign and smoke"
7. Parameter -x
Parameters -x, -x Stop the test execution of the current thread as long as one test case execution fails.
Command line input: pytest -vsx (Note: Multiple parameters can be used in combination, such as -s, -v, -x can be written as -svx)
8. Parameter –maxfail=num
Parameter –maxfail=num Stops the test execution of the current future when num test cases fail to execute.
Command line input: pytest -vs --maxfail=1 (set the maximum number of failures to 1, and the execution will not continue after one failure)
9. Parameter –reruns=num
Parameter –reruns=num, rerun the failed case num times. The pip install pytest-rerunfailures plugin module needs to be installed.
Command line input: pytest -vs --rerun=1 (set the number of failed reruns to 1, and run it again after the use case fails)