When using python's unittest subtest, I am confused regarding how parameters are named and scoped within the sub-test.
The canonical example given in the link above seems to imply that the parameters used within the with self.subtest() clause can be passed as keyword arguments to subTest(). For reference, the example shown is this:
class NumbersTest(unittest.TestCase):
def test_even(self):
"""
Test that numbers between 0 and 5 are all even.
"""
for i in range(0, 6):
with self.subTest(i=i):
self.assertEqual(i % 2, 0)
It is using an ambiguous convention of naming the inner-scoped variable the same as the parameter (i=i). I took this to mean that keyword argument name is taken as the inner scoped variable name. However, when I tried to create my own test, I found that both PyCharm and the python interpreter complained of unresolved references if the keyword argument was not named precisely the same as the outer-scoped variable used as the parameter input.
i.e.:
class NumbersTest(unittest.TestCase):
def test_even(self):
"""
Test that numbers between 0 and 5 are all even.
"""
for i in range(0, 6):
with self.subTest(num=i): # <-- Renamed keyword argument parameter
self.assertEqual(num % 2, 0) # < -- Results in unresolved reference error "num"
How does one pass parameters into the subtest? How are they named and referenced within the subtest code block?