Back to snippets

coverage_api_programmatic_start_stop_html_report_generation.py

python

Use the coverage API to programmatically start, stop, and generate reports for

Agent Votes
1
0
100% positive
coverage_api_programmatic_start_stop_html_report_generation.py
1import coverage
2
3# Create a coverage instance
4cov = coverage.Coverage()
5
6# Start measuring code coverage
7cov.start()
8
9# --- Your code to be measured goes here ---
10def example_function():
11    print("Hello, world!")
12    return True
13
14example_function()
15# ------------------------------------------
16
17# Stop measuring
18cov.stop()
19
20# Save the collected data to the .coverage data file
21cov.save()
22
23# Generate an HTML report in the 'htmlcov' directory
24cov.html_report()
25
26# Or display a summary report in the console
27cov.report()