测试时不要加载'initial_data.json'夹具

时间:2022-09-20 13:40:56

I'm testing a django app not written by myself, which uses two fixtures: initial_data.json and testing.json. Both fixtures files contain conflicting data (throwing an integrity error).

我正在测试一个不是由我自己编写的django应用程序,它使用两个灯具:initial_data.json和testing.json。两个灯具文件都包含冲突的数据(抛出完整性错误)。

For testing, I've specified TestCase.fixtures = ['testing.json'], but initial_data.json is loaded too.

为了测试,我已经指定了TestCase.fixtures = ['testing.json'],但是也加载了initial_data.json。

How can I avoid loading initial_data.json (not renaming it) in the testcase?

如何避免在测试用例中加载initial_data.json(不重命名)?

3 个解决方案

#1


6  

Quoting from Django Website:

引自Django网站:

If you create a fixture named initial_data.[xml/yaml/json], that fixture will be loaded every time you run syncdb. This is extremely convenient, but be careful: remember that the data will be refreshed every time you run syncdb. So don't use initial_data for data you'll want to edit.

如果您创建名为initial_data。[xml / yaml / json]的夹具,则每次运行syncdb时都会加载该夹具。这非常方便,但要小心:请记住,每次运行syncdb时都会刷新数据。因此,不要将initial_data用于您想要编辑的数据。

So I guess there's no way to say "okay, don't load initial data just this once". Perhaps you could write a short bash script that would rename the file. Otherwise you'd have to dig into the Django code.

所以我想没有办法说“好吧,不要只加载一次初始数据”。也许你可以编写一个重命名文件的短bash脚本。否则你必须深入研究Django代码。

More info here: http://docs.djangoproject.com/en/dev/howto/initial-data/#automatically-loading-initial-data-fixtures

更多信息:http://docs.djangoproject.com/en/dev/howto/initial-data/#automatically-loading-initial-data-fixtures

#2


2  

You might want to think about whether initial_data.json is something your app actually needs. It's not hard to "manually" load your production data with ./manage.py loaddata production.json after running a syncdb (how often do you run syncdb in production, anyway?), and it would make loading your testing fixture much easier.

您可能想要考虑一下您的应用实际需要的是initial_data.json。在运行syncdb之后,使用./manage.py loaddata production.json“手动”加载生产数据并不难(无论如何,你在生产中运行syncdb的频率如何?),这样可以更轻松地加载测试夹具。

#3


2  

If you want to have tables with no initial data, this code will help you:

如果您希望表中没有初始数据,此代码将帮助您:

edit tests.py:

编辑tests.py:

from django.core import management

class FooTest(TestCase):

    @classmethod
    def setUpClass(cls):
        management.call_command('flush', interactive=False, load_initial_data=False)

this will remove your data and syncdb again without loading initial data.

这将在不加载初始数据的情况下再次删除您的数据和syncdb。

#1


6  

Quoting from Django Website:

引自Django网站:

If you create a fixture named initial_data.[xml/yaml/json], that fixture will be loaded every time you run syncdb. This is extremely convenient, but be careful: remember that the data will be refreshed every time you run syncdb. So don't use initial_data for data you'll want to edit.

如果您创建名为initial_data。[xml / yaml / json]的夹具,则每次运行syncdb时都会加载该夹具。这非常方便,但要小心:请记住,每次运行syncdb时都会刷新数据。因此,不要将initial_data用于您想要编辑的数据。

So I guess there's no way to say "okay, don't load initial data just this once". Perhaps you could write a short bash script that would rename the file. Otherwise you'd have to dig into the Django code.

所以我想没有办法说“好吧,不要只加载一次初始数据”。也许你可以编写一个重命名文件的短bash脚本。否则你必须深入研究Django代码。

More info here: http://docs.djangoproject.com/en/dev/howto/initial-data/#automatically-loading-initial-data-fixtures

更多信息:http://docs.djangoproject.com/en/dev/howto/initial-data/#automatically-loading-initial-data-fixtures

#2


2  

You might want to think about whether initial_data.json is something your app actually needs. It's not hard to "manually" load your production data with ./manage.py loaddata production.json after running a syncdb (how often do you run syncdb in production, anyway?), and it would make loading your testing fixture much easier.

您可能想要考虑一下您的应用实际需要的是initial_data.json。在运行syncdb之后,使用./manage.py loaddata production.json“手动”加载生产数据并不难(无论如何,你在生产中运行syncdb的频率如何?),这样可以更轻松地加载测试夹具。

#3


2  

If you want to have tables with no initial data, this code will help you:

如果您希望表中没有初始数据,此代码将帮助您:

edit tests.py:

编辑tests.py:

from django.core import management

class FooTest(TestCase):

    @classmethod
    def setUpClass(cls):
        management.call_command('flush', interactive=False, load_initial_data=False)

this will remove your data and syncdb again without loading initial data.

这将在不加载初始数据的情况下再次删除您的数据和syncdb。