Python Forum

Full Version: VSCode not able to discover tests
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My test file is as below:

from unittest import TestCase
import pytest

class TestNumbers(TestCase):
    def TestSum(self):
        self.assertEqual(AddNum(5,6), 11)
        
    def TestDiff(self):
        self.assertEqual(SubtractNum(6,3),3)

obj = TestNumbers()
obj.TestSum()
obj.TestDiff()

# if __name__ == "__main__":
#     unittest.main()
I have set VSCode configuration. Enabled test framework and set it to use pyTest. But each time I start VSCode, it shows message to select Test Framework as no tests were discovered.

In the code given above, the last two lines I have commented. Even when these were enabled, VSCode was not able to discover the tests.
Are you intending to use pytest or unittest? Your code looks like it's really using unittest and your test methods aren't named correctly - they should start with test (docs here). Since they don't, they aren't found. I don't know what the naming conventions are for pytest, but the docs should tell you.
Sorry, I edited the original post but that code is not appearing.

I tried both. pyTest code is as below:

import pytest

def TestSum():
    assert AddNum(5,6) == 11
    
def TestDiff():
    assert SubtractNum(6,3) == 3

TestAdd()
TestDiff()
The VSCode documentation says that if tests are discovered it shows "Run Tests" at the bottom. However, it is also showing "No Tests Discovered" even when I have configured testing framework.
Did you check the docs for pytest to see what the naming conventions are for test functions? You shouldn't have to call the functions yourself - the test framework discovers them and calls them.
I found the mistake based on your previous post. Thanks.

The function names started with 'Test' when these should start with 'test'. It seems it is case-sensitive.

After changing function name to 'testAddition', I am getting: 'Run Test | Debug Test' on top of the function.

However, VSCode alerts are confusing. Previously, when it was not discovering tests, it was still showing 'Run Tests' on the status bar and also was showing separate alert "No tests were found'.
Ah, I guess pytest uses a similar naming convention then. That makes sense; I believe PEP 8 says names should start with lowercase letters.

Can't help with VSCode, I'm afraid; I've never used it.