简体   繁体   中英

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.

工作区结构和文件内容 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.

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 .

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM