Python unittest - possible to keep failing until pass?

276 views Asked by At

Let's say there is a test function that goes through a list of various numbers to test. These numbers are listed in increasing order such that once you get past a certain number (done through a loop), the results become acceptable and I can pass the test. (This means that all numbers BEFORE the passing number are failures). Now that I've found the acceptable number, I can stop the entire test regardless of whether there are more numbers in the list. Thus, I basically need a way to "fail until pass" using Python unittest. Is there a way to do this using built-in methods from unittest? I've pasted a code snippet to better communicate what I need to do.

def test_numbers(self):
    passing_number = 10     # can be any number randomly chosen in  number_list
    number_list = [8,9,10,11,12]   # example list of numbers to test
    for(number in number_list):
        # Do some operations with number...

        # Pass criteria - Not sure what to do here
        if(number == passing_number):
            # Then accept 'number' and stop the entire test.
1

There are 1 answers

5
Paulo Scardine On BEST ANSWER

Well, by definition a unit test is an automated piece of code that invokes a unit of work in the system and then checks a single assumption about the behavior of that unit of work. Single is the key here.

If the test is supposed to fail for a given input, then the code unit passed the test. IMHO if you want to apply the test to a range of inputs and assure it passes for at least one of them, this loop should be placed at a code unit, not on the test case.