Closed keszybz closed 3 years ago
It's not clear to me that relaxing the test would not cause false positives. A less disruptive change would be to add a keyword arg to assertRaisesMessages to allow substring matches and default it to false, explicitly turning it on only for those tests that fail. Unless all such tests fail, in which case your PR is fine.
It's only the two matches shown in the commit message that fail like that.
If you prefer, I can rework it to have a keyword arg... I didn't think it was worth it the trouble, because if the whole test string matches, having a false positive where the string unexpectedly changes while still matching the whole older value of the string should be quite rare. I.e. if the exception message is broken / unexpectedly changed, I expect the match for the substring will fail too.
yes, I prefer a keyword arg, thanks.
I guess python2 compat is still necessary? With a keyword-only argument this works nicely, but it's not compatible with python2 :(
diff --git css_parser_tests/basetest.py css_parser_tests/basetest.py
index 2b269064a1..0410416bfa 100644
--- css_parser_tests/basetest.py
+++ css_parser_tests/basetest.py
@@ -149,7 +149,7 @@ class BaseTestCase(unittest.TestCase):
else:
self.fail("%s did not raise %s" % (callsig, exception))
- def assertRaisesMsg(self, excClass, msg, callableObj, *args, **kwargs):
+ def assertRaisesMsg(self, excClass, msg, callableObj, *args, substring_match=False, **kwargs):
"""
Just like unittest.TestCase.assertRaises,
but checks that the message is right too.
@@ -158,8 +158,9 @@ class BaseTestCase(unittest.TestCase):
self.assertRaisesMsg(
MyException, "Exception message",
- my_function, (arg1, arg2)
- )
+ my_function, arg1, arg2,
+ kwarg1=val, kwarg2=val,
+ substring_match=True|False)
from
http://www.nedbatchelder.com/blog/200609.html#e20060905T064418
@@ -171,7 +172,7 @@ class BaseTestCase(unittest.TestCase):
if not msg:
# No message provided: any message is fine.
return
- elif excMsg == msg:
+ elif (msg in excMsg if substring_match else msg == excMsg):
# Message provided, and we got the right message: passes.
return
else:
This works here with python2.7-2.7.18-6.fc33.x86_64 and python3-3.9.1-1.fc33.x86_64.
With python3.10, we get the following failure:
By checking if the argument matches just a substring, we can match those cases without making the code more complicated. Matching just for substrings in exception messages is pretty common.