python - 编写单元测试以检查python中的代码覆盖率
问题描述
我需要在 python 中使用 unittest 编写测试,当覆盖率小于 50% 时会失败。如:
class ExampleTest(unittest.TestCase):
def setUp(self):
cov = coverage.Coverage()
cov.load()
with open(os.devnull, "w") as f:
self.total = cov.report(file=f)
def test_compare_values(self):
self.assertGreaterEqual(self.total, 20)
但是当测试运行时,文件覆盖率被锁定并且无法打开。
如何解决这个问题?
解决方案
不要尝试从测试中读取覆盖率数据。而是使用--fail-under=50
覆盖率报告命令上的选项。
推荐阅读
- android - 传递 ViewModel 的 MutableLiveData 的值
- google-chrome - 无法在 Ubuntu 上安装谷歌浏览器
- c# - 自动将 Log 语句添加到方法开始和方法结束
- python - Keyerror:添加此行期间的 ID df4 = pd.read_csv('../input/riadd-dataset/RFMiD_Training_Labels.csv',sep='\t')
- python - 在 Python 中移动值
- php - (1/1) ErrorException Trying to get property 'id' of non-object
- android - 有没有办法让编译器相信 @NonNull 变量在 Kotlin 中实际上是可空的?
- arrays - 在 Rails 中创建一系列月份和年份
- c - 从c中的文件中读取值
- powerbi - Unichar 不显示在 PBI 报告服务器中