簡體   English   中英

pytest 可以在測試類中運行測試嗎?

[英]Can pytest run tests within a test class?

我有一堆測試,我決定放在一個類中,示例代碼如下:

class IntegrationTests:

    @pytest.mark.integrationtest
    @pytest.mark.asyncio
    async def test_job(self):
        assert await do_stuff()

但是,當我嘗試運行測試時: pipenv run pytest -v -m integrationtest ,根本沒有檢測到它們,在將它們移到類之前我得到了以下信息:

5 passed, 4 deselected in 0.78 seconds

我現在明白了:

2 passed, 4 deselected in 0.51 seconds

為什么pytest沒有檢測到這些測試? 不支持測試類嗎?

類的名稱需要以Test開頭,以便 pytest 發現才能找到它。

class TestIntegration:

    @pytest.mark.integrationtest
    @pytest.mark.asyncio
    async def test_job(self):
        assert await do_stuff()

請參閱Python 測試發現的約定

創建一個 pytest.ini

文檔

如果您需要更改測試文件、類和測試的命名約定,您可以創建一個文件pytest.ini ,並設置選項python_filespython_classespython_functions

例子:

# content of pytest.ini
# Example 1: have pytest look for "check" instead of "test"
# can also be defined in tox.ini or setup.cfg file, although the section
# name in setup.cfg files should be "tool:pytest"
[pytest]
python_files = check_*.py
python_classes = *Tests
python_functions = *_check

在您的情況下,如果您不想更改類IntegrationTests的名稱,請將python_classes設置為*Tests

在類中運行測試

pytest /path/to/test_file_name.py::ClassName

在類中運行測試

pytest /path/to/test_file_name.py::ClassName::test_name

要運行類下的所有測試,您可以使用“TestIntegration”:

pytest -k TestIntegration

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM