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

Viewed 105

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?

1 Answers

You can add a comment, like this:

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