Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

who-tests-what: empty context names #796

Closed
fersarr opened this issue Apr 15, 2019 · 4 comments
Closed

who-tests-what: empty context names #796

fersarr opened this issue Apr 15, 2019 · 4 comments
Labels
enhancement New feature or request

Comments

@fersarr
Copy link

fersarr commented Apr 15, 2019

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

1
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

@fersarr fersarr added the enhancement New feature or request label Apr 15, 2019
@fersarr
Copy link
Author

fersarr commented Apr 15, 2019

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?

@nedbat
Copy link
Owner

nedbat commented Apr 15, 2019

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?

@fersarr
Copy link
Author

fersarr commented Apr 16, 2019

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.

nedbat added a commit that referenced this issue May 7, 2019
@nedbat
Copy link
Owner

nedbat commented May 7, 2019

I added a clarifying paragraph in 8558658

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants