Skip to main content

unittest subTest() support and subtests fixture

Project description

unittest subTest() support and subtests fixture.

PyPI version https://img.shields.io/conda/vn/conda-forge/pytest-subtests.svg Python versions https://github.com/pytest-dev/pytest-subtests/workflows/test/badge.svg https://img.shields.io/badge/code%20style-black-000000.svg

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:

tox -e py37

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-subtests-0.12.0.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

pytest_subtests-0.12.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest-subtests-0.12.0.tar.gz.

File metadata

  • Download URL: pytest-subtests-0.12.0.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.1 CPython/3.11.8

File hashes

Hashes for pytest-subtests-0.12.0.tar.gz
Algorithm Hash digest
SHA256 331de3989c37abdad0745f38bd5f2e1ac1a025c7fbd0e01fe37651b51022857c
MD5 aa51eb1a22eab4115f57b4274a8a6bc1
BLAKE2b-256 b8c09ae4723cc7cdf689fbd32797b7af864669824bca0718aebe7e79590a07d7

See more details on using hashes here.

File details

Details for the file pytest_subtests-0.12.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_subtests-0.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 01c05a72758aa77c31306a65cb28aa5f911dcf042c334496e5273b7cb5b04003
MD5 d6f9835f6fafe4b979801c021c53666b
BLAKE2b-256 9d19facd807defab2524b0f9bca1eccf55021beaff04e18e3cd48fb2df9511b0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page