简体   繁体   English

如何使用 pytest-cov 生成覆盖率报告并打印到终端?

[英]How can I use pytest-cov to both generate a coverage report and also print to terminal?

Background背景

I'm new to using pytest and pytest-cov having switched over from unittest + coverage.py我不熟悉使用 pytest 和 pytest-cov 已从 unittest + coverage.py 切换

I first set up my automated tests to run in this way:我首先将我的自动化测试设置为以这种方式运行:

python3 -m pytest --cov=myapplication

which gave me output like this to the terminal:这给了我 output 这样的终端:

----------- coverage: platform linux, python 3.8.5-final-0 -----------
Name                        Stmts   Miss  Cover
-----------------------------------------------
myapplication/__init__.py       0      0   100%
myapplication/file.py          30     30     0%
myapplication/another_file.py  20      6    70%
[...]
-----------------------------------------------
TOTAL                        1195    464    61%

Then i wanted to generate an xml report so i changed the command:然后我想生成一个 xml 报告所以我改变了命令:

python3 -m pytest --cov-report xml:coverage.xml --cov=myapplication

Problem问题

The problem i'm having is that after adding --cov-report xml:coverage.xml i no longer get any output to the terminal我遇到的问题是,在添加--cov-report xml:coverage.xml后,我不再得到任何 output 到终端

Looking at the documentation for pytest-cov i find this: 查看 pytest-cov 的文档,我发现:

These three report options output to files without showing anything on the terminal: [goes on to show xml, html and annotation reporting options]这三个报告选项 output 到文件而不在终端上显示任何内容:[继续显示 xml、html 和注释报告选项]

Question问题

How can i both generate a report and also print to terminal in the same test run?如何在同一测试运行中既生成报告又打印到终端? (Is this even possible?) (这甚至可能吗?)

(I could run the test suite two times, but if i can i'd like to do everything at once) (我可以运行测试套件两次,但如果可以的话,我想一次做所有事情)


I am using these versions:我正在使用这些版本:

  • Python 3.8.5 Python 3.8.5
  • pytest 6.2.2 (the latest version as of writing this) pytest 6.2.2(撰写本文时的最新版本)
  • pytest-cov 2.11.1 (-"-) pytest-cov 2.11.1 (-"-)

You can do this by specifying another --cov-report argument with one of the terminal output formats.您可以通过使用终端 output 格式之一指定另一个--cov-report参数来执行此操作。 You can have --cov-report term or --cov-report term-missing .您可以有--cov-report term--cov-report term-missing For example:例如:

python3 -m pytest --cov-report term --cov-report xml:coverage.xml --cov=myapplication

See the pytest-cov docs you linked to for how term and term-missing work.请参阅您链接到的 pytest-cov 文档,了解termterm-missing工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM