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

[MRG + 1] Make it possible to run doctests in .rst files with pytest #9697

Merged
merged 1 commit into from
Sep 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions build_tools/travis/test_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ run_tests() {
fi
$TEST_CMD sklearn

# Test doc (only with nose until we switch completely to pytest)
if [[ "$USE_PYTEST" != "true" ]]; then
# Going back to git checkout folder needed for make test-doc
cd $OLDPWD
# Going back to git checkout folder needed to test documentation
cd $OLDPWD

if [[ "$USE_PYTEST" == "true" ]]; then
pytest $(find doc -name '*.rst' | sort)
else
# Makefile is using nose
make test-doc
fi
}
Expand Down
Empty file added conftest.py
Empty file.
75 changes: 75 additions & 0 deletions doc/datasets/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from os.path import exists
from os.path import join

import numpy as np

from sklearn.utils.testing import SkipTest
from sklearn.utils.testing import check_skip_network
from sklearn.datasets import get_data_home
from sklearn.utils.testing import install_mldata_mock
from sklearn.utils.testing import uninstall_mldata_mock


def setup_labeled_faces():
data_home = get_data_home()
if not exists(join(data_home, 'lfw_home')):
raise SkipTest("Skipping dataset loading doctests")


def setup_mldata():
# setup mock urllib2 module to avoid downloading from mldata.org
install_mldata_mock({
'mnist-original': {
'data': np.empty((70000, 784)),
'label': np.repeat(np.arange(10, dtype='d'), 7000),
},
'iris': {
'data': np.empty((150, 4)),
},
'datasets-uci-iris': {
'double0': np.empty((150, 4)),
'class': np.empty((150,)),
},
})


def teardown_mldata():
uninstall_mldata_mock()


def setup_rcv1():
check_skip_network()
# skip the test in rcv1.rst if the dataset is not already loaded
rcv1_dir = join(get_data_home(), "RCV1")
if not exists(rcv1_dir):
raise SkipTest("Download RCV1 dataset to run this test.")


def setup_twenty_newsgroups():
data_home = get_data_home()
if not exists(join(data_home, '20news_home')):
raise SkipTest("Skipping dataset loading doctests")


def setup_working_with_text_data():
check_skip_network()


def pytest_runtest_setup(item):
fname = item.fspath.strpath
if fname.endswith('datasets/labeled_faces.rst'):
setup_labeled_faces()
elif fname.endswith('datasets/mldata.rst'):
setup_mldata()
elif fname.endswith('datasets/rcv1.rst'):
setup_rcv1()
elif fname.endswith('datasets/twenty_newsgroups.rst'):
setup_twenty_newsgroups()
elif fname.endswith('datasets/working_with_text_data.rst'):
setup_working_with_text_data()


def pytest_runtest_teardown(item):
fname = item.fspath.strpath
if fname.endswith('datasets/mldata.rst'):
teardown_mldata()
10 changes: 10 additions & 0 deletions doc/datasets/mldata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

>>> import numpy as np
>>> import os
>>> import tempfile
>>> # Create a temporary folder for the data fetcher
>>> custom_data_home = tempfile.mkdtemp()
>>> os.makedirs(os.path.join(custom_data_home, 'mldata'))


.. _mldata:

Expand Down Expand Up @@ -70,3 +75,8 @@ defaults to individual datasets:
... data_home=custom_data_home)
>>> iris3 = fetch_mldata('datasets-UCI iris', target_name='class',
... data_name='double0', data_home=custom_data_home)


..
>>> import shutil
>>> shutil.rmtree(custom_data_home)
15 changes: 0 additions & 15 deletions doc/datasets/mldata_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,12 @@
Mock urllib2 access to mldata.org and create a temporary data folder.
"""

from os import makedirs
from os.path import join
import numpy as np
import tempfile
import shutil

from sklearn import datasets
from sklearn.utils.testing import install_mldata_mock
from sklearn.utils.testing import uninstall_mldata_mock


def globs(globs):
# Create a temporary folder for the data fetcher
global custom_data_home
custom_data_home = tempfile.mkdtemp()
makedirs(join(custom_data_home, 'mldata'))
globs['custom_data_home'] = custom_data_home
return globs


def setup_module():
# setup mock urllib2 module to avoid downloading from mldata.org
install_mldata_mock({
Expand All @@ -42,4 +28,3 @@ def setup_module():

def teardown_module():
uninstall_mldata_mock()
shutil.rmtree(custom_data_home)