【发布时间】:2011-04-05 19:02:41
【问题描述】:
在我的测试中,我不仅测试完美情况,还特别测试边缘情况和错误情况。所以我想确保一些唯一性约束有效。
虽然我的测试和测试装置非常复杂,但我能够将问题追溯到以下示例,该示例不使用任何自定义模型。要重现该行为,只需将代码保存到 tests.py 并运行 django 测试运行程序。
from django.contrib.auth.models import User
from django.db import IntegrityError
from django.test import TransactionTestCase
class TransProblemTest(TransactionTestCase):
def test_uniqueness1(self):
User.objects.create_user(username='user1', email='user1@example.com', password='secret')
self.assertRaises(IntegrityError, lambda :
User.objects.create_user(username='user1', email='user1@example.com', password='secret'))
def test_uniqueness2(self):
User.objects.create_user(username='user1', email='user1@example.com', password='secret')
self.assertRaises(IntegrityError, lambda :
User.objects.create_user(username='user1', email='user1@example.com', password='secret'))
具有单个测试方法的测试类可以工作,但由于两个相同的方法实现而失败。 第一个测试抛出异常会破坏 Django 测试环境,使后面的所有测试都失败。
我将 Django 1.1 与 Ubuntu 10.04、Postgres 8.4 和 psycopg2 一起使用。
这个问题在 Django 1.2 中还存在吗?
这是一个已知的错误还是我遗漏了什么?
【问题讨论】:
标签: django postgresql django-testing