unittest subTest() support and subtests fixture
Project description
unittest subTest() support and subtests fixture.
This pytest plugin was generated with Cookiecutter along with @hackebrot’s cookiecutter-pytest-plugin template.
Features
Adds support for TestCase.subTest.
New subtests fixture, providing similar functionality for pure pytest tests.
Requirements
Python >= 3.5.
pytest >= 5.3.
pytest 6.2+
pytest 6.2 now issues a warning when internal classes are used by third-party code, which is the case for pytest-subtests which needs to use some internal classes to integrate with other pytest features (such as capturing and debugging).
For now users can ignore those warnings by adding this to their configuration file:
[pytest]
filterwarnings =
ignore:A private pytest class or function was used.:PytestDeprecationWarning
Installation
You can install pytest-subtests via pip from PyPI:
$ pip install pytest-subtests
Usage
unittest subTest() example
import unittest
class T(unittest.TestCase):
def test_foo(self):
for i in range(5):
with self.subTest("custom message", i=i):
self.assertEqual(i % 2, 0)
if __name__ == "__main__":
unittest.main()
Output
λ pytest .tmp\test-unit-subtest.py
======================== test session starts ========================
...
collected 1 item
.tmp\test-unit-subtest.py FF. [100%]
============================= FAILURES ==============================
_________________ T.test_foo [custom message] (i=1) _________________
self = <test-unit-subtest.T testMethod=test_foo>
def test_foo(self):
for i in range(5):
with self.subTest('custom message', i=i):
> self.assertEqual(i % 2, 0)
E AssertionError: 1 != 0
.tmp\test-unit-subtest.py:9: AssertionError
_________________ T.test_foo [custom message] (i=3) _________________
self = <test-unit-subtest.T testMethod=test_foo>
def test_foo(self):
for i in range(5):
with self.subTest('custom message', i=i):
> self.assertEqual(i % 2, 0)
E AssertionError: 1 != 0
.tmp\test-unit-subtest.py:9: AssertionError
================ 2 failed, 1 passed in 0.07 seconds =================
subtests fixture example
def test(subtests):
for i in range(5):
with subtests.test(msg="custom message", i=i):
assert i % 2 == 0
Output
λ pytest .tmp\test-subtest.py
======================== test session starts ========================
...
collected 1 item
.tmp\test-subtest.py .F.F.. [100%]
============================= FAILURES ==============================
____________________ test [custom message] (i=1) ____________________
def test(subtests):
for i in range(5):
with subtests.test(msg='custom message', i=i):
> assert i % 2 == 0
E assert (1 % 2) == 0
.tmp\test-subtest.py:4: AssertionError
____________________ test [custom message] (i=3) ____________________
def test(subtests):
for i in range(5):
with subtests.test(msg='custom message', i=i):
> assert i % 2 == 0
E assert (3 % 2) == 0
.tmp\test-subtest.py:4: AssertionError
================ 2 failed, 1 passed in 0.07 seconds =================
Contributing
Contributions are very welcome. Tests can be run with tox:
tox -e py37,linting
License
Distributed under the terms of the MIT license, “pytest-subtests” is free and open source software
Issues
If you encounter any problems, please file an issue along with a detailed description.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pytest_subtests-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3755a42b7416b99d90bb3cb2bd1ac4767d5e4b93b8853cb3565200a4e3a10b7e |
|
MD5 | 58c1a70d157c840edb3951dfd1ce6b85 |
|
BLAKE2b-256 | dc46ea5d2c432b1ac54591a0c818cffa05b8353bd6d418114ef2388878a52d45 |