我正在使用Django 1.9和Django Restframework处理传感器API和仪表板应用程序。
我有通常的单元测试,应该在我调用python manage.py test
时运行。
此外,我有两种不同类型的测试:
a)测试与上游数据源和API的连接。
b)数据一致性测试。
虽然单元测试应该在任何上下文中运行,但a)和b)取决于特定的上下文。当这个上下文可用(或应该被测试)时,我想手动调用它们。
每当我调用管理test
命令时,我都不希望它们运行(并且失败)。
任何建议,如何创建一个默认排除某些测试文件夹但在显式调用时运行它们的测试运行器。
答案 0 :(得分:1)
您将依赖于上下文的测试移到单独的应用程序并将其排除。然后实现这个跑步者:TEST_RUNNER = 'testing.simple.AdvancedTestSuiteRunner'
from django.test.simple import DjangoTestSuiteRunner #@UnresolvedImport
import logging
from django.conf import settings
EXCLUDED_APPS = getattr(settings, 'TEST_EXCLUDE', [])
class AdvancedTestSuiteRunner(DjangoTestSuiteRunner):
def __init__(self, *args, **kwargs):
from django.conf import settings
settings.TESTING = True
south_log = logging.getLogger("south")
south_log.setLevel(logging.WARNING)
super(AdvancedTestSuiteRunner, self).__init__(*args, **kwargs)
def build_suite(self, *args, **kwargs):
suite = super(AdvancedTestSuiteRunner, self).build_suite(*args, **kwargs)
if not args[0] and not getattr(settings, 'RUN_ALL_TESTS', False):
tests = []
for case in suite:
pkg = case.__class__.__module__.split('.')[0]
if pkg not in EXCLUDED_APPS:
tests.append(case)
suite._tests = tests
return suite
当明确添加到命令时,app将不会被忽略'python manage.py test south'
他们有类别
@attr(speed='slow')
class MyTestCase:
def test_long_integration(self):
pass
def test_end_to_end_something(self):
pass
来自docs
-a = ATTR, - attr = ATTR 仅运行具有ATTR [NOSE_ATTR]
指定属性的测试-A = EXPR, - eval-attr = EXPR 仅运行Python表达式EXPR求值为True的测试[NOSE_EVAL_ATTR]
答案 1 :(得分:1)
这里是我正在使用的精确代码片段的Django 1.9更新:
# from https://djangosnippets.org/snippets/2211/
from django.test.runner import DiscoverRunner
import logging
from django.conf import settings
EXCLUDED_APPS = getattr(settings, 'TEST_EXCLUDE', [])
class AdvancedDiscoverRunner(DiscoverRunner):
def __init__(self, *args, **kwargs):
from django.conf import settings
settings.TESTING = True
south_log = logging.getLogger("south")
south_log.setLevel(logging.WARNING)
super(AdvancedDiscoverRunner, self).__init__(*args, **kwargs)
def build_suite(self, *args, **kwargs):
suite = super(AdvancedDiscoverRunner, self).build_suite(*args, **kwargs)
if not args[0] and not getattr(settings, 'RUN_ALL_TESTS', False):
tests = []
for case in suite:
pkg = case.__class__.__module__.split('.')[0]
if pkg not in EXCLUDED_APPS:
tests.append(case)
suite._tests = tests
return suite