Skip to content

Commit c9ddd65

Browse files
committed
Add test coverage collecting
I believe that we should be able to see the test coverage. These changes: - make tests use pytest - add displaying of the test coverage - add sending of collected coverage data to https://codecov.io
1 parent 6d1cd1f commit c9ddd65

File tree

10 files changed

+103
-34
lines changed

10 files changed

+103
-34
lines changed

.coveragerc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[run]
2+
omit =
3+
*/tests/*
4+
*/setup.py

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ venv*/
2525
pip-log.txt
2626

2727
# Unit test / coverage reports
28+
coverage.xml
2829
.coverage
2930
.nox
3031
.tox

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ install:
1919

2020
script:
2121
- tox
22+
23+
after_success:
24+
- pip install codecov
25+
- codecov -v

ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/gen/agent/Agent.py

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/gen/agent/ttypes.py

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

opentelemetry-api/src/opentelemetry/context/async_context.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,34 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import typing # pylint: disable=unused-import
16-
from contextvars import ContextVar
15+
try:
16+
from contextvars import ContextVar
17+
except ImportError:
18+
pass
19+
else:
20+
import typing # pylint: disable=unused-import
21+
from . import base_context
1722

18-
from . import base_context
23+
class AsyncRuntimeContext(base_context.BaseRuntimeContext):
24+
class Slot(base_context.BaseRuntimeContext.Slot):
25+
def __init__(self, name: str, default: object):
26+
# pylint: disable=super-init-not-called
27+
self.name = name
28+
self.contextvar = ContextVar(name) # type: ContextVar[object]
29+
self.default = base_context.wrap_callable(
30+
default
31+
) # type: typing.Callable[..., object]
1932

33+
def clear(self) -> None:
34+
self.contextvar.set(self.default())
2035

21-
class AsyncRuntimeContext(base_context.BaseRuntimeContext):
22-
class Slot(base_context.BaseRuntimeContext.Slot):
23-
def __init__(self, name: str, default: "object"):
24-
# pylint: disable=super-init-not-called
25-
self.name = name
26-
self.contextvar = ContextVar(name) # type: ContextVar[object]
27-
self.default = base_context.wrap_callable(
28-
default
29-
) # type: typing.Callable[..., object]
36+
def get(self) -> object:
37+
try:
38+
return self.contextvar.get()
39+
except LookupError:
40+
value = self.default()
41+
self.set(value)
42+
return value
3043

31-
def clear(self) -> None:
32-
self.contextvar.set(self.default())
33-
34-
def get(self) -> "object":
35-
try:
36-
return self.contextvar.get()
37-
except LookupError:
38-
value = self.default()
39-
self.set(value)
40-
return value
41-
42-
def set(self, value: "object") -> None:
43-
self.contextvar.set(value)
44+
def set(self, value: object) -> None:
45+
self.contextvar.set(value)

pytest-cov.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[pytest]
2+
addopts = -rs -v
3+
4+
# We need this to make pytest "touch" every python file
5+
# in order to include them into the coverage report.
6+
python_files = *.py

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
addopts = -rs -v

scripts/coverage.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
function cov {
6+
pytest ${1} \
7+
-c pytest-cov.ini \
8+
--ignore-glob=*/setup.py \
9+
--cov opentelemetry-api \
10+
--cov opentelemetry-sdk \
11+
--cov ext \
12+
--cov examples \
13+
--cov-append \
14+
--cov-branch \
15+
--cov-report=
16+
}
17+
18+
19+
coverage erase
20+
21+
cov opentelemetry-api
22+
cov opentelemetry-sdk
23+
cov ext/opentelemetry-ext-http-requests
24+
cov ext/opentelemetry-ext-jaeger
25+
cov ext/opentelemetry-ext-opentracing-shim
26+
cov ext/opentelemetry-ext-wsgi
27+
cov examples/opentelemetry-example-app
28+
29+
coverage report
30+
coverage xml

tox.ini

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ skip_missing_interpreters = True
44
envlist =
55
py3{4,5,6,7,8}-test-{api,sdk,example-app,ext-wsgi,ext-http-requests,ext-jaeger,opentracing-shim}
66
pypy3-test-{api,sdk,example-app,ext-wsgi,ext-http-requests,ext-jaeger,opentracing-shim}
7+
py3{4,5,6,7,8}-coverage
8+
9+
; Coverage is temporarily disabled for pypy3 due to the pytest bug.
10+
; pypy3-coverage
11+
712
lint
813
py37-tracecontext
914
py37-{mypy,mypyinstalled}
@@ -15,6 +20,8 @@ python =
1520

1621
[testenv]
1722
deps =
23+
test: pytest
24+
coverage: pytest-cov
1825
mypy,mypyinstalled: mypy~=0.740
1926

2027
setenv =
@@ -45,12 +52,24 @@ commands_pre =
4552
jaeger: pip install {toxinidir}/ext/opentelemetry-ext-jaeger
4653
opentracing-shim: pip install {toxinidir}/opentelemetry-sdk {toxinidir}/ext/opentelemetry-ext-opentracing-shim
4754

55+
; In order to get a healthy coverage report,
56+
; we have to install packages in editable mode.
57+
coverage: pip install -e {toxinidir}/opentelemetry-api
58+
coverage: pip install -e {toxinidir}/opentelemetry-sdk
59+
coverage: pip install -e {toxinidir}/ext/opentelemetry-ext-azure-monitor
60+
coverage: pip install -e {toxinidir}/ext/opentelemetry-ext-http-requests
61+
coverage: pip install -e {toxinidir}/ext/opentelemetry-ext-jaeger
62+
coverage: pip install -e {toxinidir}/ext/opentelemetry-ext-opentracing-shim
63+
coverage: pip install -e {toxinidir}/ext/opentelemetry-ext-wsgi
64+
coverage: pip install -e {toxinidir}/examples/opentelemetry-example-app
65+
4866
; Using file:// here because otherwise tox invokes just "pip install
4967
; opentelemetry-api", leading to an error
5068
mypyinstalled: pip install file://{toxinidir}/opentelemetry-api/
5169

5270
commands =
53-
test: python -m unittest discover
71+
test: pytest
72+
coverage: {toxinidir}/scripts/coverage.sh
5473

5574
mypy: mypy --namespace-packages opentelemetry-api/src/opentelemetry/
5675
; For test code, we don't want to enforce the full mypy strictness

0 commit comments

Comments
 (0)