How do I run Django tests on a copy of my production database?

后端 未结 2 2171
执笔经年
执笔经年 2021-02-13 13:08

I\'ve written a series of tests for my Django app, and would like to run them on a copy of my production database.

As far as I can tell, the best way to do this is using

相关标签:
2条回答
  • 2021-02-13 13:32

    Generally, testing against the live DB or a copy of the live DB is discouraged. Why? Because tests need to be predictable. When you make a copy of the live db, the input becomes unpredictable. The second problem is that you cannot obviously test on the live site, so you need to clone the data. That's slow for anything more than few MB in size.

    Even if the DB is small, dumpdata followed by loaddata isn't the way. That's because dumpdata by default exports in a JSON format which has a large generating overhead, not to mention making the data file very bulky. Importing using loaddata is even slower.

    The only realistic way to make a clone is using the database engines built in export/import mechanism. In the case of sqlite that's just copying the db file. For mysql it's SELECT INTO OUTFILE followed by LOAD DATA INFILE. And for postgresql it's COPY TO followed by COPY FROM and so on.

    All of these export/import commands can be executed using the lowlevel connection object available in django and thus can be used to load fixtures.

    0 讨论(0)
  • 2021-02-13 13:38

    You didn't mention which version of Django you're using, but looking at the 1.11 documentation:

    • dumpdata can dump the data for all the apps from a particular database, rather than needing to do them individually.
    • loaddata can load data for multiple apps, and as well as looking in individual app directories for fixtures subdirectories, it can also look in directories defined in FIXTURE_DIRS.

    It's not clear from the 1.11 docs about fixture loading for tests whether they would also look in FIXTURE_DIRS, though. So this might not solve your problem entirely.

    0 讨论(0)
提交回复
热议问题