Python Tips: On Getting Started, Unit Testing and Code Coverage
tl;dr Here is a collection of useful Python tips, a starter “Hello World” Python template for both web (using Flask) and stand-alone script. And additional tips in writing effective Python code, getting started, Unit Tests and using the Coverage tool to improve code.

Photo by Fabian Grohs on Unsplash
Getting Started - Introducing “Python Hello World” Template

Sometimes when we need to get started in Python, we need an easy “Hello World” template that gets us started with all the basics including unit test and coverage reports. I’ve searched and searched and I have not found something that fits my needs and so I decided to create one. I have placed it in the public domain for others to use under MIT license (free as water). If it solves a little bit of your headache, send me a note and share your feedback. Don’t forget to share the link with others. 😊
In summary, if you’re trying to get started with a Python 3+ project, then feel free to use the template I have for you.
Link to the public repository (on GitHub): Hello World Python Starter Kit
First step, choose your poison:
- For Linux
- For Windows (work pending)
- For GitHub Users
For nix* (Linux) environment:
For Windows environment: (work pending)
For GitHub account holders:
What is in the ‘Hello World’ project (File Structure):
Output from Tests and Coverage script:
How did I achieve a 100% coverage
Examine file: /nix/run_tests_and_coverage.sh
- Use effective “omit” and “include” options to only include (and exclude) what you want (and don’t want) in your coverage.
- By default, Coverage will include all python files including the ones that are under “/test” folder. Exclude the tests from coverage.
- By default, Coverage will include all the “init.py” files that marks the folder as a module to Python. These files are always 100%. Why include them in the report. Adds too much noise. Omit them from the report.
- Sometimes your debug code like print(f’Full Name: {person.name}’) maybe enclosed in a “if DEBUG:” clause, and that particular branch does not serve any useful purpose other than debugging the variables. You don’t need to cover them in your tests. To avoid this use “# pragma: no cover” (see this link for more details)
- In your production code use effective “# pragma: no cover” to denote areas which you don’t want to include in your coverage.
- If you have a block of code (multiple lines) that you want to exclude, you don’t want to add “# pragma: no cover” to every line, use something like:
The above sample will ignore all four lines from the coverage while the code is syntactically the same without the “if True:” (from the Python Compilers point-of-view. That’s another future article. But for now, if you want to learn more about Python ByteCode, check out dis [stands for “disassembler”] - or here.)
Using the tips above, you’ll be tempted to go globally and remove all code from coverage to reach that 100%. Don’t do that. By doing so, you will pay back later when your code is missing real coverage and the issue creeps in production. So find your right balance between writing unit tests and coverage.
If you find this article useful, consider bookmarking, subscribing and/or sharing it on your favorite social channels so that others can also find and read these articles. I do this out of love and passion to share my ideas, thoughts, findings and knowledge with others. So the more you help me share, the more my content can reach others.
Thank you for helping me spread the word.
- Find your passion and inspiration today!
- Love what you do [and when life gives you lemons, make lemonade (or better - an Arnold Palmer drink) 🍋+🍵 = 🍹]
- Help someone else discover their passion!