Coveragepy: who-tests-what: empty context names

Created on 15 Apr 2019  路  4Comments  路  Source: nedbat/coveragepy

I have been using who-tests-what and wanted to ask about the empty context names I have found and perhaps have this as a future reference.

For example, if I have 2 files:

A source file source_1.py

2  def multiply(a, b):
3          return a * b
4

and a test file test_file_1.py

1  import source_1
2
3  def test_source1():
4          assert 30 == source_1.multiply(5,6)

with .coveragerc as follows:

[run]
dynamic_context = test_function

Then using coverage (version 5.0a4 with C extension) I run the tests and generate the coverage DB .coverage:

$ py.test --cov=. test_file_1.py
...
$ sqlite3 .coverage "select * from context;"
1|
2|test_source1

As you can see, the first context is empty. Why is that? What does it mean?

This is what the DB looks like:

$ sqlite3 .coverage "select * from file, context, line where file.id = line.file_id and line.context_id=context.id"
1|/.../workspace/coverage_tests/test_file_1.py|1||1|1|1
1|/.../workspace/coverage_tests/test_file_1.py|1||1|1|3
2|/.../workspace/coverage_tests/source_1.py|1||2|1|2
1|/.../workspace/coverage_tests/test_file_1.py|2|test_source1|1|2|4
2|/.../workspace/coverage_tests/source_1.py|2|test_source1|2|2|3

My Environment

Python 3.6.5
Coverage 5.0a4 with C extension

enhancement

Most helpful comment

I added a clarifying paragraph in 8558658bdebaa8e0d2885f97585ba8d9f5fb48ce

All 4 comments

I think I have found the answer to my own question. The empty contexts refer to the lines that are touched at import time, when import source_1 happens. No test was running at that time, therefore no contexts (since we asked for dynamic_context = test_function).

I have verified this by moving import source_1 into the test body and re-running:

def test_source1():
    import source_1
    assert 30 == source_1.multiply(5,6)

Now the coverage data is as follows:

sqlite3 .coverage "select * from file, context, line where file.id = line.file_id and line.context_id=context.id"
1|/.../workspace/coverage_tests/test_file_1.py|1||1|1|2
1|/.../workspace/coverage_tests/test_file_1.py|2|test_source1|1|2|3
1|/.../workspace/coverage_tests/test_file_1.py|2|test_source1|1|2|4
2|/.../workspace/coverage_tests/source_1.py|2|test_source1|2|2|2
2|/.../workspace/coverage_tests/source_1.py|2|test_source1|2|2|3

Is there any other scenario in which I will have empty contexts?

You could also have an empty context between two tests, though the code that runs between tests has likely already been run before the tests, and also likely is not in your own code.

Should we handle this differently somehow?

Should we handle this differently somehow?

Hm, is there any scenario in which coverage can't be measured? and if so, what would be the context string in those cases? Otherwise, I couldn't think of any other downsides.

I added a clarifying paragraph in 8558658bdebaa8e0d2885f97585ba8d9f5fb48ce

Was this page helpful?
0 / 5 - 0 ratings