在运行测试之前,在Django中创建数据库条目 [英] Create a Database Entry in Django Before Running Tests

查看:47
本文介绍了在运行测试之前,在Django中创建数据库条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ubuntu 13.04上使用Django 1.4和Python 2.7.我正在研究的Django项目具有使用Nose和Coverage的整个测试套件.在运行任何测试之前,我需要在数据库中创建一个用户.我在哪里可以做?

I'm using Django 1.4 with Python 2.7 on Ubuntu 13.04. The Django project I'm working on has an entire test suite using Nose and Coverage. I need to create a user in the database prior to any tests being run. Where would I do this?

system_user = User(username=accounts_constants.SYSTEM_USER)
system_user.save()

这样做的动机源于存在一个系统机器人",该机器人可以处理许多数据库事务.我在我们的项目中创建了一个完整的跟踪套件.如果用户更改了任何敏感信息,我们将对其进行跟踪.如果系统进行了这些更改,我们希望在跟踪信息中列出系统用户.

The motivation for this stems from the existence of a "system robot" that handles a lot of the database transactions. I have created a full tracking suite within our project. If a user changes any sensitive information we track it. If the system makes those changes we want the system user to be listed in the tracking information.

代码中有几个地方需要修改以支持此更改.显然,测试数据库没有该用户.我想在运行任何测试之前(但是在创建数据库之后)添加它.

There are several places in the code that needed to be modified to support this change. Obviously the test database doesn't have this user. I would like to add it prior to any tests running (but after the database is created).

我一直在四处搜寻,但无法确切看到在何处创建数据库以进行测试.我的第一次尝试是在测试套件运行程序的 init 中.

I've been digging around but can't see exactly where the database is created for testing purposes. My first attempt was in the init of our test suite runner.

class CustomTestSuiteRunner(NoseTestSuiteRunner):
    """
    Runs testing suite and adds code coverage report.
    """
    def __init__(self, *args, **kwargs):
        super(CustomTestSuiteRunner, self).__init__(*args, **kwargs)
        self.coverage = coverage.coverage()
        self.coverage.use_cache(0) # don't use caching with coverage.py
        system_user = User(username=accounts_constants.SYSTEM_USER)
        system_user.save()

之所以失败,是因为它仍在使用开发数据库(该数据库上已经存在系统用户).

This failed because it was still using the development database (the system user already exists on this database).

我尝试在测试顺序中进一步添加用户,但似乎找不到合适的位置.

I tried adding the user further along in the testing sequence but can't seem to find where the appropriate place would be.

任何方向将不胜感激.

从girasquid的建议解决方案中:

From girasquid's suggested solution:

我不想修改每个 setUp 以取代 CustomTestCase .相反,我试图在 __ init __ 中做到这一点.

I don't want to modify each setUp to super to the CustomTestCase. Instead I am trying to do it in __init__.

class CustomTestCase(TestCase):
    def __init__(self, *args, **kwargs):
        super(CustomTestCase, self).__init__(*args, **kwargs)
        try:
            system_user = User.objects.create(
                username=accounts_constants.SYSTEM_USER)
            system_user.save()
        except psycopg2.IntegrityError:
            pass

但是我从 nose 收到了 IntegrityError .任何想法为什么会这样?

But I am getting an IntegrityError from nose. Any ideas why this might be?

lib/python2.7/site-packages/nose/loader.py", line 518, in makeTest
    return self._makeTest(obj, parent)
IntegrityError: duplicate key value violates unique constraint "auth_user_username_key"

我们的团队说服了我,我要实施一个逐案的setUp更改,以包括用户而不是整个系统范围的更改.

I was convinced by our team to implement a case-by-case setUp change to include the user rather than a system-wide change.

推荐答案

我将定义在我的测试用例上="=" nofollow> setUp :

class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.system_user = User.objects.create(username=accounts_constants.SYSTEM_USER)

如果您需要执行许多测试用例,将其放在基础 TestCase 上,然后从所有测试中继承下来可能会很有用.

If you have a lot of test cases you need to do this for, it might be useful to put this on a base TestCase and then inherit from that for all your tests.

这篇关于在运行测试之前,在Django中创建数据库条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆