-
Factories should represent their models
-
Do not rely on defaults from factories
- If a default value is changed, all tests that depend on it will break
- The setup of a test should contain all the logic to ensure it will always pass
- Explicit better than implicit
-
Factories should contain only the required data
- If the field is nullable (null=True) the attribute should be under a trait and not as a default value
- If we want to have explicitly the attribute, we can use MyFactory(with_myattr=True)
- When are we going to remember to test the case MyFactory(myattr=None)?
- We should not assume there is an author when DB actually allows to not have it.
-
Build over create
- MyFactory.build(): creates a local object (memory)
- MyFactory.create(): creates a local object + stores it in the DB
-
If FK is in the table: SubFactor
If FK is in the other table: RelatedFactory + trait
- SubFactory: builds/creates the SubFactory during the process of creation of the main factory
- RelatedFactory: builds/creates the RelatedFactory after creating the main factory
-
Use fixtures to wrap factories to avoid duplication
-
Avoid sharing factories or fixtures among different files
-
get_or_create should be used only for unique keys
-
Notifications
You must be signed in to change notification settings - Fork 4
factory_boy best practices
License
camilamaia/factory-boy-best-practices
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
factory_boy best practices