Python Django多重测试+;设置+;迁移
我真的很困惑:我使用了#Django 1.11.2。我在同一个测试类中有两个测试。它们(以及实际的代码)依赖于数据,我使用迁移注入数据。特别是,我创建了一些安全组。当通过Python Django多重测试+;设置+;迁移,python,django,python-3.x,django-models,python-unittest,Python,Django,Python 3.x,Django Models,Python Unittest,我真的很困惑:我使用了#Django 1.11.2。我在同一个测试类中有两个测试。它们(以及实际的代码)依赖于数据,我使用迁移注入数据。特别是,我创建了一些安全组。当通过/manage.py migrate运行迁移时,迁移工作正常。他们在班上的第一次考试中也表现得很好。但是,当为第二个测试运行setUp时,它会抱怨组不存在。这是测试类中的设置函数: class MyDjangoTest(TestCase): def setUp(self): # Every test ne
/manage.py migrate
运行迁移时,迁移工作正常。他们在班上的第一次考试中也表现得很好。但是,当为第二个测试运行setUp
时,它会抱怨组不存在。这是测试类中的设置函数:
class MyDjangoTest(TestCase):
def setUp(self):
# Every test needs access to the request factory.
self.client = Client()
self.password = 'top_secret'
# ERROR: fails at this line in test_2 wit object not found exception, test_1 OK
group: Group = Group.objects.get_by_natural_key("SomeGroup")
self.user1: User = User.objects.create_user(
username='john', email='john@…', password=self.password)
self.user1.groups.add(group)
self.user2: User = User.objects.create_user(
username='jane', email='jane@…', password=self.password)
self.user2.groups.add(group)
def test_1(self):
# Executes just fine
doStuff()
def test_2(self):
# Throws an exception when fetching the group in setUp
doMoreStuff()
我想我知道发生了什么-django在单独的测试之间刷新数据库中的所有数据,所以我的组被破坏了,可以这样说。我需要在我的测试设置代码中对此进行调整。我想我了解正在发生的事情-django在各个测试之间刷新数据库中的所有数据,所以我的组被破坏了,可以这样说。我需要在测试设置代码中对此进行调整。