Created
December 12, 2016 14:05
-
-
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.
This file contains 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
$ 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 ============================================= |
This file contains 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
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)) |
This file contains 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
$ 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