-
-
Save sytranvn/fd46e6084ff1d35f06539ed3643e079d to your computer and use it in GitHub Desktop.
Python 3.4. subTest example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| This is a comment on http://eli.thegreenplace.net/2014/04/02/dynamically-generating-python-test-cases/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import unittest | |
| class TestsContainer(unittest.TestCase): | |
| longMessage = True | |
| testsmap = { | |
| 'foo': [1, 1], | |
| 'bar': [1, 2], | |
| 'baz': [5, 5], | |
| 'baf': [5, 6], | |
| } | |
| def test_a(self): | |
| for name, (a, b) in self.testsmap.items(): | |
| with self.subTest(name=name): | |
| self.assertEqual(a, b, name) | |
| if __name__ == '__main__': | |
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ====================================================================== | |
| FAIL: test_a (__main__.TestsContainer) (name='bar') | |
| ---------------------------------------------------------------------- | |
| Traceback (most recent call last): | |
| File "/tmp/x.py", line 16, in test_a | |
| self.assertEqual(a, b, name) | |
| AssertionError: 1 != 2 : bar | |
| ====================================================================== | |
| FAIL: test_a (__main__.TestsContainer) (name='baf') | |
| ---------------------------------------------------------------------- | |
| Traceback (most recent call last): | |
| File "/tmp/x.py", line 16, in test_a | |
| self.assertEqual(a, b, name) | |
| AssertionError: 5 != 6 : baf | |
| ---------------------------------------------------------------------- | |
| Ran 1 test in 0.001s | |
| FAILED (failures=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment