Skip to main content

A tool to extract "sketches" of Python source files based on coverage data.

Project description

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

  1. Version Compatibility Handling:
    • Supports Python 3+
    • Uses different AST node types (Ellipsis vs Constant) depending on Python version (pre-3.8 vs 3.8+)
    • Uses astor.to_source for Python < 3.9 and ast.unparse for 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
  1. First generate coverage data by running your tests with coverage:

    coverage run -m pytest  # or whatever test command you use
    
  2. 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

coverage2sketch-0.1.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

coverage2sketch-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file coverage2sketch-0.1.0.tar.gz.

File metadata

  • Download URL: coverage2sketch-0.1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for coverage2sketch-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6beb724764435b27489d1e9bd04888f4830053ef341de2be45aa67c35fcf134d
MD5 3e6d5bacb4679f2db0d43fa7d154de27
BLAKE2b-256 e3ffa84d187f6dc7976fc269764d2ef2bab59e713299a5fb6d34a9accd5ddf6c

See more details on using hashes here.

File details

Details for the file coverage2sketch-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for coverage2sketch-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9ee1ee90952dbf6c2be4c9fcbd26795f204b753f8985f64ae3c4f8440dc4e172
MD5 171d524b8f60ed92f33a90c7340b7a4c
BLAKE2b-256 4dc19502964d13073e8b533be7ebc35c0876c2a5f602b6ea09745468f3ffdb17

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page