Skip to content

Instantly share code, notes, and snippets.

@elyezer
Created December 12, 2016 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elyezer/cabc2c2c59d29a1ca8e5cf9888ed4398 to your computer and use it in GitHub Desktop.
Save elyezer/cabc2c2c59d29a1ca8e5cf9888ed4398 to your computer and use it in GitHub Desktop.
Python subTest usage and how py.test is not able to report subTest failures.
$ py.test test.py
=============================================== test session starts ===============================================
platform linux -- Python 3.5.2, pytest-3.0.5, py-1.4.31, pluggy-0.4.0
rootdir: /home/elyezer/code, inifile:
collected 1 items
test.py F
==================================================== FAILURES =====================================================
_______________________________________ TestIsEven.test_should_all_be_even ________________________________________
self = <test.TestIsEven testMethod=test_should_all_be_even>
def test_should_all_be_even(self):
for n in (0, 4, -2, 3, 7, 11):
with self.subTest(n=n):
> self.assertTrue(is_even(n))
E AssertionError: False is not true
test.py:12: AssertionError
============================================ 1 failed in 0.04 seconds =============================================
from unittest import TestCase
def is_even(n):
return n % 2 == 0
class TestIsEven(TestCase):
def test_should_all_be_even(self):
for n in (0, 4, -2, 3, 7, 11):
with self.subTest(n=n):
self.assertTrue(is_even(n))
$ python3 -m unittest test.py
======================================================================
FAIL: test_should_all_be_even (test.TestIsEven) (n=3)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/elyezer/code/pulp-smash/test.py", line 12, in test_should_all_be_even
self.assertTrue(is_even(n))
AssertionError: False is not true
======================================================================
FAIL: test_should_all_be_even (test.TestIsEven) (n=7)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/elyezer/code/pulp-smash/test.py", line 12, in test_should_all_be_even
self.assertTrue(is_even(n))
AssertionError: False is not true
======================================================================
FAIL: test_should_all_be_even (test.TestIsEven) (n=11)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/elyezer/code/pulp-smash/test.py", line 12, in test_should_all_be_even
self.assertTrue(is_even(n))
AssertionError: False is not true
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment