Blue Moon
Blue Moon

Reputation: 4661

How to exclude the program startup code (__name__ == "__main__") from being included in pytest coverage reports?

We have several small scripts in our project that take some command-line arguments. For example:

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--branch")
    command_line_args = parser.parse_args()
    if not command_line_args.branch:
        raise Exception
    main(command_line_args.branch)

we are not really interested in unit-test this. However, this impacts our coverage report, is there a way to exclude the if __name__ == "__main__" from unit-tests using pytest?

Upvotes: 3

Views: 596

Answers (1)

Laurent
Laurent

Reputation: 13518

You can add a comment, like this:

if __name__ == "__main__":  # pragma: no cover
    parser = argparse.ArgumentParser()    
    ...

Upvotes: 2

Related Questions