Use pytest conventions consistently (#6337)

* 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
This commit is contained in:
Philip Hyunsu Cho
2020-11-19 17:00:15 -08:00
committed by GitHub
parent c763b50dd0
commit 9c9070aea2
34 changed files with 200 additions and 223 deletions

View File

@@ -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
@@ -14,7 +13,7 @@ sys.path.append("tests/python")
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'

View File

@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
import numpy as np
import xgboost as xgb
import unittest
import pytest
import sys
@@ -9,7 +8,7 @@ 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,

View File

@@ -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):

View File

@@ -1,5 +1,4 @@
import numpy as np
import unittest
import sys
sys.path.append("tests/python")
# Don't import the test class, otherwise they will run twice.
@@ -7,7 +6,7 @@ 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):

View File

@@ -1,6 +1,5 @@
'''Test model IO with pickle.'''
import pickle
import unittest
import numpy as np
import subprocess
import os
@@ -35,7 +34,7 @@ def load_pickle(path):
return bst
class TestPickling(unittest.TestCase):
class TestPickling:
args_template = [
"pytest",
"--verbose",

View File

@@ -1,6 +1,4 @@
import sys
import json
import unittest
import pytest
import numpy as np
@@ -26,7 +24,7 @@ predict_parameter_strategy = strategies.fixed_dictionaries({
})
class TestGPUPredict(unittest.TestCase):
class TestGPUPredict:
def test_predict(self):
iterations = 10
np.random.seed(1)

View File

@@ -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
"""
@@ -75,7 +74,7 @@ class TestRanking(unittest.TestCase):
'predictor': 'cpu_predictor'}
@classmethod
def tearDownClass(cls):
def teardown_class(cls):
"""
Cleanup test artifacts from download and unpacking
:return:

View File

@@ -1,4 +1,3 @@
import unittest
import numpy as np
import xgboost as xgb
import json
@@ -6,7 +5,7 @@ import json
rng = np.random.RandomState(1994)
class TestGPUTrainingContinuation(unittest.TestCase):
class TestGPUTrainingContinuation:
def run_training_continuation(self, use_json):
kRows = 64
kCols = 32

View File

@@ -2,7 +2,6 @@ import xgboost as xgb
import pytest
import sys
import numpy as np
import unittest
sys.path.append("tests/python")
import testing as tm # noqa
@@ -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')

View File

@@ -1,7 +1,6 @@
import sys
import numpy as np
import unittest
import pytest
import xgboost as xgb
@@ -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')
@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(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)
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)