如何禁用nose测试的覆盖率报告

9 投票
3 回答
2824 浏览
提问于 2025-04-16 12:12

希望这是一个简单的关于python/django的nose测试问题,不过我在nose的文档里找不到答案。

我该怎么做才能在运行./manage.py test测试后不显示覆盖率报告呢?

覆盖率报告让我不得不向上滚动几十行才能看到我失败测试的错误信息,这真的打断了我的工作流程!我喜欢用nose,但如果我找不到解决办法,我就得回去用普通的django测试了。

提前谢谢你的建议!

这是我需要向上滚动才能看到错误信息的内容:

./manage.py test
[...]
======================================================================
FAIL: testFreightAveragesContainer ([...].tests.test_average_container.AveragesContainerTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "[...]/tests/test_average_container.py", line 32, in testAveragesContainer
    121
AssertionError: 12 != 121

Name                                                    Stmts   Miss  Cover   Missing
-------------------------------------------------------------------------------------
abc.advertising                                             0      0   100%   
abc.advertising.management                                  0      0   100%   
abc.advertising.models                                     73     73     0%   1-91
abc.client                                                  0      0   100%   
abc.client.admin                                          200    200     0%   1-430
abc.client.forms                                           57     57     0%   1-99
abc.client.management                                       0      0   100%   
abc.client.models                                         434    434     0%   1-1007
abc.client.translations                                    30     30     0%   1-33
abc.client.views                                           18     18     0%   1-24
abc.cms                                                     0      0   100%   
abc.cms.management                                          0      0   100%   
abc.cms.models                                            235    235     0%   1-416
abc.cms.translations                                       18     18     0%   1-21
abc.commodity                                               0      0   100%   
abc.commodity.admin                                        51     51     0%   1-107
abc.commodity.forms                                        12      0   100%   
abc.commodity.models                                      343    343     0%   1-669
abc.commodity.search_indexes                               10      1    90%   13
abc.commodity.tests                                         5      3    40%   3-6
abc.commodity.tests.test_average_container                 14      1    93%   37
abc.commodity.tests.test_lead_time_analyser                70      0   100%   
abc.commodity.tests.test_price_analysers                    6      2    67%   10, 15
abc.commodity.translations                                 38     38     0%   1-40
abc.commodity.urls                                         11      0   100%   
abc.commodity.views                                       452    359    21%   22-25, 29-32, 74, 83-96, 101-142, 151-159, 166-172, 179-192, 198-250, 260-264, 287-288, 300-314, 321-378, 385-433, 441-502, 509-557, 563-567, 574-581, 587-662, 668-672, 679-686, 694-722, 730-754, 761-843, 850-854, 861-871, 878-904, 951-952, 962-978, 990-1010
abc.data_submission                                         0      0   100%   
abc.data_submission.iron_ore_submission_normalisation     187    187     0%   1-481
abc.data_submission.managers                               27     27     0%   1-71
abc.data_submission.models                                635    635     0%   1-1222
abc.invoicing                                               0      0   100%   
abc.invoicing.models                                      261    261     0%   1-440
abc.invoicing.translations                                 25     25     0%   1-26
abc.localisation                                            0      0   100%   
abc.localisation.management                                 0      0   100%   
abc.localisation.models                                   167    167     0%   1-528
abc.localisation.translation                                0      0   100%   
abc.localisation.translation.models                       144    144     0%   1-318
abc.localisation.translations                              22     22     0%   1-24
abc.mailing                                                 0      0   100%   
abc.mailing.admin                                         110    110     0%   1-165
abc.mailing.forms                                          33     33     0%   1-44
abc.mailing.management                                      0      0   100%   
abc.mailing.models                                        337    337     0%   1-706
abc.mailing.translations                                   15     15     0%   1-16
abc.marketing                                               0      0   100%   
abc.marketing.models                                        0      0   100%   
abc.product                                                 0      0   100%   
abc.product.models                                          0      0   100%   
abc.staff                                                   0      0   100%   
abc.staff.models                                           80     80     0%   1-154
.staff.translations                                      4      4     0%   1-5
abc.utils                                                   0      0   100%   
abc.utils.base                                              9      9     0%   1-19
abc.utils.base.models                                     187    187     0%   1-435
abc.utils.custom_ui                                         0      0   100%   
abc.utils.custom_ui.advanced_filter                       175    175     0%   1-241
abc.utils.custom_ui.models                                  3      3     0%   1-5
abc.utils.date                                             41     41     0%   1-73
abc.utils.db                                                0      0   100%   
abc.utils.db.routers                                       21     19    10%   1-7, 10, 13-32
abc.utils.format                                           33     33     0%   1-60
abc.utils.models                                           10     10     0%   1-30
abc.utils.navigation                                       10     10     0%   2-19
abc.utils.views                                            23     23     0%   1-42
-------------------------------------------------------------------------------------
TOTAL                                                    4636   4420     5%   
----------------------------------------------------------------------
Ran 9 tests in 0.059s

FAILED (failures=1)
./manage.py test commodity --failfast  13.59s user 0.17s system 97% cpu 14.099 total

3 个回答

1

我对Django的manage.py脚本不太熟悉,不过我猜你可以看看里面的“test”任务,看看它是怎么调用nose的。我想你是想只运行测试,而不需要覆盖率的报告。关于nose的使用说明可以在这里找到:http://somethingaboutorange.com/mrl/projects/nose/0.11.3/usage.html

4

你可以这样做:

pip install nose-cov

这样做可以让你对报告的选项有更多的控制,而且只有在你要求的时候,它才会把信息输出到控制台。接着把 --with-coverage 改成 --with-cov

NOSE_ARGS = [
 '--with-cov',
 '--cov-report', 'html',
]

这样只会导出为html格式。

这里也有同样的问题:

在启用HTML报告时,如何禁用nose覆盖报告输出到STDOUT?

4

你在用哪个版本的Django和nose呢?我知道的情况是,Django的测试运行默认并不是这样,而nose的默认设置也不是这样。

如果你想在nose中使用覆盖率插件,你需要加一个选项叫做"--with-coverage"。

我不太确定你是不是不小心把覆盖率功能打开了,你可以去settings.py文件里看看,找找像COVERAGE_MODULES和TEST_RUNNER这样的变量,看看它们的设置是什么。

撰写回答