nosetests - 如何告诉 Nose 和覆盖范围不要在我的 virtualenv 中包含文件?

标签 nosetests coverage.py

我正在尝试争论 Nose +覆盖范围。如果我有这个代码:

class Foobar(object):
    def add(self, a, b):
        return a + b

这个测试:

from unittest import TestCase
from foobar import Foobar

class FoobarTest(TestCase):
    def test_good(self):
        f = Foobar()
        self.assertEquals(f.add(1,2), 3)

那么一切看起来都很好!

$ nosetests
.
Name        Stmts   Miss  Cover   Missing
-----------------------------------------
foobar.py       3      0   100%
----------------------------------------------------------------------
Ran 1 test in 0.018s

OK

但是如果我在源代码中添加一行

import requests

class Foobar(object):
    def add(self, a, b):
        return a + b

然后我的报告中会得到很多额外的内容

$ nosetests
.
Name                                                                           Stmts   Miss  Cover   Missing
--------------------------------------------------------------------------------------------------------
foobar.py                                                                      4      0   100%
requests.py                                                                   26      5    81%   54, 72-75
requests/adapters.py                                                         180    134    26%   48, 51, 54, 89-102, 105, 111-117, 1

[snip]

那么我如何配置覆盖范围以表示“不必费心测量或报告属于我的 virtualenv 一部分的任何内容 - 只需在我的工作目录下执行这些操作即可”。我确信它与 .coveragerc 有关,但我在正确调用时遇到了一些问题。

最佳答案

想通了。

1) 删除任何与覆盖率相关的 ini 文件

2)将其添加到“nose.cfg”在你的主目录中作为 Nose 的东西

[nosetests]
with-coverage=1   ; generate a coverage report (in the "cover" directory)
cover-package=.   ; only report on coverage files in the current directory
cover-html=1      ; generate a pretty html report
cover-erase=1     ; re-generate coverage statistics on each run

当然,您必须确保您的测试文件与 Nose 正在寻找的 testMatch 模式匹配。

关于nosetests - 如何告诉 Nose 和覆盖范围不要在我的 virtualenv 中包含文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33150637/

相关文章:

python - 如何使用 nosetests 测试函数在函数内被调用

pycharm - 如何让 PyC​​harm 了解某些文件/目录被排除在覆盖范围之外?

python - 是否可以在 Python 中进行代码覆盖断言?

python - 从 python 的覆盖范围中排除单元测试

pytest - 分析 numba 包装函数的覆盖范围

google-app-engine - 如何在运行单元测试时抑制 App Engine 日志记录?

python - 单元测试 pythons telnetlib

python - 在 python 中使用 nose 进行并行化测试

python - 如何在 Python 的装饰器中访问类属性?

python - 使用 py.test 覆盖不包括导入