mercredi 14 avril 2021

What does custom unittest Test Suite really do? How to use it?

Like bellow, I thought loadTestsFromTestCase would only run the cases that were loaded, but turns out it still runs all cases, both MyTest1 and MyTest2.

import unittest

class MyTest1(unittest.TestCase):
    def test1(self):
        print('test - 1')
 
    def test2(self):
        print('test - 2')
 
 
class MyTest2(unittest.TestCase):
    def test3(self):
        print('test - 3')
 
    def test4(self):
        print('test - 4')
 
 
if __name__ == '__main__':
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    runner = unittest.TextTestRunner()
 
    suite.addTest(loader.loadTestsFromTestCase(MyTest1))
 
    runner.run(suite)

Another example, adjust the execution order of test cases

if __name__ == '__main__':
    suite = unittest.TestSuite()

    test_cases = [MyTest2('test4'), MyTest2('test3'), MyTest1('test2'), MyTest1('test1')]
    suite.addTests(test_cases)

    runner = unittest.TextTestRunner()
    runner.run(suite)

The outcome still from 1 to 4, not the reverse order that I expected. Do I understand it right? Or under what circumstances need to use it and how to use it? Looking forward to answers.

Aucun commentaire:

Enregistrer un commentaire