What you have is an expression with the and
operator. The and
operator returns its first operand if it is falsey, or its second operand otherwise. Assuming self.subTest(...)
returns something truthy, your code is equivalent to:
ctx = self.subTest("Invalid input") and self.assertRaises(ValueError)
with ctx: ...
Which is equivalent to:
self.subTest("Invalid input")
ctx = self.assertRaises(ValueError)
with ctx: ...
Or:
self.subTest("Invalid input")
with self.assertRaises(ValueError): ...
So, at best, what you have may be misleading. At worst it's a bug, since subTest
's context manager isn't being used.
If self.subTest
returns a falsey value, then self.assertRaises(...)
is never executed, which would be a clear bug in your test.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…