1

Python docs say give this example of using a subTest in unit testing:

import unittest

class NumbersTest(unittest.TestCase):

    def test_even(self):
        """
        Test that numbers between 0 and 5 are all even.
        """
        for i in range(0, 6):
            with self.subTest(i=i):
                self.assertEqual(i % 2, 0)

This runs all 5 tests (i=0..5) before producing output for all 5 failures.

How can I print about the failures along the way (say in the for loop after the with block)?

I only need the answer for Python 3.

In my test, I have thousands of subtests and it can take many minutes to finish. I want to know if it's already failed as I'm watching it.

dfrankow
  • 20,191
  • 41
  • 152
  • 214
  • Are these really tests that all contribute their unique part in testing the code, and if so, could they not be entirely independent tests? For me, having thousands of tests sounds more like a "stress-test" that I wouldn't include into the unit test suite. But if you really want it, maybe run via `pytest -s` and include print statements. See here: https://stackoverflow.com/questions/14405063/how-can-i-see-normal-print-output-created-during-pytest-run – Dr. V Dec 14 '22 at 20:56
  • @Dr.V Someone has written a test that reads in a file of cases with an arbitrary number of lines (possibly thousands), and uses subTest on each line. They are all separate cases. – dfrankow Dec 14 '22 at 21:59

1 Answers1

0

You could add some decorator to your subtest functions to create some logging during the test execution. Following code works for a decoration of the unit test cases, but you can probably adapt it to make it work also with the subtests.

from functools import wraps
import os

def print_test_name(func):
    """
    Usage: add as a decorator to the test function like:

    @print_test_name
    def my_test_case(self):
        ...

    """
    @wraps(func)
    def inner_func(*args, **kwargs):
        print("\n\rRunning test: ", func.__name__)
        result = func(*args, **kwargs)
        print("Test complete: ", func.__name__)
        return result
    return inner_func
A. L
  • 131
  • 2
  • 12
  • Thanks. I only want to know about the failures. There might be 20 failures out of 5000 cases. Can I tell if a unit test (e.g., `self.assertTrue`) has failed? – dfrankow Dec 15 '22 at 14:50