Skip to content

Instantly share code, notes, and snippets.

@koepnick
Last active August 7, 2024 16:30
Show Gist options
  • Save koepnick/667c3b5881c38e2620e812e1b8a5fbc8 to your computer and use it in GitHub Desktop.
Save koepnick/667c3b5881c38e2620e812e1b8a5fbc8 to your computer and use it in GitHub Desktop.
Examples of Python Testing

These can be used as pre-commit tests or as part of a broader testing strategy.

While I consider these examples to be sane, there are a plethora of additional options available. I'm still using the pytest library. But this should still work when using the unittest module now part of the standard library.

I've also kept these reasonably quiet and only checking for the most egregious problems.

The assumption is that we're using Poetry as a builder.

Additionally, the output from these tests will be saved as a JUnit XML file for future parsing.

We'll be testing for:

This is a pretty bare bones set of examples. The actual implementation will vary. Please don't simply copy and paste.

[tool.pytest.ini_options]
testpaths = [
"tests"
]
[tool.pylint.format]
max-line-length = 120
[tool.pylint.MASTER]
fail-under = 9.0
ignore-patterns = ''
jobs = 0
ignore = '.git'
[tool.pylint."MESSAGES CONTROL"]
disable = 'E,W,C,R' # Clean slate. No implicit checks. Error, Warning, Convention, Refactor
enable = 'W0612,W0611,W0120,C' # We care about a few Warnings and all Convention messages
[tool.pylint.REPORTS]
output-format = 'json' # Plain, simple, JSON. https://pylint.pycqa.org/en/latest/user_guide/usage/output.html
# Examples of overriding the global settings on a per message type level.
[tool.pylint.REFACTORING]
max-nested-blocks = 3
never-returning-functions = 'sys.exit'
[tool.pylint.LOGGING]
logging-format-style = 'new'
logging-modules = 'logging'
[tool.pylint.MISCELLANEOUS]
notes = '''FIXME,
TODO,
'''
notes-rgx = '[A-Z]+:.*' # Capture any all caps string, terminated by a ':'.
[tool.mypy]
disallow_untyped_defs = true
disallow_incomplete_defs = true
disallow_untyped_calls = true
#exclude = []
#ignore_missing_imports = true
#!/bin/bash
poetry run pylint path/to/src --exit-zero
poetry run pytest --junitxml=test-results/pytest.xml
poetry run coverage run -m pytest
"""
We can leverage the pytest (or unittest) checks to also
check for code coverage as, in theory, the unit tests
should be executing as much of the codebase as cna be
executed
"""
poetry run coverage report
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment