What's the recommended (painless) way for setting up data for unit testing Page-derived models?
See original GitHub issueWagtail uses a fixture to set up its test data, which is great for core code which can depend on a set number of Page-derived models for testing. I’ve tried using fixtures to generate data for a testserver but I find that they’re very brittle and hard to work with. Getting a fixture which will load correctly into loaddata
is already a pain because of dynamically-generated fields like contenttypes, so my fixtures are created with something like:
python manage.py dumpdata --exclude contenttypes --exclude auth.Permission --exclude sessions --exclude admin --natural --format=yaml --indent=4 > app/fixtures/test-fixture.yaml
Whenever adding new models or fields, however, those new contenttype entries throw everything off and old fixtures become invalid to loaddata
. You’d have to create a new testing fixture every time the schema changes. Developing a Wagtail site means you’re pretty rapidly moving through schema changes while building out models, so keeping the test fixtures current becomes unworkable.
So I tabled that problem and looked into Factory Boy to create factory models to generate only the objects I need for a unit test. I can’t get that to work because factories use .create()
to make objects and the Page class or one of the classes it inherits from uses a custom manager (MP_Node
from treebeard?).
Am I missing something? I’m new to testing but I feel like I’ve tried some commonly recommended ideas in the Django community for managing complex testing data and the obstacles I’ve butted up against are heavily-technical edge cases that will put testing off the table for most Wagtail users.
Issue Analytics
- State:
- Created 9 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
@nkuttler Hah, nope. Sorry.
Here’s some more proof-of-concept code from before I got distracted and abandoned testing. It shows the use of Factory Boy’s fuzzy methods to generate more realistic-looking data. I needed to generate lots of mock content to fill out a website for design purposes, but it might be handy for testing, too.
Thank you @kaedroho @gasman .
I got hung up on an API change in Factory Boy that was in their docs but not yet in pip. After several hours of rewarding work to realize that fact 😃 , I was able to get Wagtail models working correctly with Factory Boy (below). I haven’t fully explored django-treebeard, but this sort of tree setup should suffice for alot of use cases. When I’ve had a chance to write a test suite for my project and feel like I have a handle on what all the caveats are, I’ll write up a doc detailing the usage.