簡體   English   中英

如何查看在 Django 的 manage.py test 命令期間運行了哪些測試

[英]How to see which tests were run during Django's manage.py test command

使用 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. 按什么順序

我在文檔中沒有找到任何解決方案。

您可以將-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=非常詳細的輸出

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以及在reports/cover的整潔的 html 報告時,您將獲得一個很好的代碼覆蓋率摘要

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM