Skip to content

Commit

Permalink
Add test coverage collecting
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Jamim committed Oct 9, 2019
1 parent 9fa92f3 commit 4999fa4
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 34 deletions.
4 changes: 4 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[run]
omit =
*/tests/*
*/setup.py
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ venv*/
pip-log.txt

# Unit test / coverage reports
coverage.xml
.coverage
.nox
.tox
Expand Down
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ install:

script:
- tox

after_success:
- pip install codecov
- codecov -v
29 changes: 29 additions & 0 deletions coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

set -e

function cov {
pytest ${1} \
-c pytest-cov.ini \
--ignore-glob=*/setup.py \
--cov opentelemetry-api \
--cov opentelemetry-sdk \
--cov ext \
--cov examples \
--cov-append \
--cov-branch \
--cov-report=
}


coverage erase

cov opentelemetry-api
cov opentelemetry-sdk
cov ext/opentelemetry-ext-http-requests
cov ext/opentelemetry-ext-jaeger
cov ext/opentelemetry-ext-wsgi
cov examples/opentelemetry-example-app

coverage report
coverage xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 27 additions & 25 deletions opentelemetry-api/src/opentelemetry/context/async_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,34 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import typing # pylint: disable=unused-import
from contextvars import ContextVar
try:
from contextvars import ContextVar
except ImportError:
pass
else:
import typing # pylint: disable=unused-import
from . import base_context

from . import base_context
class AsyncRuntimeContext(base_context.BaseRuntimeContext):
class Slot(base_context.BaseRuntimeContext.Slot):
def __init__(self, name: str, default: "object"):
# pylint: disable=super-init-not-called
self.name = name
self.contextvar = ContextVar(name) # type: ContextVar[object]
self.default = base_context.wrap_callable(
default
) # type: typing.Callable[..., object]

def clear(self) -> None:
self.contextvar.set(self.default())

class AsyncRuntimeContext(base_context.BaseRuntimeContext):
class Slot(base_context.BaseRuntimeContext.Slot):
def __init__(self, name: str, default: "object"):
# pylint: disable=super-init-not-called
self.name = name
self.contextvar = ContextVar(name) # type: ContextVar[object]
self.default = base_context.wrap_callable(
default
) # type: typing.Callable[..., object]
def get(self) -> "object":
try:
return self.contextvar.get()
except LookupError:
value = self.default()
self.set(value)
return value

def clear(self) -> None:
self.contextvar.set(self.default())

def get(self) -> "object":
try:
return self.contextvar.get()
except LookupError:
value = self.default()
self.set(value)
return value

def set(self, value: "object") -> None:
self.contextvar.set(value)
def set(self, value: "object") -> None:
self.contextvar.set(value)
6 changes: 6 additions & 0 deletions pytest-cov.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[pytest]
addopts = -rs -v

# We need this to make pytest "touch" every python file
# in order to include them into the coverage report.
python_files = *.py
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
addopts = -rs -v
17 changes: 16 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ skip_missing_interpreters = True
envlist =
py3{4,5,6,7,8}-test-{api,sdk,example-app,ext-wsgi,ext-http-requests,ext-jaeger}
pypy3-test-{api,sdk,example-app,ext-wsgi,ext-http-requests,ext-jaeger}
py3{4,5,6,7,8}-coverage
pypy3-coverage
lint
py37-{mypy,mypyinstalled}
docs
Expand All @@ -14,6 +16,8 @@ python =

[testenv]
deps =
test: pytest
coverage: pytest-cov
mypy,mypyinstalled: mypy~=0.711

setenv =
Expand Down Expand Up @@ -42,12 +46,23 @@ commands_pre =
jaeger: pip install {toxinidir}/opentelemetry-sdk
jaeger: pip install {toxinidir}/ext/opentelemetry-ext-jaeger

; In order to get a healthy coverage report,
; we have to install packages in editable mode.
coverage: pip install -e {toxinidir}/opentelemetry-api
coverage: pip install -e {toxinidir}/opentelemetry-sdk
coverage: pip install -e {toxinidir}/ext/opentelemetry-ext-azure-monitor
coverage: pip install -e {toxinidir}/ext/opentelemetry-ext-http-requests
coverage: pip install -e {toxinidir}/ext/opentelemetry-ext-jaeger
coverage: pip install -e {toxinidir}/ext/opentelemetry-ext-wsgi
coverage: pip install -e {toxinidir}/examples/opentelemetry-example-app

; Using file:// here because otherwise tox invokes just "pip install
; opentelemetry-api", leading to an error
mypyinstalled: pip install file://{toxinidir}/opentelemetry-api/

commands =
test: python -m unittest discover
test: pytest
coverage: {toxinidir}/coverage.sh

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

0 comments on commit 4999fa4

Please sign in to comment.