简体   繁体   English

如何使用Behave从Python BDD功能测试中获取代码覆盖率数据?

[英]How can I get code coverage data from Python BDD functional tests using Behave?

I haven't seen an answer for this specific question ( Test coverage tool for Behave test framework ) and I haven't seen any Google search results produce a sufficient answer. 我还没有看到这个特定问题的答案( 行为测试框架的测试覆盖率工具 ),我还没有看到任何谷歌搜索结果产生足够的答案。 Therefore... 因此...

How can I get a code coverage report from Behave? 如何从Behave获取代码覆盖率报告? I find it hard to believe that there are no Python developers using BDD methodology and I find it even harder to believe that those Python developers who are using BDD are doing so without code coverage statistics from their functional tests. 我发现很难相信没有使用BDD方法的Python开发人员,我发现更难以相信那些使用BDD的Python开发人员在没有功能测试的代码覆盖统计数据的情况下这样做。 Can Coverage.py be used to drive Behave to produce code coverage? 可以使用Coverage.py来驱动Behave以产生代码覆盖吗? How? 怎么样?

I don't know how to use behave, but I used Cucumber for BDD, which I think probably almost similar. 我不知道如何使用表现,但我使用Cucumber进行BDD,我认为可能几乎相似。 And so I think you should be able to use behave with coverage. 所以我认为你应该能够使用覆盖范围的行为。 you have to specify which file to include in the file.. (I used it with cucumber). 你必须指定要包含在文件中的文件..(我用它和黄瓜一起使用)。 See if this might help. 看看可能会有所帮助。

Hope this answer your question :) 希望这回答你的问题:)

# .coveragerc to control coverage.py
[run]
parallel = True

# if you want to include tests append tests/*
include =
    src/*
    *src*

[paths]
source =
    src/
    */src

tests =
    tests/
    */tests

Following on from David's suggestion above. 继大卫的建议之后。

Assuming the code to be tested is in an app directory, add the following to your .coveragerc file: 假设要测试的代码位于app目录中,请将以下内容添加到.coveragerc文件中:

[run]
source=app/

From the terminal: 从终端:

coverage run $(which behave);

You can then use coverage report or coverage html as normal. 然后,您可以正常使用覆盖率报告或覆盖率html。 If you don't specify the app dir in your .coveragerc file, coverage will test all the Python libraries local to your behave installation. 如果未在.coveragerc文件中指定app dir,则coverage将测试您的行为安装本地的所有Python库。

Behave can generate junit coverage data and the coverage package can combine this data from multiple test runs as well as produce an HTML report that you can peruse or automatically publish in your CI environment. Behave可以生成junit覆盖数据, 覆盖包可以组合来自多个测试运行的数据,并生成HTML报告,您可以在CI环境中仔细阅读或自动发布。

Here are the statements I currently use to produce, combine, and report on coverage with behave: 以下是我目前用于生成,组合和报告覆盖率的语句:

cd your/repo/tests  # <-- Make sure you're inside your tests dir!
rm -rf behave-reports/*
behave --junit --junit-directory behave-reports
coverage combine
coverage html

The rm -rf behave-reports/* forcefully removes everything inside the behave-reports/ directory so that I am guaranteed either a fresh coverage report or nothing at all (producing a failure in CI, in my case). rm -rf behave-reports/*强制删除了behave-reports /目录中的所有内容,这样我就可以保证新的覆盖率报告或者根本不保证(在我的情况下会导致CI失败)。 Note that if you run your tests locally you'll want to add an entry to your .gitignore file (or equivalent) so that you aren't adding and committing your test results. 请注意,如果您在本地运行测试,则需要在.gitignore文件(或等效文件)中添加一个条目,这样您就不会添加和提交测试结果。

Running behave with --junit will produce junit output, while the --junit-directory flag tells behave where to write that junit data on disk. 使用--junit运行行为将产生junit输出,而--junit-directory标志告诉行为在磁盘上写入该junit数据的行为。

Running coverage combine eats all of the code coverage and combines that into a single coverage file. 运行coverage combine占用所有代码覆盖率,并将其组合到单个覆盖文件中。

Finally, coverage html produces a pretty html report that includes all of the combined coverage data. 最后, coverage html生成一个漂亮的html报告,其中包含所有组合的覆盖数据。

Another option, use coverage to run behave's main script: 另一种选择,使用coverage来运行行为的主脚本:

coverage run /path/to/lib/python2.7/site-packages/behave/__main__.py

Of course you'll want to specify in your .coveragerc which source files you want to include. 当然,您需要在.coveragerc中指定要包含的源文件。

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

相关问题 如何从 Python function 运行行为测试? - How can I run Behave tests from a Python function? 如何在行为 (BDD) 中查看 print() 语句 - How can I see print() statements in behave (BDD) 在运行一些调用python脚本的java DB单元测试时,如何测试python脚本的代码覆盖率? - While running some java DB Unit tests which call a python script how can I test the code coverage for the python script? 我的代码 python BDD 行为循环中的动作链问题 - problem with action chain in my code python BDD behave loop 如何提高 Python3 的代码覆盖率 - How can I improve code coverage of Python3 如何在 Eclipse 中或从命令行运行 Google App Engine 的 Python 代码覆盖率测试 - How to run Python code coverage tests for Google App Engine in Eclipse or from command line 如何使用行为(Python BDD 框架)设置环境变量? - How to setup environment variables with behave (Python BDD framework)? 一般而言,如何处理Python行为或BDD场景中的迭代/循环? - How to deal with iteration/looping in Python behave or BDD Scenarios in general? 如何从Python行为步骤传递变量? - How can I pass a variable from the Python-Behave step? 我如何使用python tornado从html获取数据 - How can I get data from html using python tornado
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM