在看了几个关于 django 测试的演讲之后,我想编写自己的 TestRunner 来跳过 django 测试,并为我的测试创建更好的包结构。
问题是我们改变了项目结构,测试运行器找不到正确的路径来进行测试发现。这就是我的项目的样子:
project/
-src/
- project_name/
- apps/
- test/ # Not a good name, i know, will change it
- some_app/
- test_models.py
- manage.py
- development.db
现在,为了测试test_models.py
我想这样做:
$ cd project/src/
$ python manage.py test some_app.test_models
问题是测试运行器找不到那个包(some_app
)和模块(test_models.py
)。如果我在测试运行程序中硬编码名称,它会改变,但我不喜欢这样做。这是我为使其工作所做的工作。
test_labels = ["%s.%s" % ("project_name.test", l)
for l in test_labels
if not l.startswith("project_name.test")]
所以,如果你这样做
$ python manage.py test some_app.test_models
它将被重写为:
$ python manage.py test project_name.test.some_app.test_models
这很好用。
我试过做sys.path.append("(...)/project_name/test)
,但也没有用。
这是我的 TestRunner 的代码:
class DiscoveryDjangoTestSuiteRunner(DjangoTestSuiteRunner):
"""A test suite runner that uses unittest2 test discovery.
It's better than the default django test runner, becouse it
doesn't run Django tests and let you put your tests in different
packages, modules and classes.
To test everything in there:
$ ./manage.py test
To test a single package/module:
$ ./manage.py test package
$ ./manage.py test package.module
To test a single class:
$ ./manage.py test package.module.ClassName
"""
def build_suite(self, test_labels, extra_tests=None, **kwargs):
suite = None
discovery_root = settings.TEST_DISCOVERY_ROOT
if test_labels:
# This is where I append the path
suite = defaultTestLoader.loadTestsFromNames(test_labels)
# if single named module has no tests, do discovery within it
if not suite.countTestCases() and len(test_labels) == 1:
suite = None
discovery_root = import_module(test_labels[0]).__path__[0]
if suite is None:
suite = defaultTestLoader.discover(
discovery_root,
top_level_dir=settings.BASE_PATH,
)
if extra_tests:
for test in extra_tests:
suite.addTest(test)
return reorder_suite(suite, (TestCase,))