- pytest Quick Start Guide
- Bruno Oliveira
- 311字
- 2021-07-16 17:51:27
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 id. It 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.
- 通信網絡基礎與設備
- FreeSWITCH 1.2
- 連接未來:從古登堡到谷歌的網絡革命
- 物聯網工程規劃技術
- 走進物聯網
- Windows Server 2003 Active Directory Design and Implementation: Creating, Migrating, and Merging Networks
- Drush User’s Guide
- 數字通信同步技術的MATLAB與FPGA實現:Altera/Verilog版(第2版)
- NB-IoT物聯網技術解析與案例詳解
- 計算機網絡原理與應用技術
- The Kubernetes Workshop
- Unity Artificial Intelligence Programming
- 面向5G-Advanced的關鍵技術
- 大型企業微服務架構實踐與運營
- 設備監控技術詳解