Instance of 'MyTestClass' has no 'assertEqual' member pylint (no-member) VScode

Viewed 125

I'm trying to setup my VScode environment for python programing (I was using it till now only to write c++ code). I set up very simple "Hello world" program and wrote dummy test for it:

from problems.hello_world import HelloWorld

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

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

This test is passing, which is wrong, it should fail! But my VScode complains (in the last line where I call assertEqual) that Instance of 'SimpleTest' has no 'assertEqual' member pylint (no-member).

It also complains in import line that Unable to import 'problems.hello_world'pylint(import-error) .

My python binary and python test are both in the same folder named problems.

I'm using bazel build and python 3.7 version. I can't figure it out what is wrong with my VScode python setup, or whatever the problem is. With c++ environment it works perfectly. In my workspace settings.json I have pylint enabled, here is the content of it:

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

}

That are only two lines in my settings.json file. For my c++ programs I use separate workspace (might that be the problem?). Thanks for help!

2 Answers

You have to implement a child-class of unittest.TestCase, like

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) 

While the accepted answer is correct, there are other correct answers as well. The key is what test framework you are using.

The accepted answer uses unittest which is built into python. It is capable but, is generally not used in modern python projects as there are other options that provide more DRY functionality, better reporting, etc.

One such test framework is pytest. It does not rely on subclassing or calling methods to perform assertions. Tests can be written as functions or direct subclasses of object. Assertions are done using the assert statement built into python.

def test_something():
    assert 1 != 2

class TestSomething:
    def test_a_thing(self):
        assert 1 == 1
Related