如何在 PyCharm for Python 项目中获取代码覆盖率报告

编程入门 行业动态 更新时间:2024-10-28 01:27:44
本文介绍了如何在 PyCharm for Python 项目中获取代码覆盖率报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 python 中创建了一个简单的加法方法,例如[main.py]

I have created a simple addition method in python like [main.py]

class Calculator: def sum(self,a,b): return a+b

我已经在 [test.py] 中编写了测试用例

And I have written test case in [test.py]

from unittest import Testcase from main import Calculator class TestCalculator(TestCase): def setUp(self): self.calc=Calculator() def test_sum(self): answer=self.cals.sum(2,4) self.assertEqual(answer,6)

现在我想检查这段代码的代码覆盖率.我不知道如何在 PyCharm IDE 中获取覆盖率报告.任何人,如果您知道如何获得整个项目的代码覆盖率,请指导

Now I want to check code coverage of this code. I am not getting how to get the coverage report in PyCharm IDE. Anyone, please guide if you know how to get the code coverage of the whole project

推荐答案

你可以使用 Coverage.py.只需 pip install coverage 并在 test.py 或 main.py 上添加一个 main 方法并运行它.

You could just use Coverage.py. Just pip install coverage and add a main method on your test.py or main.py and run it.

例如添加到test.py,

For example, add to test.py,

if __name__ == '__main__': unittest.main()

并在终端上运行,

coverage test.py

更多推荐

如何在 PyCharm for Python 项目中获取代码覆盖率报告

本文发布于:2023-11-14 10:43:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587002.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:覆盖率   代码   报告   项目   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!