小编pil*_*lls的帖子

链接测试并将对象从一个测试传递到另一个

我正在尝试将一个测试的结果传递给pytest中的另一个测试-或更具体地说,重用第二个测试中由第一个测试创建的对象。这就是我目前的做法。

@pytest.fixture(scope="module")
def result_holder:
    return []

def test_creation(result_holder):
    object = create_object()
    assert object.status == 'created' # test that creation works as expected
    result_holder.append(object.id) # I need this value for the next test

# ideally this test should only run if the previous test was successful
def test_deletion(result_holder):
    previous_id = result_holder.pop()
    object = get_object(previous_id) # here I retrieve the object created in the first test
    object.delete()
    assert object.status == 'deleted' # test for deletion
Run Code Online (Sandbox Code Playgroud)

(在进一步介绍之前,我知道py.test将一项测试的结果传递给另一项 -但该问题的单个答案是题外话,问题本身已有 2年历史了)

像这样使用fixtures感觉不是很干净……而且如果第一个测试失败,行为也不清楚(尽管可以通过测试fixture的内容或在pytest doc中使用增量fixture来纠正)以及下面的评论)。是否有更好/更规范的方法来做到这一点?

python pytest

6
推荐指数
1
解决办法
2317
查看次数

标签 统计

pytest ×1

python ×1