0

我在 conftest.py 中创建一个对象并在一些固定装置中使用它。我还需要在我的测试模块中使用这个对象。目前我正在我的测试模块中导入 conftest.py 并使用那个“助手”对象。我很确定这不是推荐的方式。我期待着您的建议。

谢谢 :)

以下是我的问题的虚拟编码版本:

conftest.py

import pytest

class Helper():

    def __init__(self, img_path:str):
        self.img_path = img_path

    def grayscale(self):
        pass

    def foo(self):
        pass

helper = Helper("sample.png")

@pytest.fixture()
def sample():
    return helper.grayscale()

测试模块.py

import conftest

helper = conftest.helper

def test_method1(sample):
    helper.foo()
    ...
4

1 回答 1

0

正如已经评论的那样,如果我在测试中有一个帮助类,我之前也通过固定装置处理了这些场景。

conftest.py

import pytest


class Helper():
    def __init__(self, img_path: str):
        self.img_path = img_path

    def grayscale(self):
        pass

    def foo(self):
        pass


@pytest.fixture(scope="session")
def helper():
    return Helper("sample.png")


@pytest.fixture()
def sample(helper):
    return helper.grayscale()

测试模块.py

def test_method1(helper, sample):
    helper.foo()
于 2021-08-12T03:28:33.423 回答