Skip to content

Commit 98ca260

Browse files
willpricesoumith
authored andcommitted
[travis] Record code coverage and display on README (#703)
Test installed version of package To test against the installed version of the package (which is preferably since this can catch installation file inclusion bugs) we have to deal with quirks of coverage.py. By default the ecosystem is set up to do development coverage tests (e.g. in src, or by using `pip install -e .` or `python setup.py develop`). We want to test against the version installed, to do this we have to find the install path which we'll then pass to the `--cov` arg added by `pytest-cov`. To do this, we have to cd out of the current folder, and import the installed version and get it's install path (if we don't cd out, then we end up getting the existing directory in the cwd since by default cwd is on `sys.path`) Once we have the install path, we pass that to the `--cov` of pytest, however we also want to rewrite the paths for codecov to pick them up on the website, if they don't have to code coverage with local paths, they don't register is properly. In order to that, we have a .coveragerc file that has the contents: ``` [paths] source = torchvision /**/site-packages/torchvision ``` This tells codecov to treat all paths with those prefixes as the same, so anything like `/home/travis/miniconda/envs/test-environment/lib/python2.7/site-packages/torchvision/models/__init__.py` would be treated the same as `torchvision/models/__init__.py` after running the coverage combination command. The first path seems to be special, in that all subsequent paths are rewritten to that one. Once we collect coverage, we then run `coverage run` to rewrite the installation paths in the coverage file, `.coverage`, to those expected by codecov. Phew. And that's it. N.B: Whilst adding test/__init__.py does solve the issue, it also results in PWD being added to the path, and then we'll test the development version of the package, and not the installed version (see https://docs.pytest.org/en/latest/goodpractices.html#tests-as-part-of-application-code)
1 parent 8f0ef5a commit 98ca260

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

.coveragerc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[run]
2+
branch = True
3+
4+
[paths]
5+
source =
6+
torchvision
7+
/**/site-packages/torchvision

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ torchvision.egg-info/
66
*/**/*~
77
*~
88
docs/build
9+
.coverage
10+
htmlcov

.travis.yml

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ matrix:
66
python: "2.7"
77
install: pip install flake8
88
script: flake8
9+
after_success: []
910
- python: "2.7"
1011
env: IMAGE_BACKEND=Pillow-SIMD
1112
- python: "2.7"
1213
- python: "3.5"
1314
env: IMAGE_BACKEND=Pillow-SIMD
1415
- python: "3.5"
1516

16-
install:
17+
before_install:
1718
- sudo apt-get update
1819
- wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
1920
- bash miniconda.sh -b -p $HOME/miniconda
@@ -26,11 +27,34 @@ install:
2627

2728
- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION pytorch scipy -c pytorch
2829
- source activate test-environment
29-
- python setup.py install
30-
- pip install --upgrade pytest
31-
- if [[ "$IMAGE_BACKEND" == "Pillow-SIMD" ]]; then
32-
pip uninstall -y pillow && CC="cc -march=native" pip install --force-reinstall pillow-simd;
30+
- |
31+
if [[ "$IMAGE_BACKEND" == "Pillow-SIMD" ]]; then
32+
pip uninstall -y pillow && CC="cc -march=native" pip install --force-reinstall pillow-simd
3333
fi
34+
- pip install pytest pytest-cov codecov
35+
36+
37+
install:
38+
# Using pip instead of setup.py ensures we install a non-compressed version of the package
39+
# (as opposed to an egg), which is necessary to collect coverage.
40+
# We still get the benefit of testing an installed version over the
41+
# test version to iron out installation file-inclusion bugs but can
42+
# also collect coverage.
43+
- pip install .
44+
# Move to home dir, otherwise we'll end up with the path to the
45+
# package in $PWD rather than the installed v
46+
- |
47+
cd $HOME
48+
export TV_INSTALL_PATH="$(python -c 'import os; import torchvision; print(os.path.dirname(os.path.abspath(torchvision.__file__)))')"
49+
echo "$TV_INSTALL_PATH"
50+
cd -
51+
3452
script:
35-
- pytest test/
53+
- pytest --cov-config .coveragerc --cov torchvision --cov $TV_INSTALL_PATH test
3654

55+
after_success:
56+
# Necessary to run coverage combine to rewrite paths from
57+
# /travis/env/path/site-packages/torchvision to actual path
58+
- coverage combine .coverage
59+
- coverage report
60+
- codecov

README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ torchvision
44
.. image:: https://travis-ci.org/pytorch/vision.svg?branch=master
55
:target: https://travis-ci.org/pytorch/vision
66

7+
.. image:: https://codecov.io/gh/pytorch/vision/branch/master/graph/badge.svg
8+
:target: https://codecov.io/gh/pytorch/vision
9+
710
.. image:: https://pepy.tech/badge/torchvision
811
:target: https://pepy.tech/project/torchvision
912

1013
.. image:: https://img.shields.io/badge/dynamic/json.svg?label=docs&url=https%3A%2F%2Fpypi.org%2Fpypi%2Ftorchvision%2Fjson&query=%24.info.version&colorB=brightgreen&prefix=v
1114
:target: https://pytorch.org/docs/stable/torchvision/index.html
1215

16+
1317
The torchvision package consists of popular datasets, model architectures, and common image transformations for computer vision.
1418

1519
Installation

0 commit comments

Comments
 (0)