How to generate reports in Behave-Python?

落花浮王杯 提交于 2019-11-29 07:42:13
Chong

Because my automations which using behave are all running on Jenkins so I can use the Jenkins plugin to display my junit report.

I think this question might help you: How can I generate an HTML report for Junit results?

You can use your junit result from behave and follow some of the top answers in this question.

BTW, if you would like to use jenkins and if you need 'good-looking' html report of behave, I would suggust you to make behave generating json output for you and display the json output using reporter of cucumber.

thistle

You can generate Allure report for your Behave tests.

First you need to install Allure Behave formatter:

$ pip install allure-behave

Then specify the formatter when run your tests:

$ behave -f allure_behave.formatter:AllureFormatter -o %allure_result_folder% ./features

This will generate JSON report to %allure_result_folder%. Then, to view HTML report you can use Allure Command line (plugins for Jenkins/TeamCity/Bamboo also available)

$ allure serve %allure_result_folder%

For more details about Allure report you can see the docs.

Behave can also generate reports in jUnit XML format. You can enable this feature simply by adding --junit to the commandline [1]. More info about formatters and reporters here [2].

$ behave --junit

[1] http://pythonhosted.org/behave/behave.html?highlight=#cmdoption--junit

[2] http://pythonhosted.org/behave/formatters.html

I know this question was asked/answered quite sometime ago.

But I thought of giving the solution which worked for me.

The Cucumber json schema differs from Behave ones. So you can't use the json created by behave to generate html reports using Cucumber Reports plugin. When I tried behave json with cucumber reports, this is what I got and you would also see NPE for uri since cucumber json was expecting to have uri exists but the behave json doesn't have uri hence NPE.

`[CucumberReport] Processing 1 json files: 
 [CucumberReport] /var/lib/jenkins/jobs/behave-test/builds/14/cucumber-html- 
                  reports/.cache/results.json
 [CucumberReport] Missing report result - report was not successfully completed
 [CucumberReport] Build status is left unchanged`

You would see report was not successfully completed.

So I installed behave2cucumber to convert behave json into cucumber json.

pip install behave2cucumber

Then have an additional step like below.

python -m behave2cucumber -i behave_json.json -o cucumber_json.json

-i represents input file in our case json file generated by behave

-o represents output file in our case cucumber compatible json file

cucumber_json.json would have the uri field populated which were missing behave json.

It works like charm.

Hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!