如何查看Django的manage.py测试命令中运行的测试

时间:2014-02-01 16:09:29

标签: python django unit-testing django-unittest

使用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'...

有没有办法看到:

  1. 实际执行了哪些测试
  2. 来自什么模块
  3. 按什么顺序
  4. 我在doc中找不到任何解决方案。

2 个答案:

答案 0 :(得分:95)

您可以将-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}, - verbosity {0,1,2,3}

     

详细程度; 0 =最小输出,1 =正常输出,                    2 =详细输出,3 =非常详细的输出

答案 1 :(得分:15)

奈杰尔的答案很棒,绝对是最低的入门门槛。但是,您可以通过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

输出应该如下所示:

enter image description here

注意:您测试中的注释可用于提供比名称更好的输出。

例如:

def test_something(self):
    """Something should happen"""
    ...

输出&#34;应该发生什么事情&#34;在运行测试时。

要获得额外分数:您还可以生成/输出代码覆盖率:

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

中运行reports/cover以及整洁的HTML报告时,您将获得一个很好的代码覆盖率摘要