Coverage branch confused on if/elif block

Issue #487 invalid
Chris Warrick created an issue

This rather simple code has 89% coverage, because coverage in branch mode thinks that line 5 never exits the module.

import sys

if sys.argv[1] == 'a':
    print("A")
elif sys.argv[1] == 'b':
    print("B")

Steps to reproduce:

coverage run --branch foo.py a
coverage run -a --branch foo.py b

To get 100% coverage: append one more run, this time with c (or anything other than a or b) as its argument.

Reproducible in 4.0.3 and 4.1 (from hg)

Comments (3)

  1. Ned Batchelder repo owner

    I don't understand what you think is wrong here. When you run the program twice (with "a" and "b"), there is a branch that isn't taken: from line 5 to the exit. It's the missing "else". Coverage.py is correctly telling you that you are missing a test case.

  2. Chris Warrick reporter

    You’re right. I actually oversimplified my example when writing this: in my real code, those are mutual argparse arguments — coverage is not aware of this, and thus can’t know that one of the branches will always fire. (I’d love to use elif args.other: for readability, but else: # args.other will do)

  3. Log in to comment