Note: mainly this part comes from TezRomacH/python-package-template since it combines good tools I used to know, and some new analysis instruments.
-
FastAPI is good web-framework due to explicit typing requirements, automated documentation, fast backend.
-
Pytest - flexible test framework
- Mypy - static type checker for python
- Safety - validates requirements for known security vulnerabilities
- Bandit - python-code analyzer for common vulnerable code patterns
- darglint - documentation checker. Validates that documentation corresponds to function implementation by signature
- editorconfig - a tool that allows to describe style rules understandable by many IDEs out-of-box
- isort - import sorter for reading ease
- black - good code formatter
- pyupgrade - tool that replaces deprecated syntax to updated alternatives
- pre-commit - a tool to simplify management of pre-commit github hooks.
Used hooks:
- common hooks from pre-commit official repo for yaml-validation and newline fixer
- hooks for formatters
- hook for bandit
- gitignore.io - a website that generates gitignore files for popular tools
- Make - handy tool to describe commonly used commands in the project. For example: build, lint, cache-clear
As mentioned before, the testing is done using pytest framework, since it:
- provides easy interface for testing
- has fixture mechanism for state preparation (and dependency resolution between fixtures)
- is a flexible framework allowing test parametrization and custom plugins
Unit testing practices:
-
Refactor code, so we can test one piece of functionality at a time
-
Tests must be fully independent. In particular:
- pass regardless the order of execution
- run on fresh dataset every time
-
Have small readable tests. Usually we can separate 3 parts of a test:
- setup: preparation of data. Often common for several tests so can be factored out into fixtures
- test: asserting a state
- possibly, teardown: if needed -- destruction of data
-
If there are not a lot of tests, we can add running test into pre-commit hooks so that developers do not push a broken code
-
Unit tests should be fast in order not to slow down development. If there are slow test, they should be filtered out and run periodically in a CI job.
-
Keep track of test coverage to verify that new code is at least covered by tests