即使使用正确的命名约定,Pytest 也不会选择测试

Pytest not picking up tests even with correct naming convention

我正在尝试测试 class,我在 class 中使用存储在 conftest.py 中的另一个函数对 class 中的变量进行 monkeypatch,但检测到测试的 none即使使用前缀 test_ 到测试名称和文件名的正确命名约定。

这是 tests 目录根目录中的 conftest.py

import pytest
from pathlib import Path
import spacy

# Pass path to staticdata to any test
@pytest.fixture(scope="session")
def get_staticdata_path():
    static_data_path = (
        Path(
            __file__,
        )
        .resolve()
        .parent
        / ".."
        / ".."
        / "staticdata"
    )
    return static_data_path

# Pass function that loads a spacy model based on data in staticdata 
# instead of downloading it everytime
@pytest.fixture(scope="session")
def load_spacy_model():
    static_data_path = get_staticdata_path()
    model_path = static_data_path / "en_core_web_sm"
    nlp = spacy.load(model_path)
    return nlp

这是未被提取用于测试的文件,它位于 tests/natural_language_processing/test_utils.py

import pytest
from omdenalore.natural_language_processing.utils import TextUtils


@pytest.fixture(autouse=True)
@pytest.mark.parametrize(
    "text,expected,exception",
    [
        # No inputs
        (None, None, Exception),
        (
            "This is a sample sentence, showing off the stop words filtration.",
            "This sample sentence, showing stop words filtration.",
            None,
        ),
    ],
)
def test_stop_word_removal(text, expected, exception, monkeypatch):
    monkeypatch.setattr(TextUtils.stop_word_removal, "nlp", load_spacy_model)
    with pytest.raises(exception):
        result = TextUtils.stop_word_removal(text)
        assert result == expected

当我在根目录 运行 pytest 时输出:

============================================================= test session starts =============================================================
platform darwin -- Python 3.7.11, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /Users/yravindranath/OmdenaLore
collected 32 items                                                                                                                            

tests/computer_vision/test_IoU.py ...                                                                                                   [  9%]
tests/computer_vision/test_MSE.py ...                                                                                                   [ 18%]
tests/computer_vision/test_PSNR.py ...                                                                                                  [ 28%]
tests/computer_vision/test_RMSE.py ...                                                                                                  [ 37%]
tests/computer_vision/test_SSIM.py ...                                                                                                  [ 46%]
tests/datasets/test_download_datasets.py .....                                                                                          [ 62%]
tests/optical_character_recognition/test_pytesseract_ocr.py ............                                                                [100%]

test_stop_word_removal 被声明为固定装置 (@pytest.fixture(autouse=True))。好像是没有被检测为测试的原因。

装饰器 @pytest.fixture(autouse=True) 声明了一个将被所有测试自动请求 的夹具,参见pytest fixtures

这里是使用夹具声明测试方法的方式load_spacy_model

def test_stop_word_removal(text, expected, exception, monkeypatch, load_spacy_model):
    monkeypatch.setattr(TextUtils.stop_word_removal, "nlp", load_spacy_model)
    # ...