Django:tests.py 作为一个模块
Django: tests.py as a module
背景:
我正在使用 Django 1.8。
我开始对其进行测试。
当我使用models.py
或views.py
时,我通常会删除它们,并创建一个同名的模块文件夹来替换。
这样我就可以将模型和视图拆分到不同的代码文件中,方便编辑。
问题:
但是当我尝试将tests.py
更改为模块文件夹时,我发现__init__.py
中的测试用例无法运行。
怎么了?如果我想这样做,有什么办法吗?
请帮忙,谢谢。
来自docs:
Test discovery is based on the unittest module’s built-in test
discovery. By default, this will discover tests in any file named
“test*.py” under the current working directory.
所以我猜 __init__.py
中的测试不会被自动发现。
虽然您的问题已经得到解答,但我想分享我的测试设置。
假设我有一个具有以下结构的应用程序foo
foo
admin.py
apps.py
fixtures
testdata_01.json
forms
bar.py
__init__.py
__init__.py
models
bar.py
__init__.py
static
foo
base.css
foo.js
templates
foo
home.html
...
tests
forms
bar.py
__init__.py
views
bar.py
__init__.py
__init__.py
test_run.py
urls.py
views
bar.py
__init__.py
如您所见,我的应用程序中有一些 Bar
模型。所有对应的元素都在它们自己的模块中,即 forms.bar
和 views.bar
.
现在看看 tests
模块:如前所述,Django test-运行ner 将自动 运行 test*.py
中的所有测试,在我的设置中是 test.test_run.py
.
test_run.py
from .forms.bar import *
from .views.bar import *
如您所见,真正的测试都在同名文件中,因此我一眼就能看出将测试应用程序的哪一部分,即 tests.forms.bar
包括与表单相关的所有测试Bar
class.
tests/forms/bar.py
from ...tests import FooTest
class FooBarFormTests(FooTest):
"""Contains all tests for BarForm"""
def test_constructor(self):
self.assertTrue(True)
def test_clean(self):
self.assertTrue(False)
你注意到基数了吗classFooTest
?这个 class 位于 tests/__init__.py
.
tests/__init__.py
from django.test import TestCase
class FooTest(TestCase):
"""Base class for all app specific tests"""
fixtures = ['testdata_01.json']
如您所见,所有常见任务,例如分配特定测试数据都是在此基础中完成的 class。
背景:
我正在使用 Django 1.8。
我开始对其进行测试。
当我使用models.py
或views.py
时,我通常会删除它们,并创建一个同名的模块文件夹来替换。
这样我就可以将模型和视图拆分到不同的代码文件中,方便编辑。
问题:
但是当我尝试将tests.py
更改为模块文件夹时,我发现__init__.py
中的测试用例无法运行。
怎么了?如果我想这样做,有什么办法吗?
请帮忙,谢谢。
来自docs:
Test discovery is based on the unittest module’s built-in test discovery. By default, this will discover tests in any file named “test*.py” under the current working directory.
所以我猜 __init__.py
中的测试不会被自动发现。
虽然您的问题已经得到解答,但我想分享我的测试设置。
假设我有一个具有以下结构的应用程序foo
foo
admin.py
apps.py
fixtures
testdata_01.json
forms
bar.py
__init__.py
__init__.py
models
bar.py
__init__.py
static
foo
base.css
foo.js
templates
foo
home.html
...
tests
forms
bar.py
__init__.py
views
bar.py
__init__.py
__init__.py
test_run.py
urls.py
views
bar.py
__init__.py
如您所见,我的应用程序中有一些 Bar
模型。所有对应的元素都在它们自己的模块中,即 forms.bar
和 views.bar
.
现在看看 tests
模块:如前所述,Django test-运行ner 将自动 运行 test*.py
中的所有测试,在我的设置中是 test.test_run.py
.
test_run.py
from .forms.bar import *
from .views.bar import *
如您所见,真正的测试都在同名文件中,因此我一眼就能看出将测试应用程序的哪一部分,即 tests.forms.bar
包括与表单相关的所有测试Bar
class.
tests/forms/bar.py
from ...tests import FooTest
class FooBarFormTests(FooTest):
"""Contains all tests for BarForm"""
def test_constructor(self):
self.assertTrue(True)
def test_clean(self):
self.assertTrue(False)
你注意到基数了吗classFooTest
?这个 class 位于 tests/__init__.py
.
tests/__init__.py
from django.test import TestCase
class FooTest(TestCase):
"""Base class for all app specific tests"""
fixtures = ['testdata_01.json']
如您所见,所有常见任务,例如分配特定测试数据都是在此基础中完成的 class。