Skip to content

Instantly share code, notes, and snippets.

@sytranvn
Forked from encukou/subTest
Created March 19, 2018 04:39
Show Gist options
  • Select an option

  • Save sytranvn/fd46e6084ff1d35f06539ed3643e079d to your computer and use it in GitHub Desktop.

Select an option

Save sytranvn/fd46e6084ff1d35f06539ed3643e079d 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