kreigan
kreigan

Reputation: 113

VS Code Python unittest: No tests discovered even when following the tutorial

I am trying to follow the Python tutorial as per https://code.visualstudio.com/docs/python/unit-testing#_create-tests. But every time no tests are discovered and neither I can execute the test file manually: 0 tests are run.

the workspace structure and files content I have tried doing that on my working workspace and then attempted to start everything from scratch but both times the results are the same. Also, I have tried using Remote - WSL on VS Code Insiders and the classic VS Code:

Version: 1.34.0-insider (user setup)
Commit: 473af338e1bd9ad4d9853933da1cd9d5d9e07dc9
Date: 2019-05-01T00:22:05.899Z
Electron: 3.1.8
Chrome: 66.0.3359.181
Node.js: 10.2.0
V8: 6.6.346.32
OS: Windows_NT x64 10.0.17134

Version: 1.33.1 (user setup)
Commit: 51b0b28134d51361cf996d2f0a1c698247aeabd8
Date: 2019-04-11T08:27:14.102Z
Electron: 3.1.6
Chrome: 66.0.3359.181
Node.js: 10.2.0
V8: 6.6.346.32
OS: Windows_NT x64 10.0.17134

basic.py


    def just_method(number):
        return number

    if __name__ == "__main__":
        print just_method(42)

basictest.py

    import unittest
    import basic

    class MyBasicTests(unittest.TestCase):

        def default_number(self):
            number = 42
            self.assertEqual(basic.just_method(number), number)

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

settings.json content:

    {
        "python.testing.unittestArgs": [
            "-v",
            "-s",
            ".",
            "-p",
            "*test.py"
        ],
        "python.testing.pyTestEnabled": false,
        "python.testing.nosetestsEnabled": false,
        "python.testing.unittestEnabled": true,
        "python.pythonPath": "/usr/bin/python"
    }

Could you please help to identify the problem? I am pretty sure it is located somewhere on my side.

Upvotes: 1

Views: 2112

Answers (2)

Eric Jin
Eric Jin

Reputation: 3934

In order to inherit a class, you have to use the super() method.

class ChildClass(ParentClass):
    def __init__(self, *args): # '*args' for arguments to this class
        super().__init__(self, *args) # '*args' for arguments to pass in the parent class's __init__ method

in your case:

# put imports here
class MyBasicTests(unittest.TestCase):
    def __init__(self):
        super().__init__()
    def test_this(self):
        number = 20
        self.assertEqual(number, basic.just_method(number))

Upvotes: -1

Björn Kristinsson
Björn Kristinsson

Reputation: 624

File names and method names are important for test discovery. If you follow the tutorial to the letter this should work.

If you want to use your own code instead, try changing default_number to test_default_number, and basictest.py to test_basic.py.

Upvotes: 3

Related Questions