【发布时间】:2016-11-05 12:08:48
【问题描述】:
我正在为我的项目处理测试用例,我必须使用夹具来存储数据。整个项目的夹具相当大(大约 20 MB)。由于该项目涉及多个应用程序,因此我希望在运行测试时仅加载一次夹具。在每个测试用例上指定固定装置以及使用 keepdb 标志效果不佳,并且所有测试都需要大量时间。您能否建议一个好的解决方法,以便我加快速度。我也关注了这个post,但它不适用于 django 1.9。
添加了示例测试 App1/tests.py
from django.test import TestCase
class App1View1TestCase(TestCase):
fixtures = [
'fixtures/app_fixture.json',
]
class App1View2TestCase(TestCase):
fixtures = [
'fixtures/app_fixture.json',
]
class App1View3TestCase(TestCase):
fixtures = [
'fixtures/app_fixture.json',
]
App2/tests.py
from django.test import TestCase
class App2View1TestCase(TestCase):
fixtures = [
'fixtures/app_fixture.json',
]
class App2View2TestCase(TestCase):
fixtures = [
'fixtures/app_fixture.json',
]
class App2View3TestCase(TestCase):
fixtures = [
'fixtures/app_fixture.json',
]
每个测试用例有超过 5 个测试。
【问题讨论】:
-
你读过the docs吗?关于范围的部分似乎很相关。
-
嘿@jonrsharpe 我是 django 测试的新手,并试图只坚持这一点。我不熟悉 pytest 和 django。你曾经在一个项目中使用过这两种方法吗?
-
哦,你不是说 pytest 夹具吗?对不起,我的错;你能澄清你的意思吗? minimal reproducible example 会很有帮助。
-
@TarunBehal 你考虑过用鼻子试试吗?看来 django 测试运行器可能不灵活直接地做到这一点。见这里:stackoverflow.com/a/9816748/1757321
-
@Rexford 不,我没有。问题是我自己不能介绍一个新的应用程序。如果 django 测试运行程序无法处理,那么我会看到这个选项。你有这方面的经验吗?您是否遇到过任何限制?
标签: python django unit-testing testing