unittest says the continue statement is not being tested?

Viewed 202

Python 2.7.18 with Coverage 5.3 (client requirement)

Really stumped with this one. Trying to get the unit tests to report 100% before starting refactoring.

In the code example below (greatly simplified of course), Coverage says the continue statement is not being checked!

class Test7(unittest.TestCase):
    def test_7(self):
        test7()

def test7():
    for i in range(0, 3):
        if i > 0:
            if i > 1:
                print 'loop1 iteration = ' + str(i)
            continue
        if i == 0:
            print 'blah'

If I put in a print statement above the continue, it gets checked and has no issues! 'pass' doesn't do the same though... :(

I've tried many other iterations and cannot figure it out.

It has to be something basic?

Thanks!

1 Answers

The continue line is probably removed by the CPython peephole optimizer, so can not be covered.

Suggested refactoring to avoid that issue:

if i > 0:
    if i > 1:
        print 'loop1 iteration = ' + str(i)
elif i == 0:
    print 'blah'
Related