我有一个奇怪的问题tox
,py.test
,coverage
和pytest-cov
:当py.test
与--cov
选项是从启动tox
,这似乎需要__init__.py
的文件tests
是不会立即明显的文件夹.
在撰写这篇文章时,我通过添加上述内容解决了最初的问题tests/__init__.py
,但到目前为止我还没有完全理解为什么它确实有效或无效,所以我仍然在寻求帮助.请参阅下面的详细信息.
我在SO上找到了一个相关的问题,但它只会让它更加混乱,因为答案似乎与我到目前为止所得到的相反: `py.test`和`__init __.py`文件
另请参阅此处的官方文档:py.test - 良好的集成实践(页面底部).
简化的项目结构:
setup.py
tox.ini
.coveragerc
project/
__init__.py
module1.py
module2.py
tests/
__init__.py (optional, an empty file)
test_module1.py
test_module2.py
Run Code Online (Sandbox Code Playgroud)
相关部分tox.ini
:
[testenv:check]
commands = py.test --cov=project --cov-report=term
deps =
pytest
coverage
pytest-cov
[pytest]
python_files = test_*.py
norecursedirs = .tox
Run Code Online (Sandbox Code Playgroud)
相关部分.coveragerc
:
[run]
branch = True
omit = project/tests/*
Run Code Online (Sandbox Code Playgroud)
现在,结果如下:
py.test --cov=project …
我试图实现基本python模块的100%覆盖率.我使用Ned Batchelder的coverage.py模块来测试它.
1 class account(object):
2 def __init__(self, initial_balance=0):
3 self.balance = initial_balance
4 def add_one(self):
5 self.balance = self.balance + 1
Run Code Online (Sandbox Code Playgroud)
这些是测试.
class TestAccount(unittest.TestCase):
def test_create_edit_account(self):
a = account1.account()
a.add_one()
Run Code Online (Sandbox Code Playgroud)
这是我得到的报道报道.
COVERAGE REPORT = Name Stmts Miss Cover Missing ----------------------------------------------------- __init__ 1 1 0% 1 account1 5 3 40% 1-2, 4 account2 7 7 0% 1-7
我们可以看到,第1-2和第4行未被覆盖,这些是定义.其余的行都被执行了.
我有一个Python的小宠物项目,我想做它的报道.我跑的时候
py.test -x -s MYPACKAGE --cov-report html --cov MYPACKAGE
Run Code Online (Sandbox Code Playgroud)
它向我展示了覆盖范围内缺少的大量线条.主要是导入和类/方法定义.
我确信所有这些线都在我的单元测试中处理,第19和31-35行验证了这一点.
为什么py.test
将所有定义标记为"缺失"?