Coverage2Sketch
This code implements a tool to extract "sketches" of Python source files based on coverage data.
In a "sketch":
- Only code that was executed (covered) remains
- Uncovered code is replaced with
...placeholders - Consecutive placeholders are collapsed
- Comments and docstrings are removed
This is helpful for:
- Understanding Test Coverage: Quickly visualize which parts of your code are actually executed during testing.
- Code Visualization: Generate a text-based representation of the code's execution flow.
- Debugging: Pinpoint areas of code that are not reached or have potential issues.
- Code Review: Assist in identifying dead code or areas requiring more thorough testing.
Features
- Version Compatibility Handling:
- Supports Python 3+
- Uses different AST node types (
EllipsisvsConstant) depending on Python version (pre-3.8 vs 3.8+) - Uses
astor.to_sourcefor Python < 3.9 andast.unparsefor Python ≥ 3.9
Installation
pip install coverage2sketch
Usage as an Executable Module
When used as an executable module (i.e., python -m coverage2sketch -c .coverage), the tool outputs Markdown-formatted results showing:
- Each measured file path in backticks
- The code sketch in a Python code block
-
First generate coverage data by running your tests with coverage:
coverage run -m pytest # or whatever test command you use
-
Then generate sketches from the coverage data:
python -m coverage2sketch -c .coverage | tee sketches.md # Save to file
Command Line Arguments
| Argument | Description |
|---|---|
-c/--coverage-file |
Path to the .coverage data file (default: .coverage) |
Example Output
/path/to/module.py:
def covered_function():
print("This was executed")
...
def another_covered_function():
x = 1
return x
/path/to/other_module.py:
class TestedClass:
def method(self):
return True
...
Usage as a Package
When used as a a package, the function extract_sketches(coverage_file_path: str) -> Dict[str, str] returns a dictionary with file path as key and execution trace as value.
from coverage2sketch import extract_sketches
# Example usage
sketches = extract_sketches('/some/directory/.coverage')
# Output the sketches for each module
for file_path, sketch in sketches.items():
print('File: %s' % file_path)
print(sketch)
print('-' * 20)
Contributing
Contributions are welcome! Please submit pull requests or open issues on GitHub. (Replace with your actual repository URL.)
License
This project is licensed under the MIT License. See the LICENSE file for details.
Release files for coverage2sketch 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| coverage2sketch-0.1.0.tar.gz | 4.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| coverage2sketch-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.4 kB
Release files / coverage2sketch-0.1.0.tar.gz
| Download URL | coverage2sketch-0.1.0.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6beb724764435b27489d1e9bd04888f4830053ef341de2be45aa67c35fcf134d
|
|
BLAKE2b-256 checksum How to use checksums |
e3ffa84d187f6dc7976fc269764d2ef2bab59e713299a5fb6d34a9accd5ddf6c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.10
|
Release files / coverage2sketch-0.1.0-py3-none-any.whl
| Download URL | coverage2sketch-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9ee1ee90952dbf6c2be4c9fcbd26795f204b753f8985f64ae3c4f8440dc4e172
|
|
BLAKE2b-256 checksum How to use checksums |
4dc19502964d13073e8b533be7ebc35c0876c2a5f602b6ea09745468f3ffdb17
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.10
|