简体   繁体   中英

How to use unittest.TestSuite in VS Code?

In the future, I'll need to add many identical tests with different parameters. Now I am making a sample test suite:

import unittest

class TestCase(unittest.TestCase):
    def __init__(self, methodName='runTest', param=None):
        super(TestCase, self).__init__(methodName)
        self.param = param

    def test_something(self):
        print '\n>>>>>> test_something: param =', self.param
        self.assertEqual(1, 1)

if __name__ == "__main__":
    suite = unittest.TestSuite()
    testloader = unittest.TestLoader()
    testnames = testloader.getTestCaseNames(TestCase)
    for name in testnames:
        suite.addTest(TestCase(name, param=42))
    unittest.TextTestRunner(verbosity=2).run(suite)

It gets discovered by VS Code:

start
test.test_navigator.TestCase.test_something

When I run the tests, I don't receive the parameter:

test_something (test.test_navigator.TestCase) ... 
>>>>>> test_something: param = None
ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

If I run this file directly, everything works as expected (note param = 42 part)

test_something (__main__.TestCase) ...
>>>>>> test_something: param = 42
ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

So it looks like VS Code is running the tests on its own just by using the discovered classes and ignoring TestSuite completely?

What am I doing wrong?

Thanks.

The problem is your code is in a if __name__ == "__main__" block which is only executed when you point Python directly at the file. So when the extension asks unittest to get all the tests and then run them for us it doesn't run the code in your if __name__ == "__main__" block (which is why it can find it but it doesn't do anything magical).

If you can get it to work using unittest 's command-line interface then the extension should run it as you want it to.

The key is to implement the load_tests function:

def load_tests(loader, tests, pattern):
    suite = unittest.TestSuite()
    testnames = loader.getTestCaseNames(TestCase)
    for name in testnames:
        suite.addTest(TestCase(name, param=42))
        suite.addTest(TestCase(name, param=84))
    return suite

The documentation says:

If load_tests exists then discovery does not recurse into the package, load_tests is responsible for loading all tests in the package.

Now my tests run as expected.

PS Thanks to Brett Cannon for pointing me to Unit testing framework documentation

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