Python Forum
VSCode not able to discover tests - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: VSCode not able to discover tests (/thread-28341.html)



VSCode not able to discover tests - rpk2006 - Jul-15-2020

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.


RE: VSCode not able to discover tests - ndc85430 - Jul-15-2020

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.


RE: VSCode not able to discover tests - rpk2006 - Jul-15-2020

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.


RE: VSCode not able to discover tests - ndc85430 - Jul-15-2020

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.


RE: VSCode not able to discover tests - rpk2006 - Jul-15-2020

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'.


RE: VSCode not able to discover tests - ndc85430 - Jul-15-2020

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.