Skip to content

Commit

Permalink
Use pytest conventions consistently (#6337)
Browse files Browse the repository at this point in the history
* Do not derive from unittest.TestCase (not needed for pytest)

* assertRaises -> pytest.raises

* Simplify test_empty_dmatrix with test parametrization

* setUpClass -> setup_class, tearDownClass -> teardown_class

* Don't import unittest; import pytest

* Use plain assert

* Use parametrized tests in more places

* Fix test_gpu_with_sklearn.py

* Put back run_empty_dmatrix_reg / run_empty_dmatrix_cls

* Fix test_eta_decay_gpu_hist

* Add parametrized tests for monotone constraints

* Fix test names

* Remove test parametrization

* Revise test_slice to be not flaky
  • Loading branch information
hcho3 committed Nov 20, 2020
1 parent c763b50 commit 9c9070a
Show file tree
Hide file tree
Showing 34 changed files with 208 additions and 231 deletions.
3 changes: 1 addition & 2 deletions tests/python-gpu/load_pickle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'''Loading a pickled model generated by test_pickling.py, only used by
`test_gpu_with_dask.py`'''
import unittest
import os
import numpy as np
import xgboost as xgb
Expand All @@ -14,7 +13,7 @@
import testing as tm


class TestLoadPickle(unittest.TestCase):
class TestLoadPickle:
def test_load_pkl(self):
'''Test whether prediction is correct.'''
assert os.environ['CUDA_VISIBLE_DEVICES'] == '-1'
Expand Down
3 changes: 1 addition & 2 deletions tests/python-gpu/test_device_quantile_dmatrix.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# -*- coding: utf-8 -*-
import numpy as np
import xgboost as xgb
import unittest
import pytest
import sys

sys.path.append("tests/python")
import testing as tm


class TestDeviceQuantileDMatrix(unittest.TestCase):
class TestDeviceQuantileDMatrix:
def test_dmatrix_numpy_init(self):
data = np.random.randn(5, 5)
with pytest.raises(TypeError,
Expand Down
4 changes: 2 additions & 2 deletions tests/python-gpu/test_gpu_basic_models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import sys
import os
import unittest
import numpy as np
import xgboost as xgb
import pytest
sys.path.append("tests/python")
# Don't import the test class, otherwise they will run twice.
import test_callback as test_cb # noqa
rng = np.random.RandomState(1994)


class TestGPUBasicModels(unittest.TestCase):
class TestGPUBasicModels:
cputest = test_cb.TestCallbacks()

def run_cls(self, X, y, deterministic):
Expand Down
3 changes: 1 addition & 2 deletions tests/python-gpu/test_gpu_interaction_constraints.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import numpy as np
import unittest
import sys
sys.path.append("tests/python")
# Don't import the test class, otherwise they will run twice.
import test_interaction_constraints as test_ic # noqa
rng = np.random.RandomState(1994)


class TestGPUInteractionConstraints(unittest.TestCase):
class TestGPUInteractionConstraints:
cputest = test_ic.TestInteractionConstraints()

def test_interaction_constraints(self):
Expand Down
3 changes: 1 addition & 2 deletions tests/python-gpu/test_gpu_pickling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'''Test model IO with pickle.'''
import pickle
import unittest
import numpy as np
import subprocess
import os
Expand Down Expand Up @@ -35,7 +34,7 @@ def load_pickle(path):
return bst


class TestPickling(unittest.TestCase):
class TestPickling:
args_template = [
"pytest",
"--verbose",
Expand Down
4 changes: 1 addition & 3 deletions tests/python-gpu/test_gpu_prediction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import sys
import json
import unittest
import pytest

import numpy as np
Expand All @@ -26,7 +24,7 @@
})


class TestGPUPredict(unittest.TestCase):
class TestGPUPredict:
def test_predict(self):
iterations = 10
np.random.seed(1)
Expand Down
7 changes: 3 additions & 4 deletions tests/python-gpu/test_gpu_ranking.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import numpy as np
import xgboost
import os
import unittest
import itertools
import shutil
import urllib.request
import zipfile


class TestRanking(unittest.TestCase):
class TestRanking:
@classmethod
def setUpClass(cls):
def setup_class(cls):
"""
Download and setup the test fixtures
"""
Expand Down Expand Up @@ -75,7 +74,7 @@ def setup_weighted(x, y, groups):
'predictor': 'cpu_predictor'}

@classmethod
def tearDownClass(cls):
def teardown_class(cls):
"""
Cleanup test artifacts from download and unpacking
:return:
Expand Down
3 changes: 1 addition & 2 deletions tests/python-gpu/test_gpu_training_continuation.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import unittest
import numpy as np
import xgboost as xgb
import json

rng = np.random.RandomState(1994)


class TestGPUTrainingContinuation(unittest.TestCase):
class TestGPUTrainingContinuation:
def run_training_continuation(self, use_json):
kRows = 64
kCols = 32
Expand Down
8 changes: 2 additions & 6 deletions tests/python-gpu/test_gpu_with_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pytest
import sys
import numpy as np
import unittest

sys.path.append("tests/python")
import testing as tm # noqa
Expand Down Expand Up @@ -33,8 +32,5 @@ def test_gpu_binary_classification():
assert err < 0.1


class TestGPUBoostFromPrediction(unittest.TestCase):
cpu_test = twskl.TestBoostFromPrediction()

def test_boost_from_prediction_gpu_hist(self):
self.cpu_test.run_boost_from_prediction('gpu_hist')
def test_boost_from_prediction_gpu_hist():
cpu_test = twskl.run_boost_from_prediction('gpu_hist')
48 changes: 24 additions & 24 deletions tests/python-gpu/test_monotonic_constraints.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sys
import numpy as np

import unittest
import pytest

import xgboost as xgb
Expand Down Expand Up @@ -38,26 +37,27 @@ def assert_constraint(constraint, tree_method):
assert non_increasing(pred)


class TestMonotonicConstraints(unittest.TestCase):
@pytest.mark.skipif(**tm.no_sklearn())
def test_gpu_hist_basic(self):
assert_constraint(1, 'gpu_hist')
assert_constraint(-1, 'gpu_hist')

def test_gpu_hist_depthwise(self):
params = {
'tree_method': 'gpu_hist',
'grow_policy': 'depthwise',
'monotone_constraints': '(1, -1)'
}
model = xgb.train(params, tmc.training_dset)
tmc.is_correctly_constrained(model)

def test_gpu_hist_lossguide(self):
params = {
'tree_method': 'gpu_hist',
'grow_policy': 'lossguide',
'monotone_constraints': '(1, -1)'
}
model = xgb.train(params, tmc.training_dset)
tmc.is_correctly_constrained(model)
@pytest.mark.skipif(**tm.no_sklearn())
def test_gpu_hist_basic():
assert_constraint(1, 'gpu_hist')
assert_constraint(-1, 'gpu_hist')


def test_gpu_hist_depthwise():
params = {
'tree_method': 'gpu_hist',
'grow_policy': 'depthwise',
'monotone_constraints': '(1, -1)'
}
model = xgb.train(params, tmc.training_dset)
tmc.is_correctly_constrained(model)


def test_gpu_hist_lossguide():
params = {
'tree_method': 'gpu_hist',
'grow_policy': 'lossguide',
'monotone_constraints': '(1, -1)'
}
model = xgb.train(params, tmc.training_dset)
tmc.is_correctly_constrained(model)
34 changes: 16 additions & 18 deletions tests/python/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import numpy as np
import os
import xgboost as xgb
import unittest
import pytest
import json
from pathlib import Path
import tempfile
Expand All @@ -12,7 +12,7 @@
rng = np.random.RandomState(1994)


class TestBasic(unittest.TestCase):
class TestBasic:
def test_compat(self):
from xgboost.compat import lazy_isinstance
a = np.array([1, 2, 3])
Expand Down Expand Up @@ -119,30 +119,28 @@ def test_dump(self):

# number of feature importances should == number of features
dump1 = bst.get_dump()
self.assertEqual(len(dump1), 1, "Expected only 1 tree to be dumped.")
self.assertEqual(len(dump1[0].splitlines()), 3,
"Expected 1 root and 2 leaves - 3 lines in dump.")
assert len(dump1) == 1, 'Expected only 1 tree to be dumped.'
len(dump1[0].splitlines()) == 3, 'Expected 1 root and 2 leaves - 3 lines in dump.'

dump2 = bst.get_dump(with_stats=True)
self.assertEqual(dump2[0].count('\n'), 3,
"Expected 1 root and 2 leaves - 3 lines in dump.")
self.assertGreater(dump2[0].find('\n'), dump1[0].find('\n'),
"Expected more info when with_stats=True is given.")
assert dump2[0].count('\n') == 3, 'Expected 1 root and 2 leaves - 3 lines in dump.'
assert (dump2[0].find('\n') > dump1[0].find('\n'),
'Expected more info when with_stats=True is given.')

dump3 = bst.get_dump(dump_format="json")
dump3j = json.loads(dump3[0])
self.assertEqual(dump3j["nodeid"], 0, "Expected the root node on top.")
assert dump3j['nodeid'] == 0, 'Expected the root node on top.'

dump4 = bst.get_dump(dump_format="json", with_stats=True)
dump4j = json.loads(dump4[0])
self.assertIn("gain", dump4j, "Expected 'gain' to be dumped in JSON.")
assert 'gain' in dump4j, "Expected 'gain' to be dumped in JSON."

def test_load_file_invalid(self):
self.assertRaises(xgb.core.XGBoostError, xgb.Booster,
model_file='incorrect_path')
with pytest.raises(xgb.core.XGBoostError):
xgb.Booster(model_file='incorrect_path')

self.assertRaises(xgb.core.XGBoostError, xgb.Booster,
model_file=u'不正なパス')
with pytest.raises(xgb.core.XGBoostError):
xgb.Booster(model_file=u'不正なパス')

def test_dmatrix_numpy_init_omp(self):

Expand Down Expand Up @@ -226,7 +224,7 @@ def cb(cbackenv):
assert output == solution


class TestBasicPathLike(unittest.TestCase):
class TestBasicPathLike:
"""Unit tests using pathlib.Path for file interaction."""

def test_DMatrix_init_from_path(self):
Expand All @@ -253,8 +251,8 @@ def test_DMatrix_save_to_path(self):

def test_Booster_init_invalid_path(self):
"""An invalid model_file path should raise XGBoostError."""
self.assertRaises(xgb.core.XGBoostError, xgb.Booster,
model_file=Path("invalidpath"))
with pytest.raises(xgb.core.XGBoostError):
xgb.Booster(model_file=Path("invalidpath"))


def test_Booster_save_and_load(self):
Expand Down
1 change: 0 additions & 1 deletion tests/python/test_basic_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np
import xgboost as xgb
import unittest
import os
import json
import testing as tm
Expand Down

0 comments on commit 9c9070a

Please sign in to comment.