使用factory_boy和randint时生成重复值的问题

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

我在使用randint和factory_boy时看到生成的重复值。检查每个对象的资产时,我的测试失败。在工厂调用create()时,两者之间的条形码也是相同的。

在每个对象创建之前,我是否需要提供不同的种子?

  • 工厂男孩2.11.1
  • Faker 1.0.4
  • python 2.7
  • django 1.11

种子/ factories.py

import factory
from A1.models import *
from random import randint, random
faker = Factory.create()

class AssetFactory(factory.DjangoModelFactory):

    class Meta:
        model = Asset

asset = "ABC" + str(randint(0000, 9999))
barcode = "AA" + randint(111111, 999999)
user = factory.lazy_attribute(lambda x: faker.name())

tests.朋友

def test_randomCreation(self):
    new_asset = AssetFactory.create()
    new_asset2 = AssetFactory.create()
    self.assertNotEqual(new_asset.asset, new_asset2.asset)  #FAILS

A1 / models.py

class Asset(models.Model):
    asset =  models.CharField(max_length=255, verbose_name="Asset")
    barcode = models.CharField(max_length=255)
    user = models.CharField(max_length=255)

愿有人指出我正确的方向吗?提前致谢!!

python-2.7 faker django-1.11 factory-boy
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.