Skip to content

Instantly share code, notes, and snippets.

@gamorales
Created March 6, 2021 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamorales/e843e0a059a8d6aa8e53608623774546 to your computer and use it in GitHub Desktop.
Save gamorales/e843e0a059a8d6aa8e53608623774546 to your computer and use it in GitHub Desktop.
Test coverage
def foo():
return 5
def main():
x = foo()
print(x)
if __name__ == '__main__': # pragma: no cover
main()
## test code
# To test:
# pip install pytest
# pytest script.py
# To test with coverage:
# put this file (script.py) in a directory by itself, say foo
# then from the parent directory of foo:
# pip install pytest-cov
# pytest --cov=foo foo/script.py
# To show missing lines
# pytest --cov=foo --cov-report=term-missing foo/script.py
def test_foo():
assert foo() == 5
def test_main(capsys):
main()
captured = capsys.readouterr()
assert captured.out == "5\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment