Skip to content

Instantly share code, notes, and snippets.

@encukou
Created April 7, 2014 10:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save encukou/10017915 to your computer and use it in GitHub Desktop.
Save encukou/10017915 to your computer and use it in GitHub Desktop.
Python 3.4. subTest example
This is a comment on http://eli.thegreenplace.net/2014/04/02/dynamically-generating-python-test-cases/
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()
======================================================================
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