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

问题描述 投票:1回答:1

我正在使用Pytest和Selenium开发一些测试。我创建了一个为帐户创建生成虚假帐户数据的功能。以下是页面(目录页面)

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")
      ....

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


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

python pytest faker
1个回答
1
投票

我通过在每个测试中添加“fake.random.seed()”来实现我想要的目标(不确定它是否是最好的方式,但它对我有用)

© www.soinside.com 2019 - 2024. All rights reserved.