【发布时间】:2020-12-16 20:24:10
【问题描述】:
我正在开发 pytest API 自动化项目,我需要从数据库中获取随机产品。有没有一种方法可以让我在课堂上的所有测试用例中使用相同的随机产品?我正在使用设置类方法,但每次测试都会生成不同的产品。谢谢。
class TestCreateOrdersSmoke:
@classmethod
def setup(cls):
cls.products_db = ProductsDao()
cls.orders_db = OrdersDao()
cls.orders_helper = OrdersHelper()
@pytest.mark.tcid48
def test_create_order_as_guest(self):
random_product = self.products_db.select_random_product_from_db()
random_product_id = random_product[0]['ID']
更新:
所以我使用了像 seggested 这样的 pytest 会话夹具,它可以工作,所以谢谢!但我想确保这是正确的做法,所以这里是更新的代码:
class TestCreateOrdersSmoke:
@pytest.fixture(scope="session")
def helpers(self):
products_db = ProductsDao()
orders_db = OrdersDao()
orders_helper = OrdersHelper()
random_product = products_db.select_random_product_from_db()
yield {'products_db':products_db,
'orders_db':orders_db,
'orders_helper':orders_helper,
'random_product':random_product}
@pytest.mark.tcid48
def test_create_order_as_guest(self, helpers):
random_product = helpers['random_product']
random_product_id = random_product[0]['ID']
@pytest.mark.tcid88
def test_create_order_with_new_user(self, helpers):
random_product = helpers['random_product']
random_product_id = random_product[0]['ID']
【问题讨论】:
标签: python unit-testing automated-tests pytest