I’ve noticed some strange behaviour when setting a test suite’s _tests
attribute to empty, and then attempting to add tests to it again as part of a loop.
I have to essentially reinitialise a test suite’s contents as part of running my whole test suite in a loop. I’ve found that I can’t just call the same test suite again, iteratively, and so I reinitialise it.
Before entering my loop, I have a list of tests my_tests
.
runner = unittest.TextTestRunner()
my_suite = unittest.TestSuite(my_tests)
for i in range(2):
result = runner.run(my_suite)
print(f"{i} Number of tests, after running tests: suite suite length {my_suite.countTestCases()} test object {my_tests}")
unittest.registerResult(result)
# Empty test suite and re-add tests
my_suite._tests = []
print(my_suite.__dict__)
my_suite.addTests(my_tests)
print(my_suite.__dict__)
print(f"{i} Number of tests, after reinitialising suite: suite length {my_suite.countTestCases()} test object {my_tests}")
and I get the following output
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
0 Number of tests, after running tests: suite length 1 test object [<single_node_tests.SingleNodeTest testMethod=test_basic>]
{'_tests': [], '_removed_tests': 1}
{'_tests': [<single_node_tests.SingleNodeTest testMethod=test_basic>], '_removed_tests': 1}
0 Number of tests, after reinitilising suite:suite length 2 test object [<single_node_tests.SingleNodeTest testMethod=test_basic>]
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
1 Number of tests, after running tests: suite length 2 test object [<single_node_tests.SingleNodeTest testMethod=test_basic>]
{'_tests': [], '_removed_tests': 2}
{'_tests': [<single_node_tests.SingleNodeTest testMethod=test_basic>], '_removed_tests': 2}
1 Number of tests, after reinitilising suite:suite length 3 test object [<single_node_tests.SingleNodeTest testMethod=test_basic>]
so the number of tests in the test suite is actually increasing over iterations, despite the list being set to an empty list. seen in suite length 1
-> suite length 2
-> suite length 3
I tried and succeeded in resolving this with the following:
I added:
my_suite.removed_tests = 0
and then found that the reported suite length stayed at 1, over all iterations.
I really just don’t understand why the removed_test
count would affect the behaviour of adding tests to a suite, which is reported to be empty?
1