Skip to content

Instantly share code, notes, and snippets.

@dzen
Created June 13, 2013 08:30
Show Gist options
  • Save dzen/5772121 to your computer and use it in GitHub Desktop.
Save dzen/5772121 to your computer and use it in GitHub Desktop.
Use unittest.mock.Mock (python 3.3) with factoryboy
import factory
import string
import random
from unittest.mock import Mock
class ThingFactory(factory.Factory):
FACTORY_FOR = Mock
number = factory.Sequence(lambda n: n, type=int)
string = factory.Sequence(lambda _:''.join(random.choice(string.ascii_uppercase) for x in range(4)))
date = factory.Sequence(lambda _: datetime.datetime.utcnow())
In [1]: from factories import ThingFactory
In [2]: thing = ThingFactory()
In [3]: thing
Out[3]: <Mock id='4367330448'>
In [4]: thing.date
Out[4]: datetime.datetime(2013, 6, 13, 8, 4, 0, 659616)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment