From 79643fcb92cc90b955f177f06bb6b342f1d3ef14 Mon Sep 17 00:00:00 2001 From: Osama Esmail Date: Mon, 14 Nov 2022 19:09:17 -0800 Subject: [PATCH 1/2] fixing the tests the way numpy PR 22487 does --- doc/TESTS.rst.txt | 17 ++++--- numpy/core/tests/test_arrayprint.py | 4 +- numpy/core/tests/test_defchararray.py | 20 ++++---- numpy/core/tests/test_deprecations.py | 8 ++-- numpy/core/tests/test_half.py | 2 +- numpy/core/tests/test_indexing.py | 2 +- numpy/core/tests/test_memmap.py | 4 +- numpy/core/tests/test_multiarray.py | 18 ++++---- numpy/core/tests/test_numeric.py | 26 +++++------ numpy/core/tests/test_numerictypes.py | 2 +- numpy/core/tests/test_overrides.py | 2 +- numpy/core/tests/test_records.py | 2 +- numpy/core/tests/test_umath.py | 4 +- numpy/core/tests/test_umath_complex.py | 8 ++-- numpy/distutils/tests/test_ccompiler_opt.py | 4 +- .../tests/test_ccompiler_opt_conf.py | 4 +- numpy/distutils/tests/test_exec_command.py | 2 +- numpy/distutils/tests/test_system_info.py | 4 +- numpy/f2py/tests/test_assumed_shape.py | 6 +-- numpy/f2py/tests/util.py | 2 +- numpy/lib/tests/test__datasource.py | 24 +++++----- numpy/lib/tests/test_function_base.py | 2 +- numpy/lib/tests/test_histograms.py | 4 +- numpy/lib/tests/test_io.py | 5 +- numpy/lib/tests/test_recfunctions.py | 12 ++--- numpy/ma/tests/test_core.py | 24 +++++----- numpy/ma/tests/test_extras.py | 4 +- numpy/ma/tests/test_mrecords.py | 2 +- numpy/ma/tests/test_old_ma.py | 46 +++++++++---------- numpy/ma/tests/test_subclassing.py | 2 +- numpy/matrixlib/tests/test_masked_matrix.py | 2 +- numpy/random/tests/test_generator_mt19937.py | 12 ++--- numpy/random/tests/test_random.py | 10 ++-- numpy/random/tests/test_randomstate.py | 10 ++-- numpy/testing/tests/test_utils.py | 14 +++--- 35 files changed, 159 insertions(+), 155 deletions(-) diff --git a/doc/TESTS.rst.txt b/doc/TESTS.rst.txt index 0d8137f4a..9c5e8571f 100644 --- a/doc/TESTS.rst.txt +++ b/doc/TESTS.rst.txt @@ -178,30 +178,33 @@ Similarly for methods:: Easier setup and teardown functions / methods --------------------------------------------- -Testing looks for module-level or class-level setup and teardown functions by -name; thus:: +Testing looks for module-level or class method-level setup and teardown +functions by name; thus:: - def setup(): + def setup_module(): """Module-level setup""" print('doing setup') - def teardown(): + def teardown_module(): """Module-level teardown""" print('doing teardown') class TestMe: - def setup(): + def setup_method(self): """Class-level setup""" print('doing setup') - def teardown(): + def teardown_method(): """Class-level teardown""" print('doing teardown') Setup and teardown functions to functions and methods are known as "fixtures", -and their use is not encouraged. +and they should be used sparingly. +``pytest`` supports more general fixture at various scopes which may be used +automatically via special arguments. For example, the special argument name +``tmpdir`` is used in test to create a temporary directory. Parametric tests ---------------- diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py index 25826d8ed..1f5b7abdd 100644 --- a/numpy/core/tests/test_arrayprint.py +++ b/numpy/core/tests/test_arrayprint.py @@ -498,10 +498,10 @@ def test_refcount(self): class TestPrintOptions: """Test getting and setting global print options.""" - def setup(self): + def setup_method(self): self.oldopts = np.get_printoptions() - def teardown(self): + def teardown_method(self): np.set_printoptions(**self.oldopts) def test_basic(self): diff --git a/numpy/core/tests/test_defchararray.py b/numpy/core/tests/test_defchararray.py index 59fc54722..64124903c 100644 --- a/numpy/core/tests/test_defchararray.py +++ b/numpy/core/tests/test_defchararray.py @@ -132,7 +132,7 @@ def fail(): class TestWhitespace: - def setup(self): + def setup_method(self): self.A = np.array([['abc ', '123 '], ['789 ', 'xyz ']]).view(np.chararray) self.B = np.array([['abc', '123'], @@ -147,7 +147,7 @@ def test1(self): assert_(not np.any(self.A != self.B)) class TestChar: - def setup(self): + def setup_method(self): self.A = np.array('abc1', dtype='c').view(np.chararray) def test_it(self): @@ -155,7 +155,7 @@ def test_it(self): assert_equal(self.A.upper()[:2].tobytes(), b'AB') class TestComparisons: - def setup(self): + def setup_method(self): self.A = np.array([['abc', '123'], ['789', 'xyz']]).view(np.chararray) self.B = np.array([['efg', '123 '], @@ -188,21 +188,21 @@ def test_type(self): class TestComparisonsMixed1(TestComparisons): """Ticket #1276""" - def setup(self): - TestComparisons.setup(self) + def setup_method(self): + TestComparisons.setup_method(self) self.B = np.array([['efg', '123 '], ['051', 'tuv']], np.unicode_).view(np.chararray) class TestComparisonsMixed2(TestComparisons): """Ticket #1276""" - def setup(self): - TestComparisons.setup(self) + def setup_method(self): + TestComparisons.setup_method(self) self.A = np.array([['abc', '123'], ['789', 'xyz']], np.unicode_).view(np.chararray) class TestInformation: - def setup(self): + def setup_method(self): self.A = np.array([[' abc ', ''], ['12345', 'MixedCase'], ['123 \t 345 \0 ', 'UPPER']]).view(np.chararray) @@ -308,7 +308,7 @@ def fail(): class TestMethods: - def setup(self): + def setup_method(self): self.A = np.array([[' abc ', ''], ['12345', 'MixedCase'], ['123 \t 345 \0 ', 'UPPER']], @@ -581,7 +581,7 @@ def fail(): class TestOperations: - def setup(self): + def setup_method(self): self.A = np.array([['abc', '123'], ['789', 'xyz']]).view(np.chararray) self.B = np.array([['efg', '456'], diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 2b7864433..d443edd13 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -32,7 +32,7 @@ class _DeprecationTestCase: message = '' warning_cls = DeprecationWarning - def setup(self): + def setup_method(self): self.warn_ctx = warnings.catch_warnings(record=True) self.log = self.warn_ctx.__enter__() @@ -46,7 +46,7 @@ def setup(self): warnings.filterwarnings("always", message=self.message, category=self.warning_cls) - def teardown(self): + def teardown_method(self): self.warn_ctx.__exit__() def assert_deprecated(self, function, num=1, ignore_others=False, @@ -297,7 +297,7 @@ class vdt(np.void): class TestTestDeprecated: def test_assert_deprecated(self): test_case_instance = _DeprecationTestCase() - test_case_instance.setup() + test_case_instance.setup_method() assert_raises(AssertionError, test_case_instance.assert_deprecated, lambda: None) @@ -306,7 +306,7 @@ def foo(): warnings.warn("foo", category=DeprecationWarning, stacklevel=2) test_case_instance.assert_deprecated(foo) - test_case_instance.teardown() + test_case_instance.teardown_method() class TestNonNumericConjugate(_DeprecationTestCase): diff --git a/numpy/core/tests/test_half.py b/numpy/core/tests/test_half.py index 1b6fd21e1..78bd068bb 100644 --- a/numpy/core/tests/test_half.py +++ b/numpy/core/tests/test_half.py @@ -17,7 +17,7 @@ def assert_raises_fpe(strmatch, callable, *args, **kwargs): "Did not raise floating point %s error" % strmatch) class TestHalf: - def setup(self): + def setup_method(self): # An array of all possible float16 values self.all_f16 = np.arange(0x10000, dtype=uint16) self.all_f16.dtype = float16 diff --git a/numpy/core/tests/test_indexing.py b/numpy/core/tests/test_indexing.py index efcb92c2e..7f22be503 100644 --- a/numpy/core/tests/test_indexing.py +++ b/numpy/core/tests/test_indexing.py @@ -826,7 +826,7 @@ class TestMultiIndexingAutomated: """ - def setup(self): + def setup_method(self): self.a = np.arange(np.prod([3, 1, 5, 6])).reshape(3, 1, 5, 6) self.b = np.empty((3, 0, 5, 6)) self.complex_indices = ['skip', Ellipsis, diff --git a/numpy/core/tests/test_memmap.py b/numpy/core/tests/test_memmap.py index e4f0a6b3f..914f86f14 100644 --- a/numpy/core/tests/test_memmap.py +++ b/numpy/core/tests/test_memmap.py @@ -15,14 +15,14 @@ ) class TestMemmap: - def setup(self): + def setup_method(self): self.tmpfp = NamedTemporaryFile(prefix='mmap') self.shape = (3, 4) self.dtype = 'float32' self.data = arange(12, dtype=self.dtype) self.data.resize(self.shape) - def teardown(self): + def teardown_method(self): self.tmpfp.close() self.data = None if IS_PYPY: diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index cacfd5bfc..e5dee24c0 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -74,7 +74,7 @@ def _aligned_zeros(shape, dtype=float, order="C", align=None): class TestFlags: - def setup(self): + def setup_method(self): self.a = np.arange(10) def test_writeable(self): @@ -275,7 +275,7 @@ def test_int(self): class TestAttributes: - def setup(self): + def setup_method(self): self.one = np.arange(10) self.two = np.arange(20).reshape(4, 5) self.three = np.arange(60, dtype=np.float64).reshape(2, 5, 6) @@ -652,7 +652,7 @@ def test_structured_non_void(self): class TestZeroRank: - def setup(self): + def setup_method(self): self.d = np.array(0), np.array('x', object) def test_ellipsis_subscript(self): @@ -756,7 +756,7 @@ def test_real_imag(self): class TestScalarIndexing: - def setup(self): + def setup_method(self): self.d = np.array([0, 1])[0] def test_ellipsis_subscript(self): @@ -5672,7 +5672,7 @@ def test_mmap_close(self): mm.close() class TestFlat: - def setup(self): + def setup_method(self): a0 = np.arange(20.0) a = a0.reshape(4, 5) a0.shape = (4, 5) @@ -5995,7 +5995,7 @@ class TestStats: funcs = [_mean, _var, _std] - def setup(self): + def setup_method(self): np.random.seed(range(3)) self.rmat = np.random.random((4, 5)) self.cmat = self.rmat + 1j * self.rmat @@ -6422,7 +6422,7 @@ def test_vdot_uncontiguous(self): class TestDot: - def setup(self): + def setup_method(self): np.random.seed(128) self.A = np.random.rand(4, 2) self.b1 = np.random.rand(2, 1) @@ -7214,7 +7214,7 @@ def test_3d_tensor(self): class TestChoose: - def setup(self): + def setup_method(self): self.x = 2*np.ones((3,), dtype=int) self.y = 3*np.ones((3,), dtype=int) self.x2 = 2*np.ones((2, 3), dtype=int) @@ -7244,7 +7244,7 @@ def test_output_dtype(self, ops): class TestRepeat: - def setup(self): + def setup_method(self): self.m = np.array([1, 2, 3, 4, 5, 6]) self.m_rect = self.m.reshape((2, 3)) diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 0b03c6576..692e4e0c6 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -347,7 +347,7 @@ def test_bitwise_xor(self): class TestBoolArray: - def setup(self): + def setup_method(self): # offset for simd tests self.t = np.array([True] * 41, dtype=bool)[1::] self.f = np.array([False] * 41, dtype=bool)[1::] @@ -434,7 +434,7 @@ def test_logical_and_or_xor(self): class TestBoolCmp: - def setup(self): + def setup_method(self): self.f = np.ones(256, dtype=np.float32) self.ef = np.ones(self.f.size, dtype=bool) self.d = np.ones(128, dtype=np.float64) @@ -1813,7 +1813,7 @@ def assert_array_strict_equal(x, y): class TestClip: - def setup(self): + def setup_method(self): self.nr = 5 self.nc = 3 @@ -2442,10 +2442,10 @@ class TestAllclose: rtol = 1e-5 atol = 1e-8 - def setup(self): + def setup_method(self): self.olderr = np.seterr(invalid='ignore') - def teardown(self): + def teardown_method(self): np.seterr(**self.olderr) def tst_allclose(self, x, y): @@ -2527,7 +2527,7 @@ class TestIsclose: rtol = 1e-5 atol = 1e-8 - def setup(self): + def _setup(self): atol = self.atol rtol = self.rtol arr = np.array([100, 1000]) @@ -2573,7 +2573,7 @@ def setup(self): ] def test_ip_isclose(self): - self.setup() + self._setup() tests = self.some_close_tests results = self.some_close_results for (x, y), result in zip(tests, results): @@ -2595,17 +2595,17 @@ def tst_isclose_allclose(self, x, y): assert_array_equal(np.isclose(x, y).all(), np.allclose(x, y), msg % (x, y)) def test_ip_all_isclose(self): - self.setup() + self._setup() for (x, y) in self.all_close_tests: self.tst_all_isclose(x, y) def test_ip_none_isclose(self): - self.setup() + self._setup() for (x, y) in self.none_close_tests: self.tst_none_isclose(x, y) def test_ip_isclose_allclose(self): - self.setup() + self._setup() tests = (self.all_close_tests + self.none_close_tests + self.some_close_tests) for (x, y) in tests: @@ -2671,7 +2671,7 @@ def test_timedelta(self): class TestStdVar: - def setup(self): + def setup_method(self): self.A = np.array([1, -1, 1, -1]) self.real_var = 1 @@ -2724,7 +2724,7 @@ def test_scalars(self): class TestCreationFuncs: # Test ones, zeros, empty and full. - def setup(self): + def setup_method(self): dtypes = {np.dtype(tp) for tp in itertools.chain(*np.sctypes.values())} # void, bytes, str variable_sized = {tp for tp in dtypes if tp.str.endswith('0')} @@ -2795,7 +2795,7 @@ def test_for_reference_leak(self): class TestLikeFuncs: '''Test ones_like, zeros_like, empty_like and full_like''' - def setup(self): + def setup_method(self): self.data = [ # Array scalars (np.array(3.), None), diff --git a/numpy/core/tests/test_numerictypes.py b/numpy/core/tests/test_numerictypes.py index 73ff4764d..e5ff52332 100644 --- a/numpy/core/tests/test_numerictypes.py +++ b/numpy/core/tests/test_numerictypes.py @@ -357,7 +357,7 @@ def test_scalar_wins3(self): # doesn't go up to 'f16' on purpose assert_(res == 'f8') class TestMultipleFields: - def setup(self): + def setup_method(self): self.ary = np.array([(1, 2, 3, 4), (5, 6, 7, 8)], dtype='i4,f4,i2,c8') def _bad_call(self): diff --git a/numpy/core/tests/test_overrides.py b/numpy/core/tests/test_overrides.py index 36970dbc0..105645027 100644 --- a/numpy/core/tests/test_overrides.py +++ b/numpy/core/tests/test_overrides.py @@ -431,7 +431,7 @@ def __array_function__(self, func, types, args, kwargs): class TestArrayLike: - def setup(self): + def setup_method(self): class MyArray(): def __init__(self, function=None): self.function = function diff --git a/numpy/core/tests/test_records.py b/numpy/core/tests/test_records.py index 4d4b4b515..a76ae2d99 100644 --- a/numpy/core/tests/test_records.py +++ b/numpy/core/tests/test_records.py @@ -337,7 +337,7 @@ def test_tofile_fromfile(self): class TestRecord: - def setup(self): + def setup_method(self): self.data = np.rec.fromrecords([(1, 2, 3), (4, 5, 6)], dtype=[("col1", " Date: Tue, 15 Nov 2022 11:25:47 -0800 Subject: [PATCH 2/2] missed on setup->setup_method --- numpy/lib/tests/test_recfunctions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index 3746de656..0e022d82f 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -893,7 +893,7 @@ def test_padded_dtype(self): class TestJoinBy2: @classmethod - def setup(cls): + def setup_method(cls): cls.a = np.array(list(zip(np.arange(10), np.arange(50, 60), np.arange(100, 110))), dtype=[('a', int), ('b', int), ('c', int)]) -- 2.17.1