Pytest2-Pytest uses and calls methods

Get into the habit of writing together! This is the second day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Thanks for liking and learning together. Follow me and learn a little bit every day.

  • [invoke pytest with python -m pytest]
  • [Possible execution exit code]
  • [Get help on version paths, command-line options, and environment variables]
  • [Stop test after 1(N) failure]
  • [Specify and select test cases]
  • [Create test report in JUnit XML format]
  • [calling pytest in Python code]

Invoke pytest with python -m pytest

We said in the last article, python -m pytest [...]to run the test cases. It's almost equivalent to direct invocation from the command line pytest [...].

Possible execution exit codes

When we execute test cases, each case will return a different status code, which is similar to our http status code. Mainly to see if the execution is successful. There are a total of 6 status codes. as follows:

  • Exit code 0 : Collect and successfully pass all test cases
  • Exit code 1 : Tests are collected and run, some test cases fail to execute
  • Exit code 2 : Test execution interrupted by user
  • Exit code 3 : An internal error occurred while executing the test
  • Exit code 4 : pytest command line usage error
  • Exit code 5 : No test cases collected

Get help with version paths, command-line options, and environment variables

image.png

Stop the test after the 1(N)th failure

pytest -x # 第1次失败后停止 
pytest --maxfail=2 # 2次失败后停止,可以是2,也可以是n
复制代码

Specify and select test cases

pytest test_model.pyRun all cases in the module run the case that matches the case in the
pytest testfilerun directory run the case
pytest -k "key"that matches the expression run the case
pytest test_model.py::test_funcspecified in the
pytest test_model.py::TestClass::test_funcmodule run the case specified in the module
pytest -m slowrun the case with the @pytest.mark.slow decorator
pytest --pyargs pkg.testrun the case in the package

Create test reports in JUnit XML format

To create XML test reports that can be read by Jenkins or other continuous integration software, you can use: pytest --junutxml=pathpath is the path file.

image.png

Calling pytest in Python code

This is simpler.pytest.main(['-x','mytestdir'])

Guess you like

Origin juejin.im/post/7086649153324843022