【问题标题】:Instance of 'MyTestClass' has no 'assertEqual' member pylint (no-member) VScode“MyTestClass”的实例没有“assertEqual”成员pylint(无成员)VScode
【发布时间】:2021-05-24 04:49:10
【问题描述】:

我正在尝试为 python 编程设置我的 VScode 环境(我直到现在才使用它来编写 c++ 代码)。我设置了非常简单的“Hello world”程序并为它编写了虚拟测试:

from problems.hello_world import HelloWorld

    class SimpleTest():
        def setUp(self):
            self.hello = HelloWorld()

        def test_dummy(self):
            self.assertEqual(True,False) 
      

这个测试是通过,这是错误的,它应该失败!但是我的 VScode 抱怨(在我调用 assertEqual 的最后一行)Instance of 'SimpleTest' has no 'assertEqual' member pylint (no-member)

它还在导入行中抱怨 Unable to import 'problems.hello_world'pylint(import-error)

我的 python 二进制文件和 python 测试都在同一个名为问题的文件夹中。

我正在使用 bazel build 和 python 3.7 版本。我无法弄清楚我的 VScode python 设置有什么问题,或者是什么问题。使用 c++ 环境,它可以完美运行。 在我的工作区 settings.json 中,我启用了 pylint,这是它的内容:

{
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,

}

这只是我的 settings.json 文件中的两行。对于我的 c++ 程序,我使用单独的工作区(可能是问题所在?)。 感谢您的帮助!

【问题讨论】:

    标签: python visual-studio-code vscode-settings pylint


    【解决方案1】:

    你必须实现一个unittest.TestCase的子类,比如

    from problems.hello_world import HelloWorld
    from unittest import TestCase
    
    
    class SimpleTest(TestCase):
        def setUp(self):
            self.hello = HelloWorld()
    
        def test_dummy(self):
            self.assertEqual(True,False) 
    

    【讨论】:

      【解决方案2】:

      虽然accepted answer 是正确的,但还有其他正确答案。关键是你使用的是什么测试框架。

      accepted answer 使用 python 内置的unittest。它功能强大,但通常不用于现代 python 项目,因为还有其他选项可以提供更多 DRY 功能、更好的报告等。

      一个这样的测试框架是pytest。它不依赖子类化或调用方法来执行断言。测试可以写成object 的函数或直接子类。断言是使用 python 内置的assert statement 完成的。

      def test_something():
          assert 1 != 2
      
      class TestSomething:
          def test_a_thing(self):
              assert 1 == 1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-22
        • 1970-01-01
        • 2019-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-25
        相关资源
        最近更新 更多