Pytest & Faker |在每个测试中使用相同的数据

Pytest & Faker | Using same data in each test

我正在使用 Pytest 和 Selenium 开发一些测试。 我创建了一个函数,可以为帐户创建生成虚假帐户数据。 以下是进入页面(catalogpage)

def fake_account(self, fname, lname, userid, pwd, cpwd):
    self._type(self._fname, fname)
    self._type(self._last_name, lname)
    self._type(self._user_id, userid)
    self._type(self._password, pwd)
    self._type(self._password_confirm, cpwd)

上面的 _type 方法是这样定义到我的基本页面中的:

    def _type(self, locator, input_text):
        self._find(locator).send_keys(input_text)

我正在调用方法 fake_account 进入多个测试,进入另一个页面,如下所示:

class TestFakeAccounts():

      @pytest.fixture()
      def catalogpage(self, driver):
          return catalogpage.CatalogPage(driver)


      def test_account1(self, catalogpage):
          catalogpage.fake_account(fake.name(), "Auth", fake.user_name(), "PassWord_123", "PassWord_123")
      ....
      def test_account2(self, catalogpage):
          catalogpage.fake_account(fake.name(), "Auth", fake.user_name(), "PassWord_123", "PassWord_123")
      ....

当我 运行 测试时(在测试文件夹中使用 pytest),它将收集我所有的测试,但对整个会话(对于我的所有测试)使用相同的生成的假数据。 有没有办法设置这个或 pytest 夹具,以便它会在同一会话期间为每个测试生成新的假数据?


稍后更新(如果有人需要这个):我能够通过在每个测试中添加 "fake.random.seed()" 来实现我想要的(不确定这是否是最好的方法,但它对我有用)

我可以通过在每个测试中添加 "fake.random.seed()" 来实现我想要的(不确定这是否是最好的方法,但它对我有用)