简体   繁体   English

如何在我的 Foundry 代码库中强制执行最低测试覆盖率百分比?

[英]How do I enforce a minimum test coverage percentage in my Foundry Code Repositories?

I not only want to write unit tests in my Foundry Code Repository, but I want to enforce a minimum amount of coverage for checks to pass.我不仅想在我的 Foundry Code Repository 中编写单元测试,而且我想强制执行最小覆盖率以通过检查。

It's a best practice to ensure that I'm covering at least a certain fraction of my code with unit tests, but tests don't seem to be a requirement in standard repositories.最好的做法是确保我至少用单元测试覆盖了我的代码的一部分,但测试似乎不是标准存储库中的要求。

How do I accomplish my two testing goals within my Foundry Code Repository?我如何在我的 Foundry 代码库中完成我的两个测试目标?

You can use PyTest Coverage to compute coverage and enforce a minimum percentage.您可以使用PyTest Coverage来计算覆盖率并强制执行最小百分比。

  1. Add the following to your repo's meta.yml :将以下内容添加到您的 repo 的meta.yml
...

test:
  requires:
    - pytest-cov
...

Note: this is outside the requirements section in a section of its own.注意:这是它自己的一个部分中的需求部分之外

  1. Create a pytest.ini file at /transforms-python/src/pytest.ini with the following contents:/transforms-python/src/pytest.ini创建一个pytest.ini文件,内容如下:
[pytest]
addopts = --cov=<<package name, e.g. myproject>> --cov-report term --cov-fail-under=100

Note: you can tune the enforcement percentage down from 100 if you'd like注意:如果您愿意,您可以将强制执行百分比从 100 调低

Your tests will now fail, even if they include some unit tests, if that coverage threshold is below your configured value:如果覆盖阈值低于您的配置值,您的测试现在将失败,即使它们包含一些单元测试:

检查

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

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