在使用Django manage.py test
命令完成测试执行后,只有传递的测试数被打印到控制台.
(virtualenv) G:\Project\>python manage.py test
Creating test database for alias 'default'...
True
..
----------------------------------------------------------------------
Ran 2 tests in 0.017s
OK
Destroying test database for alias 'default'...
有没有办法看到:
哪些测试实际执行了
从什么模块
以什么顺序
我在doc中找不到任何解决方案.
您可以传递-v 2
给test
命令:
python manage.py test -v 2
运行此命令后,你会得到类似的东西(我正在使用django 2,随意忽略迁移/数据库的东西):
Creating test database for alias 'default' ('file:memorydb_default?mode=memory&cache=shared')... Operations to perform: Synchronize unmigrated apps: messages, staticfiles Apply all migrations: admin, auth, contenttypes, sessions Synchronizing apps without migrations: Creating tables... Running deferred SQL... Running migrations: Applying contenttypes.0001_initial... OK ... Applying sessions.0001_initial... OK System check identified no issues (0 silenced). test_equal_hard (polls.tests.TestHard) ... ok <--------+ test_equal_simple (polls.tests.TestSimple) ... ok <--------+ | | That's your tests! >----------------------------+
顺便说一句,v
代表冗长(你也可以使用--verbosity=2
):
python manage.py test --verbosity=2
以下摘录自python manage.py test --help
:
-v {0,1,2,3}, - ververosity {0,1,2,3}
详细程度; 0 =最小输出,1 =正常输出,2 =详细输出,3 =非常详细的输出
Nigel的答案很棒,绝对是最低的入门门槛.但是,你可以得到更优质的反馈django_nose
(这不是说很难设置).
以下是来自:BDD with Python
第一:安装一些要求:
pip install nose pinocchio django_nose
然后添加以下内容 settings.py
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' NOSE_ARGS = ['--with-spec', '--spec-color']
然后按正常方式运行测试:
python manage.py test
输出应该如下所示:
注意:测试中的注释可用于提供比名称更好的输出.
例如:
def test_something(self): """Something should happen""" ...
运行测试时会输出"应该发生的事情".
额外点数:您还可以生成/输出代码覆盖率:
pip install coverage
将以下内容添加到settings.py中的NOSE_ARGS: '--with-coverage', '--cover-html', '--cover-package=.', '--cover-html-dir=reports/cover'
例如:
NOSE_ARGS = ['--with-spec', '--spec-color', '--with-coverage', '--cover-html', '--cover-package=.', '--cover-html-dir=reports/cover']
然后,当你运行时,你会获得一个很好的代码覆盖率摘要python manage.py test
,以及一个简洁的html报告reports/cover