【问题标题】:Why is unittest coverage disabled for Python 3.8.3?为什么对 Python 3.8.3 禁用 unittest 覆盖?
【发布时间】:2020-09-17 12:45:20
【问题描述】:

我有这个版本的 Python:

3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)]

在 PyCharm CE 2020.1.1 的 Windows 10 上运行。使用一个很简单的unittest

# test_simple.py

from unittest import TestCase

class Simple(TestCase):
    def test_one(self):
        self.assertTrue(True)

我可以运行python -m coverage run -m unittest,它完成并生成一个.coverage SQLite 文件;我可以从生成 Test Results 树的 IDE 运行测试;但是 Run / Run with Coverage 是灰色的,没有任何解释。

我已按照(相当分散的)PyCharm 文档进行设置/构建、执行、部署,启用激活覆盖视图在应用覆盖之前显示选项给编辑这没有帮助。

为什么覆盖范围变灰,我该如何解决?

【问题讨论】:

  • 据我所知,覆盖支持仅在 PyCharm 的专业版中可用。
  • @MrBeanBremen 是的,你是对的。谢谢。我会接受这个作为答案。

标签: python-3.x pycharm code-coverage python-unittest


【解决方案1】:

很遗憾,PyCharm 社区版不提供 IDE 覆盖支持,如 the edition features 所示。

您必须像往常一样在命令行上运行覆盖,例如 this answer 中所示。

您还可以使用一些包装器来创建和打开 HTML 报告。可以在this blog post 中找到使用 pytest 进行覆盖的此类包装器的示例。这是该博客中使用的脚本(请注意,我更改了覆盖路径以使用当前版本):

import os
import subprocess
import webbrowser


def main():
    subprocess.call(['coverage', 'erase'])    
    subprocess.call(['coverage', 'run', '--module', 'pytest'])
    subprocess.call(['coverage', 'html'])
    webbrowser.open("file://" + os.getcwd() + "/htmlcov/index.html", new=2)

if __name__ == '__main__':
    main()

【讨论】:

  • 我怀疑以上大部分内容在不使用subprocess 的情况下是可能的。毕竟,coverage 本身就是一个 Python 模块。
  • 是的,对 - 您可以使用 pytest.main(),其余的使用 Coverage 实例,尽管我猜这不会有什么不同。
猜你喜欢
  • 2017-09-30
  • 1970-01-01
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
相关资源
最近更新 更多