Questions tagged [factory-boy]

A test fixtures replacement for Python based on thoughtbot's factory_girl for Ruby (Python).

A test fixtures replacement for Python based on thoughtbot's factory_girl for Ruby (Python).

See project page.

344 questions
37
votes
5 answers

How to use Faker from Factory_boy

Factory_boy uses fake-factory (Faker) to generate random values, I would like to generate some random values in my Django tests using Faker directly. Factory_boy docs suggests using factory.Faker and its provider as : class…
marcanuy
  • 23,118
  • 9
  • 64
  • 113
36
votes
6 answers

How Can You Create an Admin User with Factory_Boy?

I'm a relative Django beginner and just started doing some testing for my projects. What I want to do is build a functional test with selenium that logs into the Django Admin site. I first followed this tutorial…
Brian Fabian Crain
  • 860
  • 2
  • 8
  • 16
25
votes
5 answers

Factory Boy random choice for a field with field option "choices"

When a field in a Django model has the option choices, see Django choices field option, it utilises an iterable containing iterables of 2 items to define which values are allowed. For example: Models class IceCreamProduct(models.Model): …
Robin
  • 519
  • 1
  • 7
  • 13
25
votes
2 answers

factory boy: define field that depends on other field

How to define a field that depends on other field using factory-boy? For instance, I'd like to define an email that depends on the first name and last name of an User. I tried using the post_generation decorator. However, my system requires the…
jsmedmar
  • 1,025
  • 10
  • 21
25
votes
5 answers

Is passing too many arguments to the constructor considered an anti-pattern?

I am considering using the factory_boy library for API testing. An example from the documentation is: class UserFactory(factory.Factory): class Meta: model = base.User first_name = "John" last_name = "Doe" For this to work, we…
skamsie
  • 2,614
  • 5
  • 36
  • 48
22
votes
5 answers

Avoiding duplicates with factory_boy factories

I'm using factory_boy to create test fixtures. I've got two simple factories, backed by SQLAlchemy models (simplified below). I'd like to be able to call AddressFactory.create() multiple times, and have it create a Country if it doesn't already…
Jim Stewart
  • 16,964
  • 5
  • 69
  • 89
21
votes
1 answer

Why is factory_boy superior to using the ORM directly in tests?

I don't see why factory_boy is preferred over creating ORM/model instances directly in Django tests. And the factory_boy website does little to explain the benefits of using it. It makes sense as an alternative to fixtures, which are difficult to…
user307927
  • 748
  • 9
  • 22
20
votes
5 answers

How to test auto_now_add in django

I have django 1.11 app and I want to write unit test for my solution. I want to test registration date feature. model.py: class User(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) …
user9192656
  • 549
  • 3
  • 16
19
votes
2 answers

factory-boy create a list of SubFactory for a Factory

I am using django 1.6 and factory-boy. class UserFactory(factory.Factory): class Meta: model = models.User username = factory.Sequence(lambda n: 'user%d' % n) Here username is a simple CharField in model. So that each time I am…
Saiful Azad
  • 1,823
  • 3
  • 17
  • 27
19
votes
2 answers

Many-to-many relationship in factory_boy?

I'm trying to test a many-to-many relationship between two Django models using factory_boy. The factory_boy documentation doesn't appear to discuss this and I'm having trouble figuring out what I'm doing wrong. When I run the first test, I get the…
Jim
  • 13,430
  • 26
  • 104
  • 155
18
votes
3 answers

django factory boy factory with OneToOne relationship and related field

I am using Factory Boy to create test factories for my django app. The model I am having an issue with is a very basic Account model which has a OneToOne relation to the django User auth model (using django < 1.5): # models.py from…
darko
  • 2,438
  • 8
  • 42
  • 54
16
votes
2 answers

Passing an object created with SubFactory and LazyAttribute to a RelatedFactory in factory_boy

I am using factory.LazyAttribute within a SubFactory call to pass in an object, created in the factory_parent. This works fine. But if I pass the object created to a RelatedFactory, LazyAttribute can no longer see the factory_parent and fails. This…
Chris
  • 5,664
  • 6
  • 44
  • 55
16
votes
2 answers

Datetime Field Received a Naive Datetime

I'm running into the classic DateTimeField received a naive datetime while time zone support is active warning with a twist. The error occurs when I run tests that utilize factories provided by factory_boy. Here is an example of a factory: from…
user1427661
  • 11,158
  • 28
  • 90
  • 132
16
votes
4 answers

How to pass in a starting sequence number to a Django factory_boy factory?

factory_boy defaults to 1 for sequences. How can I pass in a number to use as a different starting number instead? I can subclass the _setup_next_sequence() method, but how can I give it a variable to use? # File: models.py from django.db import…
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
15
votes
5 answers

One-to-many relationships in factory_boy

I use SQLalchemy as my ORM and am trying to port my test fixtures to factory_boy. My schema includes two objects in a one-to-many relation. I.e. instances of one model have list like structures with instances of the other. Example: class…
NiklasMM
  • 2,895
  • 2
  • 22
  • 26
1
2 3
22 23