Python Forum
VSCode not able to discover tests
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
VSCode not able to discover tests
#1
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.
Reply
#2
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.
Reply
#3
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.
Reply
#4
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.
Reply
#5
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'.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Ran 0 tests in 0.000s - unittest Peaches 8 4,929 Dec-31-2021, 08:58 AM
Last Post: Peaches
  StopIteration exception when mock PostgreSQL connection in several tests igor87z 1 2,879 Jun-10-2020, 06:16 PM
Last Post: ibreeden
  Running tests in a sibling directory to code sodhiar 1 2,666 Nov-07-2019, 11:28 PM
Last Post: MckJohan
  pytest loop through multiple tests? burvil 0 4,283 Sep-26-2019, 11:42 PM
Last Post: burvil
  How do i write tests for this code? hshivaraj 0 1,989 Apr-27-2019, 05:28 PM
Last Post: hshivaraj
  Python unittest - running multiple tests from CSV file asheru93 0 4,153 Jan-21-2019, 08:26 AM
Last Post: asheru93
  Tests are not running .Dont why shankar 3 6,279 Sep-20-2018, 07:06 AM
Last Post: shankar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020