Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7868867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:08:05+00:00 2026-06-03T01:08:05+00:00

Background I’m trying to figure out how to run a single unittest against multiple

  • 0

Background

I’m trying to figure out how to run a single unittest against multiple input values and then display the failure. This is a trivial demo of the sort of thing I have in mind:

from time import time
import unittest

def demo():
    while True:
        count = 0
        for i in xrange(10):
            count += 1
            yield int(time() * 1000) + count
        count = 0

class TestDemo(unittest.TestCase):
    def setUp(self):
        self.gen = demo()
        self.prev = next(self.gen)

    def test_always_bigger(self):
        for cycle in xrange(1000):
            curr = next(self.gen)
            self.assertGreater(curr, self.prev)
            self.prev = curr

if __name__ == '__main__':
    unittest.main()

The most similar questions I’ve found been answered with some sort of dynamic test_<something> method creation (ex: 1, 2, 3) or nose generators (ex: 1, 2). I’m looking to run 1000s of iterations based on unpredictable input while sticking to the standard library so neither solution is a great fit. Simple looping (as shown above) works well with two limitations: for complex inputs there is an inadequate record of what caused the test to fail (my real problem); and, a single failure within the test_<...> method fails the whole test.

Question

I can live with the early failure, but how can I get at the inputs causing a failure without creating thousands of lines of output on success?

Non-answer

I tried the assert... method msg kwarg but it really only works well for the sort of trivial cases the library already handles well. In the above example unittest knows to show the two longs that caused the assertion failure. I could annotate that and provide a lot of insight into the failure with `self.assertGreater(curr, self.prev, msg=”cycle %s” % cycle), but showing deeply nested dicts on an assertDictEquals is a mess.

Desirable Solution

This answer shows an interesting use of the logging module but would generate thousands of lines of output for each successful test. Is it possible to detect failure from within the test method? Something like:

    def test_always_bigger(self):
        for cycle in xrange(1000):
            curr = next(self.gen)
            fail = self.assertGreater(curr, self.prev)
            if fail:
                log.debug('...')
            self.prev = curr
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:08:06+00:00Added an answer on June 3, 2026 at 1:08 am
    def test_always_bigger(self):
        for cycle in xrange(1000):
            curr = next(self.gen)
            try:
                self.assertGreater(curr, self.prev)
            except AssertionError: # raised by TestCase.fail, called by all asserts
                log.debug('...')
                raise
            self.prev = curr
    

    This implements the semantics of logging in case of failure, then continuing with the failure. If you want to finish all tests, I would do the following:

    def test_always_bigger(self):
        ex = None
        for cycle in xrange(1000):
            curr = next(self.gen)
            try:
                self.assertGreater(curr, self.prev)
            except AssertionError, ae:
                ex = ae # just remember it
                log.debug('...')
            self.prev = curr
        if ex:
            raise ex
    

    Obviously, this would only raise the first AssertionError, but it would run until completion and would log all failures independently. Since this function represents only one test for the framework, you can’t really produce multiple failures out of it.

    If you need access to all the exceptions for some reason, you may be able to get away with the following (not tested thoroughly with unittest)

    def test_always_bigger(self):
        exes = []
        for cycle in xrange(1000):
            curr = next(self.gen)
            try:
                self.assertGreater(curr, self.prev)
            except AssertionError, ae:
                exes.append(ae)
                log.debug('...')
            self.prev = curr
        if exes:
            self.fail(exes)
            # if that doesn't work, try: raise AssertionError(exes)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background : I'm trying to convert some JavaScript code which uses the the Crossfilter
Background: Using unix, codeigniter from localhost. I'd like to run a controllers via a
Background info I am trying to upgrade a custom CMS to support the HTML5
Background Hi All, I'm trying to use Boost::MPI, at the moment I'm just trying
Background: I'm using the middleware django-multihost (http://effbot.org/zone/django-multihost.htm) to allow my single django app to
[background below] I've got my data modelled out in SQLObject in Python on the
Background I'm customising a tumblr theme (Source: hasaportfolio ), and I am trying to
Background I am trying to create a copy of a business object I have
Background: I am trying to add data to a SQL DB with C#. I
Background Information I'm trying to build the GAUL library according to this Instructions .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.