Python Py.Test:报告和 HTML 输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17557813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:31:21  来源:igfitidea点击:

Py.Test : Reporting and HTML output

pythonreportingpytest

提问by Dave

This is not a technical question at all really. However, I can not locate my .HTML report that is supposed to be generated using:

这根本不是技术问题。但是,我找不到应该使用以下方法生成的 .HTML 报告:

py.test --cov-report html pytest/01_smoke.py

py.test --cov-report html pytest/01_smoke.py

I thought for sure it would place it in the parent location, or the test script location. Does neither and I have not been able to locate. So I am thinking it is not being generated at all?

我认为它肯定会将它放在父位置或测试脚本位置。两者都没有,我一直无法找到。所以我在想它根本没有被生成?

采纳答案by hpk42

I think you also need to specify the directory/file you want coverage for like py.test --cov=MYPKG --cov-report=htmlafter which a html/index.htmlis generated.

我认为您还需要指定要覆盖的目录/文件,例如生成py.test --cov=MYPKG --cov-report=htmla 之后html/index.html

回答by TheFallenTech

if you do not specify --cov=/path/to/code then it will not generate the html at all.

如果您不指定 --cov=/path/to/code 则它根本不会生成 html。

$ py.test --cov-report html test_smoke.py
== test session starts == 
platform linux2 -- Python 2.7.12, pytest-3.4.0, py-1.5.2, pluggy-0.6.0 rootdir: /home/someuser/somedir, inifile: plugins: xdist-1.22.0, forked-0.2, cov-2.5.1 collected 3 items                                                                 


test_smoke.py ...                                             [100%]

== 3 passed in 0.67 seconds ==

We can see that there is no message that output was created... However if we specify --cov=...

我们可以看到没有输出已创建的消息...但是如果我们指定 --cov=...

$ py.test --cov-report html test_smoke.py --cov=/path/to/code
== test session starts ==
platform linux2 -- Python 2.7.12, pytest-3.4.0, py-1.5.2, pluggy-0.6.0
rootdir: /home/someuser/somedir, inifile:
plugins: xdist-1.22.0, forked-0.2, cov-2.5.1
collected 3 items                                                                                                                                                                                                                                                         

test_smoke.py ...                                            [100%] 

---------- coverage: platform linux2, python 2.7.12-final-0 ----------
Coverage HTML written to dir htmlcov

We now see that there are no stats for tests that passed, instead we see that coverage was written to HTML and sent to the default directory: ./htmlcov

我们现在看到没有通过测试的统计信息,而是看到覆盖率被写入 HTML 并发送到默认目录:./htmlcov

NOTE: if you want a different directory, then affix :/path/to/directory to the output style html -> py.test --cov-report html:/path/to/htmldir test_smoke.py --cov=/path/to/code

注意:如果你想要一个不同的目录,然后将 :/path/to/directory 添加到输出样式 html -> py.test --cov-report html:/path/to/htmldir test_smoke.py --cov=/path /至/代码

If you see a plain html file, this is an indication that your problem is the --cov=/path/to/my/pkg perhaps... are you sure that the code you are testing lives here?

如果你看到一个普通的 html 文件,这表明你的问题是 --cov=/path/to/my/pkg 也许......你确定你正在测试的代码在这里吗?

回答by Harry_pb

If you want to generate report in html, give full file path of the test file.

如果要生成 html 报告,请提供测试文件的完整文件路径。

py.test --cov-report html test_file_name.py --cov=/home/ubuntu/venv/python3/lib/python3.7/site-packages/test/

Then start a python server

然后启动一个python服务器

python -m http.server 

Navigate to the htmlfile in htmlcov directory

导航到htmlhtmlcov 目录中的文件

http://0.0.0.0:8000/venv/python3/lib/python3.7/site-packages/htmlcov/

You will see the report

你会看到报告