https://github.com/camilamaia/factory-boy-best-practices
factory_boy best practices
https://github.com/camilamaia/factory-boy-best-practices
best-practices django factory-boy python python3
Last synced: about 1 month ago
JSON representation
factory_boy best practices
- Host: GitHub
- URL: https://github.com/camilamaia/factory-boy-best-practices
- Owner: camilamaia
- License: mit
- Created: 2021-06-17T18:21:04.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-07T20:49:14.000Z (over 2 years ago)
- Last Synced: 2024-10-19T02:29:02.800Z (7 months ago)
- Topics: best-practices, django, factory-boy, python, python3
- Language: Python
- Homepage:
- Size: 103 KB
- Stars: 50
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# factory_boy Best Practices
1. Factories should represent their models
1. 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 implicit1. 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.1. Build over create
* MyFactory.build(): creates a local object (memory)
* MyFactory.create(): creates a local object + stores it in the DB1. 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 factory1. Use fixtures to wrap factories to avoid duplication
1. Avoid sharing factories or fixtures among different files
1. get_or_create should be used only for unique keys