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

Make tests fail gracefully if optional deps are missing #557

Merged
merged 1 commit into from
Feb 23, 2024
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
1 change: 0 additions & 1 deletion tests/python/hypothesis_util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Utility functions for hypothesis-based testing"""

from math import ceil
from sys import platform as _platform

import numpy as np
from hypothesis import assume
Expand Down
22 changes: 13 additions & 9 deletions tests/python/test_gtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@

import numpy as np
import pytest
from hypothesis import given, settings
from hypothesis.strategies import data as hypothesis_callback
from hypothesis.strategies import integers, just, sampled_from

import treelite

from .hypothesis_util import (
standard_classification_datasets,
standard_regression_datasets,
standard_settings,
)
try:
from hypothesis import given, settings
from hypothesis.strategies import data as hypothesis_callback
from hypothesis.strategies import integers, just, sampled_from
except ImportError:
pytest.skip("hypothesis not installed; skipping", allow_module_level=True)

try:
import xgboost as xgb
except ImportError:
# skip this test suite if XGBoost is not installed
pytest.skip("XGBoost not installed; skipping", allow_module_level=True)


from .hypothesis_util import (
standard_classification_datasets,
standard_regression_datasets,
standard_settings,
)


@given(
predict_kind=sampled_from(["leaf_id", "score_per_tree"]),
dataset=standard_regression_datasets(),
Expand Down
27 changes: 14 additions & 13 deletions tests/python/test_lightgbm_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,34 @@
import numpy as np
import pytest
import scipy
from hypothesis import given, settings
from hypothesis.strategies import data as hypothesis_callback
from hypothesis.strategies import integers, just, sampled_from

import treelite

from .hypothesis_util import (
standard_classification_datasets,
standard_regression_datasets,
standard_settings,
)
from .metadata import dataset_db
from .util import TemporaryDirectory, load_txt, to_categorical
try:
from hypothesis import given, settings
from hypothesis.strategies import data as hypothesis_callback
from hypothesis.strategies import integers, just, sampled_from
except ImportError:
pytest.skip("hypothesis not installed; skipping", allow_module_level=True)

try:
import lightgbm as lgb
except ImportError:
# skip this test suite if LightGBM is not installed
pytest.skip("LightGBM not installed; skipping", allow_module_level=True)


try:
from sklearn.datasets import load_svmlight_file
except ImportError:
# skip this test suite if scikit-learn is not installed
pytest.skip("scikit-learn not installed; skipping", allow_module_level=True)

from .hypothesis_util import (
standard_classification_datasets,
standard_regression_datasets,
standard_settings,
)
from .metadata import dataset_db
from .util import TemporaryDirectory, load_txt, to_categorical


@given(
objective=sampled_from(["regression", "regression_l1", "huber"]),
Expand Down
24 changes: 14 additions & 10 deletions tests/python/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@

import numpy as np
import pytest
from hypothesis import given, settings
from hypothesis.strategies import data as hypothesis_callback
from hypothesis.strategies import floats, integers, just, sampled_from

import treelite

from .hypothesis_util import (
standard_classification_datasets,
standard_regression_datasets,
standard_settings,
)
from .util import TemporaryDirectory
try:
from hypothesis import given, settings
from hypothesis.strategies import data as hypothesis_callback
from hypothesis.strategies import floats, integers, just, sampled_from
except ImportError:
pytest.skip("hypothesis not installed; skipping", allow_module_level=True)

try:
from sklearn.dummy import DummyClassifier, DummyRegressor
Expand All @@ -30,10 +27,17 @@
RandomForestRegressor,
)
except ImportError:
# Skip this test suite if scikit-learn is not installed
pytest.skip("scikit-learn not installed; skipping", allow_module_level=True)


from .hypothesis_util import (
standard_classification_datasets,
standard_regression_datasets,
standard_settings,
)
from .util import TemporaryDirectory


@given(
clazz=sampled_from(
[
Expand Down
30 changes: 18 additions & 12 deletions tests/python/test_sklearn_integration.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
"""Tests for scikit-learn integration"""

import numpy as np
import pandas as pd
import pytest
from hypothesis import assume, given, settings
from hypothesis.strategies import data as hypothesis_callback
from hypothesis.strategies import floats, integers, just, sampled_from
from packaging.version import parse as parse_version

import treelite

from .hypothesis_util import (
standard_classification_datasets,
standard_regression_datasets,
standard_settings,
)
from .util import has_pandas, to_categorical
try:
from hypothesis import assume, given, settings
from hypothesis.strategies import data as hypothesis_callback
from hypothesis.strategies import floats, integers, just, sampled_from
except ImportError:
pytest.skip("hypothesis not installed; skipping", allow_module_level=True)

try:
from sklearn import __version__ as sklearn_version
Expand All @@ -34,10 +30,20 @@
)
from sklearn.utils import shuffle
except ImportError:
# Skip this test suite if scikit-learn is not installed
pytest.skip("scikit-learn not installed; skipping", allow_module_level=True)

pytestmark = pytest.mark.skipif(not has_pandas(), reason="Pandas required")
try:
import pandas as pd
except ImportError:
pytest.skip("Pandas required", allow_module_level=True)


from .hypothesis_util import (
standard_classification_datasets,
standard_regression_datasets,
standard_settings,
)
from .util import to_categorical


@given(
Expand Down
21 changes: 12 additions & 9 deletions tests/python/test_xgboost_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@

import numpy as np
import pytest
from hypothesis import given, settings
from hypothesis.strategies import data as hypothesis_callback
from hypothesis.strategies import integers, just, lists, sampled_from

import treelite

try:
from hypothesis import given, settings
from hypothesis.strategies import data as hypothesis_callback
from hypothesis.strategies import integers, just, lists, sampled_from
except ImportError:
pytest.skip("hypothesis not installed; skipping", allow_module_level=True)

try:
import xgboost as xgb
except ImportError:
pytest.skip("XGBoost not installed; skipping", allow_module_level=True)

from .hypothesis_util import (
standard_classification_datasets,
standard_multi_target_binary_classification_datasets,
Expand All @@ -20,12 +29,6 @@
)
from .util import TemporaryDirectory, to_categorical

try:
import xgboost as xgb
except ImportError:
# skip this test suite if XGBoost is not installed
pytest.skip("XGBoost not installed; skipping", allow_module_level=True)


def generate_data_for_squared_log_error(n_targets: int = 1):
"""Generate data containing outliers."""
Expand Down
3 changes: 2 additions & 1 deletion tests/python/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any, Optional, Tuple, Union

import numpy as np
import pytest


def load_txt(filename):
Expand Down Expand Up @@ -76,7 +77,7 @@ def to_categorical(
Random seed or RandomState object, to control the behavior of randomness
"""
if not has_pandas():
raise RuntimeError("Pandas is required for to_categorical()")
pytest.skip(reason="Pandas is required for to_categorical()")
import pandas as pd

features = features.copy() # Avoid clobbering source matrix
Expand Down
Loading