在我的测试中,我不仅测试完美的情况,而且特别是边缘情况和错误条件。所以我想确保一些唯一性约束有效。
虽然我的测试和测试装置非常复杂,但我能够将问题追溯到以下示例,该示例不使用任何自定义模型。要重现该行为,只需将代码保存到 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 中是否仍然存在?
这是一个已知的错误还是我错过了什么?