我为基于pytest的测试实现了scenario support,并且效果很好。
但是,我的固定装置之一使用干净的表初始化数据库,第二种方案使用脏的数据库运行测试。在后续场景中如何重新初始化或刷新数据库设备?
为清楚起见,我想看看这个:
test_demo1
得到一个新的数据库,测试将写入该数据库test_demo2
不会重新初始化数据库,但是会看到test_demo1
所做的更改test_demo1
再次获得一个新的数据库,测试将写入该数据库test_demo2
不会重新初始化数据库,而是仅在场景2中看到test_demo1
所做的更改。def pytest_runtest_setup(item):
if hasattr(item.cls, "scenarios") and "first" in item.keywords:
# what to do here?
@pytest.mark.usefixtures("db")
class TestSampleWithScenarios:
scenarios = [scenario1, scenario2]
@pytest.mark.first
def test_demo1(self, db):
# db is dirty here in scenario2
pass
def test_demo2(self, db):
pass
我目前正在研究pytest的来源以找到答案,一旦有发现,我将在这里发布。
答案 0 :(得分:1)
我找到了一种解决方法。进行一个常规测试,该测试使用DB夹具parametrize
和场景,并直接在类中调用测试:
@pytest.mark.parametrize(["scenario"], scenarios)
def test_sample_with_scenarios(db, scenario):
TestSampleWithScenarios().test_demo1(db, scenario)
TestSampleWithScenarios().test_demo2(db, scenario)