The CoverageData class
Added in version 4.0.
- class coverage.CoverageData(basename=None, suffix=None, no_disk=False, warn=None, debug=None)
Manages collected coverage data, including file storage.
This class is the public supported API to the data that coverage.py collects during program execution. It includes information about what code was executed. It does not include information from the analysis phase, to determine what lines could have been executed, or what lines were not executed.
Note
The data file is currently a SQLite database file, with a documented schema. The schema is subject to change though, so be careful about querying it directly. Use this API if you can to isolate yourself from changes.
There are a number of kinds of data that can be collected:
lines: the line numbers of source lines that were executed. These are always available.
arcs: pairs of source and destination line numbers for transitions between source lines. These are only available if branch coverage was used.
file tracer names: the module names of the file tracer plugins that handled each file in the data.
Lines, arcs, and file tracer names are stored for each source file. File names in this API are case-sensitive, even on platforms with case-insensitive file systems.
A data file either stores lines, or arcs, but not both.
A data file is associated with the data when the
CoverageData
is created, using the parameters basename, suffix, and no_disk. The base name can be queried withbase_filename()
, and the actual file name being used is available fromdata_filename()
.To read an existing coverage.py data file, use
read()
. You can then access the line, arc, or file tracer data withlines()
,arcs()
, orfile_tracer()
.The
has_arcs()
method indicates whether arc data is available. You can get a set of the files in the data withmeasured_files()
. As with most Python containers, you can determine if there is any data at all by using this object as a boolean value.The contexts for each line in a file can be read with
contexts_by_lineno()
.To limit querying to certain contexts, use
set_query_context()
orset_query_contexts()
. These will narrow the focus of subsequentlines()
,arcs()
, andcontexts_by_lineno()
calls. The set of all measured context names can be retrieved withmeasured_contexts()
.Most data files will be created by coverage.py itself, but you can use methods here to create data files if you like. The
add_lines()
,add_arcs()
, andadd_file_tracers()
methods add data, in ways that are convenient for coverage.py.To record data for contexts, use
set_context()
to set a context to be used for subsequentadd_lines()
andadd_arcs()
calls.To add a source file without any measured data, use
touch_file()
, ortouch_files()
for a list of such files.Write the data to its file with
write()
.You can clear the data in memory with
erase()
. Data for specific files can be removed from the database withpurge_files()
.Two data collections can be combined by using
update()
on oneCoverageData
, passing it the other.Data in a
CoverageData
can be serialized and deserialized withdumps()
andloads()
.The methods used during the coverage.py collection phase (
add_lines()
,add_arcs()
,set_context()
, andadd_file_tracers()
) are thread-safe. Other methods may not be.- Parameters:
- __init__(basename=None, suffix=None, no_disk=False, warn=None, debug=None)
Create a
CoverageData
object to hold coverage-measured data.- Parameters:
basename (str) – the base name of the data file, defaulting to “.coverage”. This can be a path to a file in another directory.
suffix (str or bool) – has the same meaning as the data_suffix argument to
coverage.Coverage
.no_disk (bool) – if True, keep all data in memory, and don’t write any disk file.
warn (TWarnFn | None) – a warning callback function, accepting a warning message argument.
debug (TDebugCtl | None) – a DebugControl object (optional)
- Return type:
None
- add_arcs(arc_data)
Add measured arc data.
arc_data is a dictionary mapping file names to iterables of pairs of ints:
{ filename: { (l1,l2), (l1,l2), ... }, ...}
- add_file_tracers(file_tracers)
Add per-file plugin information.
file_tracers is { filename: plugin_name, … }
- add_lines(line_data)
Add measured line data.
line_data is a dictionary mapping file names to iterables of ints:
{ filename: { line1, line2, ... }, ...}
- Parameters:
line_data (Mapping[str, Collection[int]])
- Return type:
None
- arcs(filename)
Get the list of arcs executed for a file.
If the file was not measured, returns None. A file might be measured, and have no arcs executed, in which case an empty list is returned.
If the file was executed, returns a list of 2-tuples of integers. Each pair is a starting line number and an ending line number for a transition from one line to another. The list is in no particular order.
Negative numbers have special meaning. If the starting line number is -N, it represents an entry to the code object that starts at line N. If the ending ling number is -N, it’s an exit from the code object that starts at line N.
- contexts_by_lineno(filename)
Get the contexts for each line in a file.
- Returns:
A dict mapping line numbers to a list of context names.
- Parameters:
filename (str)
- Return type:
Added in version 5.0.
- dumps()
Serialize the current data to a byte string.
The format of the serialized data is not documented. It is only suitable for use with
loads()
in the same version of coverage.py.Note that this serialization is not what gets stored in coverage data files. This method is meant to produce bytes that can be transmitted elsewhere and then deserialized with
loads()
.- Returns:
A byte string of serialized data.
- Return type:
Added in version 5.0.
- erase(parallel=False)
Erase the data in this object.
If parallel is true, then also deletes data files created from the basename by parallel-mode.
- Parameters:
parallel (bool)
- Return type:
None
- file_tracer(filename)
Get the plugin name of the file tracer for a file.
Returns the name of the plugin that handles this file. If the file was measured, but didn’t use a plugin, then “” is returned. If the file was not measured, then None is returned.
- lines(filename)
Get the list of lines executed for a source file.
If the file was not measured, returns None. A file might be measured, and have no lines executed, in which case an empty list is returned.
If the file was executed, returns a list of integers, the line numbers executed in the file. The list is in no particular order.
- loads(data)
Deserialize data from
dumps()
.Use with a newly-created empty
CoverageData
object. It’s undefined what happens if the object already has data in it.Note that this is not for reading data from a coverage data file. It is only for use on data you produced with
dumps()
.Added in version 5.0.
- measured_contexts()
A set of all contexts that have been measured.
Added in version 5.0.
- measured_files()
A set of all files that have been measured.
Note that a file may be mentioned as measured even though no lines or arcs for that file are present in the data.
- purge_files(filenames)
Purge any existing coverage data for the given filenames.
Added in version 7.2.
- Parameters:
filenames (Collection[str])
- Return type:
None
- read()
Start using an existing data file.
- Return type:
None
- set_context(context)
Set the current context for future
add_lines()
etc.context is a str, the name of the context to use for the next data additions. The context persists until the next
set_context()
.Added in version 5.0.
- Parameters:
context (str | None)
- Return type:
None
- set_query_context(context)
Set a context for subsequent querying.
The next
lines()
,arcs()
, orcontexts_by_lineno()
calls will be limited to only one context. context is a string which must match a context exactly. If it does not, no exception is raised, but queries will return no data.Added in version 5.0.
- Parameters:
context (str)
- Return type:
None
- set_query_contexts(contexts)
Set a number of contexts for subsequent querying.
The next
lines()
,arcs()
, orcontexts_by_lineno()
calls will be limited to the specified contexts. contexts is a list of Python regular expressions. Contexts will be matched usingre.search
. Data will be included in query results if they are part of any of the contexts matched.Added in version 5.0.
- classmethod sys_info()
Our information for Coverage.sys_info.
Returns a list of (key, value) pairs.
- touch_file(filename, plugin_name='')
Ensure that filename appears in the data, empty if needed.
plugin_name is the name of the plugin responsible for this file. It is used to associate the right filereporter, etc.
- touch_files(filenames, plugin_name=None)
Ensure that filenames appear in the data, empty if needed.
plugin_name is the name of the plugin responsible for these files. It is used to associate the right filereporter, etc.
- Parameters:
filenames (Collection[str])
plugin_name (str | None)
- Return type:
None
- update(other_data, map_path=None)
Update this data with data from another
CoverageData
.If map_path is provided, it’s a function that re-map paths to match the local machine’s. Note: map_path is None only when called directly from the test suite.
- Parameters:
other_data (CoverageData)
- Return type:
None
- write()
Ensure the data is written to the data file.
- Return type:
None