官术网_书友最值得收藏!

Running tests

Pytest can run your tests in a number of ways. Let's quickly get into the basics now and, later on in the chapter, we will move on to more advanced options.

You can start by just simply executing the pytest command:

λ pytest

This will find all of the test_*.py and *_test.py modules in the current directory and below recursively, and will run all of the tests found in those files:

  • You can reduce the search to specific directories:
      λ pytest tests/core tests/contrib
  • You can also mix any number of files and directories:
      λ pytest tests/core tests/contrib/test_text_plugin.py
  • You can execute specific tests by using the syntax <test-file>::<test-function-name>:
      λ pytest tests/core/test_core.py::test_regex_matching
  • You can execute all of the test methods of a test class:
      λ pytest tests/contrib/test_text_plugin.py::TestPluginHooks
  • You can execute a specific test method of a test class using the syntax <test-file>::<test-class>::<test-method-name>:
      λ pytest tests/contrib/
test_text_plugin.py::TestPluginHooks::test_registration

The syntax used above is created internally by pytest, is unique to each test collected, and is called a node id or item idIt basically consists of the filename of the testing module, class, and functions joined together by the :: characters.

Pytest will show a more verbose output, which includes node IDs, with the -v flag:

 λ pytest tests/core -v
======================== test session starts ========================
...
collected 6 items

tests\core\test_core.py::test_regex_matching PASSED [ 16%]
tests\core\test_core.py::test_check_options FAILED [ 33%]
tests\core\test_core.py::test_type_checking FAILED [ 50%]
tests\core\test_parser.py::test_parse_expr PASSED [ 66%]
tests\core\test_parser.py::test_parse_num PASSED [ 83%]
tests\core\test_parser.py::test_parse_add PASSED [100%]

To see which tests there are without running them, use the --collect-only  flag:

λ pytest tests/core --collect-only
======================== test session starts ========================
...
collected 6 items
<Module 'tests/core/test_core.py'>
<Function 'test_regex_matching'>
<Function 'test_check_options'>
<Function 'test_type_checking'>
<Module 'tests/core/test_parser.py'>
<Function 'test_parse_expr'>
<Function 'test_parse_num'>
<Function 'test_parse_add'>

=================== no tests ran in 0.01 seconds ====================

--collect-only is especially useful if you want to execute a specific test but can't remember its exact name.

主站蜘蛛池模板: 广东省| 嵊泗县| 德兴市| 普定县| 兴安县| 江津市| 卓资县| 海阳市| 盐山县| 梅州市| 抚远县| 织金县| 科技| 阳新县| 南投市| 墨玉县| 醴陵市| 萨嘎县| 神木县| 常州市| 罗定市| 陈巴尔虎旗| 门头沟区| 古蔺县| 资中县| 云龙县| 鄢陵县| 延长县| 应用必备| 佛山市| 福清市| 栖霞市| 永修县| 民乐县| 思茅市| 钟祥市| 湖北省| 南澳县| 达拉特旗| 芜湖市| 凤庆县|