在python和bdd阶段之间传输某些状态的最佳实践是什么?

2024-04-29 02:29:09 发布

您现在位置:Python中文网/ 问答频道 /正文

所有人。当我开始用bdd风格编写一些测试时,有一个问题是如何在python3.x上的bdd(pytest\u bdd)中的阶段之间传递测试状态。我一直在寻找答案,找到了一个关于固定装置和字典的答案。某种。。。你知道吗

@pytest.fixture
def data_through_stages():
    return {}

但还有更多的答案,比如一些仓库类的固定装置和对象。。。你知道吗

@pytest.fixture
def context():
    class Context(object):
        pass

    return Context()

或者类与一些测试阶段的实现和转移到子测试类的功能,如。。。你知道吗

class AllSteps:

    @classmethod
    def given_step(cls):
        print("step 1")

    @classmethod
    def when_step(cls):
        print("step 2")

    @classmethod
    def then_step(cls):
        print("step 3")



class TestErrors(AllSteps):


    @staticmethod
    @given("some given discrption")
    def given():
        print(f"given step: {TestErrors.given_step()}")

    @staticmethod
    @when("some when discrption")
    def when(error):
        print("when step ", TestErrors.when_step())


    @staticmethod
    @then("some then discrption")
    def then():
        print("then step ", TestErrors.then_step())

    @staticmethod
    @scenario("./features/some_feature.feature", "some scenarion description")
    @pytest.mark.parametrize("error", [
        "no response",
        "internal error"
    ])
    def test_error(error):
        print(f"test error: {error}")

最后的主要问题是哪种解决方案最好? 关于pytest\u bdd中阶段间状态转移的思想,还有一些附加问题。我听说这是一种糟糕的代码风格吗?如果是为什么?你知道吗


Tags: 答案pytestdefsteperrorsomebddgiven