unittest subTest() support and subtests fixture
Project Links
Meta
Author: Bruno Oliveira
Requires Python: >=3.9
Classifiers
Development Status
- 4 - Beta
Framework
- Pytest
Intended Audience
- Developers
Topic
- Software Development :: Testing
Programming Language
- Python
- Python :: 3
- Python :: 3.9
- Python :: 3.10
- Python :: 3.11
- Python :: 3.12
- Python :: 3.13
- Python :: Implementation :: CPython
Operating System
- OS Independent
License
- OSI Approved :: MIT License
Typing
- Typed
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.
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.
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.
Jun 13, 2025
0.14.2
Dec 10, 2024
0.14.1
Dec 07, 2024
0.14.0
Jul 17, 2024
0.13.1
Jul 07, 2024
0.13.0
Mar 07, 2024
0.12.1
Mar 06, 2024
0.12.0
May 15, 2023
0.11.0
Feb 16, 2023
0.10.0
Oct 28, 2022
0.9.0
May 26, 2022
0.8.0
Feb 13, 2022
0.7.0
Jan 15, 2022
0.6.0
May 29, 2021
0.5.0
Dec 13, 2020
0.4.0
Aug 01, 2020
0.3.2
May 20, 2020
0.3.1
Jan 23, 2020
0.3.0
Apr 04, 2019
0.2.1
Apr 03, 2019
0.2.0
Apr 02, 2019
0.1.0