欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pytest命令行選項(xiàng)的具體使用

 更新時間:2023年11月03日 09:45:57   作者:旦莫  
pytest是一個流行的Python測試框架,它提供了許多命令行選項(xiàng),本文主要介紹了Pytest命令行選項(xiàng)的具體使用,具有一定的參考價值,感興趣的可以了解一下

pytest 是一個流行的 Python 測試框架,它提供了許多命令行選項(xiàng),可以幫助用戶更好地控制測試執(zhí)行過程。在使用 pytest 進(jìn)行測試時,熟悉 pytest 的命令行選項(xiàng)非常重要,這將有助于減少錯誤和提高測試效率。本文將詳細(xì)介紹 pytest 的命令行選項(xiàng),并給出示例。

一、pytest 命令行選項(xiàng)分類

pytest 的命令行選項(xiàng)可以分為三類:

  • 環(huán)境配置選項(xiàng):這些選項(xiàng)用于設(shè)置 pytest 的環(huán)境配置,例如設(shè)置日志級別、覆蓋配置文件、設(shè)置測試模式等。
  • 測試過濾選項(xiàng):這些選項(xiàng)用于過濾和選擇測試用例,例如指定測試目錄、選擇特定測試模塊、運(yùn)行指定測試函數(shù)等。
  • 測試執(zhí)行選項(xiàng):這些選項(xiàng)用于控制 pytest 的測試執(zhí)行過程,例如重試失敗測試、生成測試報(bào)告、并行執(zhí)行測試等。

下面,我們將逐一介紹這些命令行選項(xiàng)的具體用法。

二、環(huán)境配置選項(xiàng)

--version:打印 pytest 版本號。

-h, --help:顯示 pytest 命令行選項(xiàng)和使用說明。

--verbose, -v:增加測試結(jié)果輸出的詳細(xì)程度。

--quiet, -q:減少測試結(jié)果輸出的詳細(xì)程度。

--traceconfig:顯示解析和加載配置文件時的詳細(xì)信息。

--pdb:當(dāng)測試失敗或者發(fā)生異常時,進(jìn)入 Python 調(diào)試器。

--pdbcls:自定義調(diào)試器的類型。

--capture:設(shè)置 pytest 的標(biāo)準(zhǔn)輸出流捕獲方式,有三個值可選:sys、fd、no。

--norecursedirs:設(shè)定哪些目錄不搜索測試用例。

--rootdir:設(shè)置 pytest 的根目錄。

--maxfail=n:設(shè)定在第 n 次測試失敗后停止測試執(zhí)行。

--junit-xml=PATH:將測試結(jié)果輸出到 JUnit XML 文件中。

三、測試過濾選項(xiàng)

path:指定測試目錄或文件,可以是相對路徑或絕對路徑。例如:pytest tests/。

-m:選擇特定標(biāo)記的測試用例進(jìn)行執(zhí)行。例如:pytest -m "slow"。

-k:選擇包含某個關(guān)鍵字的測試用例進(jìn)行執(zhí)行。例如:pytest -k "add or subtract"。

-x:遇到一條測試用例失敗就停止測試執(zhí)行。

--pdb:當(dāng)測試失敗或者發(fā)生異常時,進(jìn)入 Python 調(diào)試器。

--lf:只重跑上次測試失敗的測試用例。

--ff:只重跑上次測試失敗的測試用例,并在全部測試結(jié)束后再重新運(yùn)行一遍這些測試用例。

--sw:重跑上次修改過的測試模塊。

--last-failed-no-failures:只重跑上次失敗的測試用例(如果沒有失敗的測試用例,則不執(zhí)行測試)。

--collect-only:只執(zhí)行測試用例的收集階段,不運(yùn)行測試用例的執(zhí)行階段。

--pdbcls:自定義調(diào)試器的類型。

--trace:顯示 pytest 的內(nèi)部跟蹤信息。

--count:運(yùn)行指定數(shù)量的測試用例。例如:pytest -v --count=10。

四、測試執(zhí)行選項(xiàng)

-n:并行運(yùn)行測試用例,可以在后面跟一個數(shù)字指定并發(fā)度。例如:pytest -n 4。

-x:遇到一條測試用例失敗就停止測試執(zhí)行。

--maxfail=n:設(shè)定在第 n 次測試失敗后停止測試執(zhí)行。

--last-failed:只重跑上次失敗的測試用例。

--failed-first:先運(yùn)行之前失敗的測試用例。

--reruns=n:在測試用例失敗的情況下,重新運(yùn)行 n 次測試。

--pdb:當(dāng)測試失敗或者發(fā)生異常時,進(jìn)入 Python 調(diào)試器。

--pdbcls:自定義調(diào)試器的類型。

--junit-xml=PATH:將測試結(jié)果輸出到 JUnit XML 文件中。

--html=PATH:將測試結(jié)果輸出到 HTML 文件中。

--tb=long/short/line/native/no:設(shè)置輸出錯誤信息的格式。

--capture=no:禁止捕獲標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯誤,直接將它們輸出到終端。

--capture=sys/stdout/stderr:設(shè)置 pytest 的標(biāo)準(zhǔn)輸出流捕獲方式,有三個值可選:sys、fd、no。

--show-capture=all/failed/no:控制是否顯示捕獲的標(biāo)準(zhǔn)輸出流。

--disable-warnings:禁用 pytest 的警告信息。

以上是 pytest 的命令行選項(xiàng)介紹,通過使用這些選項(xiàng),可以更好地控制 pytest 的測試執(zhí)行過程,減少錯誤和提高測試效率。接下來我將給出部分命令行選項(xiàng)的代碼示例及運(yùn)行效果。

五、代碼示例及運(yùn)行效果

1.--version:打印 pytest 版本號。

該選項(xiàng)可以通過執(zhí)行 pytest --version 命令來使用,代碼示例如下:

$ pytest --version
This is pytest version 5.0.1, imported from /usr/local/lib/python3.6/site-packages/pytest.py

2.-h, --help:顯示 pytest 命令行選項(xiàng)和使用說明。

該選項(xiàng)可以通過執(zhí)行 pytest --help 命令來使用,代碼示例如下:

$ pytest --help
usage: pytest [options] [file_or_dir] [file_or_dir] [...]

positional arguments:
  file_or_dir           test file or root directory to search for tests

optional arguments:
  -h, --help            show this help message and exit
  --version             display pytest version and information about plugins
...

3.--verbose, -v:增加測試結(jié)果輸出的詳細(xì)程度。

該選項(xiàng)可以通過執(zhí)行 pytest -v 命令來使用,代碼示例如下:

$ pytest -v
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract PASSED                                              [100%]

============================ 2 passed in 0.03s =============================

4.--quiet, -q:減少測試結(jié)果輸出的詳細(xì)程度。

該選項(xiàng)可以通過執(zhí)行 pytest -q 命令來使用,代碼示例如下:

$ pytest -q
..
2 passed in 0.03s

5.--traceconfig:顯示解析和加載配置文件時的詳細(xì)信息。

該選項(xiàng)可以通過執(zhí)行 pytest --traceconfig 命令來使用,代碼示例如下:

 $ pytest --traceconfig
using: pytest-5.0.1, py-1.8.0, pluggy-0.12.0
active plugins:
  * pytest_cov-2.7.1 at /Users/user/anaconda3/envs/test/lib/python3.6/site-packages/pytest_cov/plugin.py
disabled plugins:
collector: <_pytest.main.Session object at 0x107cbe3c8>

6.--pdb:當(dāng)測試失敗或者發(fā)生異常時,進(jìn)入 Python 調(diào)試器。

該選項(xiàng)可以通過執(zhí)行 pytest --pdb 命令來使用,在測試用例出現(xiàn)錯誤時會自動進(jìn)入 pdb 調(diào)試器,代碼示例如下:

$ pytest --pdb
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract FAILED                                               [100%]

=================================== FAILURES ===================================
________________________________ test_subtract ________________________________

    def test_subtract():
        assert subtract(4, 3) == 1
>       assert subtract(2, 3) == -1
E       assert 2 == -1
E        +  where 2 = subtract(2, 3)

test_example.py:10: AssertionError
----------------------------- PDB -----------------------------
> /Users/user/example/test_example.py(10)test_subtract()
...
(Pdb) 

7.--capture:設(shè)置 pytest 的標(biāo)準(zhǔn)輸出流捕獲方式,有三個值可選:sys、fd、no。

該選項(xiàng)可以通過執(zhí)行 pytest --capture=sys 命令來使用,將標(biāo)準(zhǔn)輸出流和標(biāo)準(zhǔn)錯誤輸出重定向到 pytest 的日志系統(tǒng)中,代碼示例如下:

$ pytest --capture=sys
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract FAILED                                               [100%]

=================================== FAILURES ===================================
________________________________ test_subtract ________________________________

    def test_subtract():
        assert subtract(4, 3) == 1
>       assert subtract(2, 3) == -1
E       assert 2 == -1
E        +  where 2 = subtract(2, 3)

test_example.py:10: AssertionError

[Capture] capturing logcall output to 'log': sys

8.--norecursedirs:設(shè)定哪些目錄不搜索測試用例。

該選項(xiàng)可以通過執(zhí)行 pytest --norecursedirs=examples 命令來使用,將 examples 目錄從搜索測試用例的路徑中排除,代碼示例如下:

$ pytest --norecursedirs=examples
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract PASSED                                              [100%]

============================ 2 passed in 0.03s =============================

9.--rootdir:設(shè)置 pytest 的根目錄。

該選項(xiàng)可以通過執(zhí)行 pytest --rootdir=/path/to/project 命令來使用,將 pytest 的根目錄設(shè)置為 /path/to/project,代碼示例如下:

$ pytest --rootdir=/path/to/project
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract PASSED                                              [100%]

============================ 2 passed in 0.03s =============================

10.--maxfail=n:設(shè)定在第 n 次測試失敗后停止測試執(zhí)行。

該選項(xiàng)可以通過執(zhí)行 pytest --maxfail=1 命令來使用,當(dāng)?shù)谝粭l測試用例失敗時會停止測試執(zhí)行,代碼示例如下:

$ pytest --maxfail=1
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract FAILED                                               [100%]

=================================== FAILURES ===================================
________________________________ test_subtract ________________________________

    def test_subtract():
        assert subtract(4, 3) == 1
>       assert subtract(2, 3) == -1
E       assert 2 == -1
E        +  where 2 = subtract(2, 3)

test_example.py:10: AssertionError

========================== 1 failed, 1 passed in 0.04s ==========================

11.--junit-xml=PATH:將測試結(jié)果輸出到 JUnit XML 文件中。

該選項(xiàng)可以通過執(zhí)行 pytest --junit-xml=test_results.xml 命令來使用,將測試結(jié)果輸出到 test_results.xml 文件中,代碼示例如下:

$ pytest --junit-xml=test_results.xml
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract PASSED                                              [100%]

============================ 2 passed in 0.03s =============================

12.path:指定測試目錄或文件,可以是相對路徑或絕對路徑。例如:pytest tests/。

該選項(xiàng)可以通過執(zhí)行 pytest tests/ 命令來使用,只運(yùn)行 tests 目錄下的測試用例,代碼示例如下:

$ pytest tests/
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract PASSED                                              [100%]

============================ 2 passed in 0.03s =============================

13.-m:選擇特定標(biāo)記的測試用例進(jìn)行執(zhí)行。例如:pytest -m "slow"。

該選項(xiàng)可以通過在測試用例中使用 @pytest.mark 標(biāo)記來進(jìn)行使用,例如:

import pytest
 
@pytest.mark.slow
def test_slow():
    pass
 
def test_not_slow():
    pass

然后執(zhí)行 pytest -m "slow" 命令來只運(yùn)行標(biāo)有 @pytest.mark.slow 標(biāo)記的測試用例,代碼示例如下:

$ pytest -m "slow"
test_example.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.slow - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
  @pytest.mark.slow
no tests ran in 0.00s

PytestUnknownMarkWarning: Unknown pytest.mark.slow - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html

14.-k:選擇包含某個關(guān)鍵字的測試用例進(jìn)行執(zhí)行。例如:pytest -k "add or subtract"。

該選項(xiàng)可以通過執(zhí)行 pytest -k "add or subtract" 命令來使用,只運(yùn)行測試用例名稱中包含 "add" 或者 "subtract" 字符串的測試用例,代碼示例如下:

$ pytest -k "add or subtract"
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract PASSED                                              [100%]

============================ 2 passed in 0.03s =============================

15.-x:遇到一條測試用例失敗就停止測試執(zhí)行。

該選項(xiàng)可以通過執(zhí)行 pytest -x 命令來使用,在第一條測試用例失敗時會停止測試執(zhí)行,代碼示例如下:

$ pytest -x
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract FAILED                                               [100%]

=================================== FAILURES ===================================
________________________________ test_subtract ________________________________

    def test_subtract():
        assert subtract(4, 3) == 1
>       assert subtract(2, 3) == -1
E       assert 2 == -1
E        +  where 2 = subtract(2, 3)

test_example.py:10: AssertionError

========================== 1 failed, 1 passed in 0.04s ==========================

16.--lf:只重跑上次測試失敗的測試用例。

該選項(xiàng)可以通過執(zhí)行 pytest --lf 命令來使用,在上次測試用例出現(xiàn)錯誤時會自動重新運(yùn)行失敗的測試用例,代碼示例如下:

$ pytest --lf
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract FAILED                                               [100%]

=================================== FAILURES ===================================
______________________ test_subtract[2-3] ______________________

x = 2
y = 3

    def test_subtract(x, y):
        assert subtract(x, y) == -1

tests/test_example.py:10: AssertionError
------------------------------ rerun test call -------------------------------
test_example.py::test_subtract[x

17.--ff:只重跑最后一次測試失敗的測試用例。

該選項(xiàng)可以通過執(zhí)行 pytest --ff 命令來使用,在上次測試用例出現(xiàn)錯誤時會自動重新運(yùn)行最后一個失敗的測試用例,代碼示例如下:

$ pytest --ff
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract FAILED                                               [100%]

=================================== FAILURES ===================================
________________________________ test_subtract ________________________________

    def test_subtract():
        assert subtract(4, 3) == 1
>       assert subtract(2, 3) == -1
E       assert 2 == -1
E        +  where 2 = subtract(2, 3)

test_example.py:10: AssertionError
------------------------------- Captured stdout --------------------------------
subtraction of 2 and 3 is -1

========================== 1 failed, 1 passed in 0.04s ==========================

18.--failed-first:在所有測試用例之前先運(yùn)行上次測試失敗的測試用例。

該選項(xiàng)可以通過執(zhí)行 pytest --failed-first 命令來使用,會先運(yùn)行上次測試失敗的測試用例,然后再運(yùn)行剩下的測試用例,代碼示例如下:

$ pytest --failed-first
test_example.py::test_subtract FAILED                                               [100%]
test_example.py::test_add PASSED

=================================== FAILURES ===================================
________________________________ test_subtract ________________________________

    def test_subtract():
        assert subtract(4, 3) == 1
>       assert subtract(2, 3) == -1
E       assert 2 == -1
E        +  where 2 = subtract(2, 3)

test_example.py:10: AssertionError
----------------------------- Captured stdout -----------------------------
subtraction of 2 and 3 is -1

========================== 1 failed, 1 passed in 0.03s ==========================

19.-n, --numprocesses:指定并發(fā)進(jìn)程數(shù)量。

該選項(xiàng)可以通過執(zhí)行 pytest -n 4 命令來使用,將測試用例分配到 4 個并發(fā)進(jìn)程中運(yùn)行,代碼示例如下:

$ pytest -n 4
test_example.py::test_add PASSED                                                   [ 25%]
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract PASSED                                              [ 75%]
test_example.py::test_subtract PASSED                                              [100%]

============================ 4 passed in 0.02s =============================

20.-d, --dist:啟用分布式測試。

該選項(xiàng)可以通過執(zhí)行 pytest --dist=loadfile 命令來使用,通過加載多個 Python 進(jìn)程并在所有進(jìn)程中分發(fā)測試運(yùn)行,代碼示例如下:

$ pytest --dist=loadfile
test_example.py::test_add PASSED                                                   [ 25%]
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract PASSED                                              [ 75%]
test_example.py::test_subtract PASSED                                              [100%]

============================ 4 passed in 0.02s =============================

21.--durations=n:按運(yùn)行時間對測試用例進(jìn)行排序,并顯示最慢 n 條測試用例的運(yùn)行時長。

該選項(xiàng)可以通過執(zhí)行 pytest --duration=2 命令來使用,顯示運(yùn)行時間最慢的兩條測試用例的運(yùn)行時長,代碼示例如下:

$ pytest --duration=2
test_example.py::test_add PASSED                                               [ 50%]
test_example.py::test_subtract PASSED                                          [100%]

======================== slowest 2 test durations =========================
0.01s call     test_example.py::test_subtract
0.01s call     test_example.py::test_add
============================ 2 passed in 0.02s =============================

22.--show-capture:在結(jié)果中顯示測試用例標(biāo)準(zhǔn)輸出流和標(biāo)準(zhǔn)錯誤輸出。

該選項(xiàng)可以通過執(zhí)行 pytest --show-capture 命令來使用,將測試用例的標(biāo)準(zhǔn)輸出流和標(biāo)準(zhǔn)錯誤輸出顯示在測試結(jié)果中,代碼示例如下:

$ pytest --show-capture
test_example.py::test_add PASSED                                                   [ 50%]
test_example.py::test_subtract PASSED                                              [100%]

================================== Captured stdout =================================
subtraction of 4 and 3 is 1
subtraction of 2 and 3 is -1

============================ 2 passed in 0.03s =============================

以上就是 pytest 命令行選項(xiàng)的詳細(xì)介紹和相應(yīng)的代碼示例,希望可以幫助你更好地使用 pytest 進(jìn)行測試。更多相關(guān)Pytest命令行選項(xiàng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python猜解網(wǎng)站數(shù)據(jù)庫管理員密碼的腳本

    Python猜解網(wǎng)站數(shù)據(jù)庫管理員密碼的腳本

    這篇文章主要和大家分享一個Python腳本,可以實(shí)現(xiàn)猜解網(wǎng)站數(shù)據(jù)庫管理員的密碼。文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下
    2022-02-02
  • Python編程求質(zhì)數(shù)實(shí)例代碼

    Python編程求質(zhì)數(shù)實(shí)例代碼

    這篇文章主要介紹了Python編程求質(zhì)數(shù)實(shí)例代碼,對求200,1000和100000以內(nèi)的質(zhì)數(shù)進(jìn)行了測試,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • PyCharm中關(guān)于安裝第三方包的三個建議

    PyCharm中關(guān)于安裝第三方包的三個建議

    這篇文章主要介紹了PyCharm中關(guān)于安裝第三方包的三個建議,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Pytorch隨機(jī)數(shù)生成常用的4種方法匯總

    Pytorch隨機(jī)數(shù)生成常用的4種方法匯總

    隨機(jī)數(shù)廣泛應(yīng)用在科學(xué)研究,但是計(jì)算機(jī)無法產(chǎn)生真正的隨機(jī)數(shù),一般成為偽隨機(jī)數(shù),下面這篇文章主要給大家介紹了關(guān)于Pytorch隨機(jī)數(shù)生成常用的4種方法,需要的朋友可以參考下
    2023-05-05
  • python熱力圖實(shí)現(xiàn)簡單方法

    python熱力圖實(shí)現(xiàn)簡單方法

    在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于python熱力圖實(shí)現(xiàn)簡單方法,對此有興趣的朋友們可以學(xué)習(xí)下。
    2021-01-01
  • Python直接使用plot()函數(shù)畫圖的方法實(shí)例

    Python直接使用plot()函數(shù)畫圖的方法實(shí)例

    Python非常簡單而又非常強(qiáng)大,它的功能之一就是畫出漂亮的圖表,實(shí)現(xiàn)數(shù)據(jù)的可視化,下面這篇文章主要給大家介紹了關(guān)于Python直接使用plot()函數(shù)畫圖的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • Python自然語言處理之切分算法詳解

    Python自然語言處理之切分算法詳解

    這篇文章主要介紹了Python自然語言處理之切分算法詳解,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • yolov5訓(xùn)練時參數(shù)workers與batch-size的深入理解

    yolov5訓(xùn)練時參數(shù)workers與batch-size的深入理解

    最近再學(xué)習(xí)YOLOv3與YOLOv5訓(xùn)練數(shù)據(jù)集的具體步驟,幾經(jīng)波折終于實(shí)現(xiàn)了很好的效果,這篇文章主要給大家介紹了關(guān)于yolov5訓(xùn)練時參數(shù)workers與batch-size的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • Django 多語言教程的實(shí)現(xiàn)(i18n)

    Django 多語言教程的實(shí)現(xiàn)(i18n)

    這篇文章主要介紹了Django 多語言教程的實(shí)現(xiàn)(i18n),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • python GUI庫圖形界面開發(fā)之PyQt5表單布局控件QFormLayout詳細(xì)使用方法與實(shí)例

    python GUI庫圖形界面開發(fā)之PyQt5表單布局控件QFormLayout詳細(xì)使用方法與實(shí)例

    這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5布局控件QFormLayout詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下
    2020-03-03

最新評論