Skip to content

Commit ac69b46

Browse files
committed
TST: Implement assert_not in testing.py
1 parent b4375bd commit ac69b46

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

pandas/tests/frame/test_operators.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def test_modulo(self):
237237
s = p[0]
238238
res = s % p
239239
res2 = p % s
240-
assert not np.array_equal(res.fillna(0), res2.fillna(0))
240+
tm.assert_not(tm.assert_series_equal, res.fillna(0), res2.fillna(0))
241241

242242
def test_div(self):
243243

@@ -271,7 +271,7 @@ def test_div(self):
271271
s = p[0]
272272
res = s / p
273273
res2 = p / s
274-
assert not np.array_equal(res.fillna(0), res2.fillna(0))
274+
tm.assert_not(tm.assert_series_equal, res.fillna(0), res2.fillna(0))
275275

276276
def test_logical_operators(self):
277277

@@ -1030,7 +1030,7 @@ def test_boolean_comparison(self):
10301030
assert_numpy_array_equal(result, expected.values)
10311031

10321032
pytest.raises(ValueError, lambda: df == b_c)
1033-
assert not np.array_equal(df.values, b_c)
1033+
tm.assert_not(tm.assert_numpy_array_equal, df.values, b_c)
10341034

10351035
# with alignment
10361036
df = DataFrame(np.arange(6).reshape((3, 2)),

pandas/tests/series/test_analytics.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ def test_modulo(self):
694694
p = p.astype('float64')
695695
result = p['first'] % p['second']
696696
result2 = p['second'] % p['first']
697-
assert not np.array_equal(result, result2)
697+
tm.assert_not(tm.assert_series_equal, result, result2)
698698

699699
# GH 9144
700700
s = Series([0, 1])

pandas/tests/series/test_operators.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ def test_div(self):
122122
assert_series_equal(result, p['first'].astype('float64'),
123123
check_names=False)
124124
assert result.name is None
125-
assert not np.array_equal(result, p['second'] / p['first'])
125+
tm.assert_not(tm.assert_series_equal,
126+
result, p['second'] / p['first'])
126127

127128
# inf signing
128129
s = Series([np.nan, 1., -1.])

pandas/tests/util/test_testing.py

+29
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,35 @@ def test_categorical_equal_message(self):
717717
tm.assert_categorical_equal(a, b)
718718

719719

720+
class TestAssertNot(object):
721+
722+
def test_assert_not_succeed(self):
723+
a = pd.DataFrame([1, 2])
724+
b = pd.DataFrame([1, 2, 3])
725+
tm.assert_not(tm.assert_frame_equal, a, b)
726+
727+
a = np.array([1, 2, 3], dtype=np.int64)
728+
b = np.array([1, 2, 3], dtype=np.int32)
729+
tm.assert_not(tm.assert_numpy_array_equal, a, b)
730+
731+
def test_assert_not_fail(self):
732+
a = pd.DataFrame([1, 2, 3])
733+
b = pd.DataFrame([1, 2, 3])
734+
735+
msg = ("Assertion function \'assert_frame_equal\' "
736+
"succeeded when expected to fail")
737+
with tm.assert_raises_regex(AssertionError, msg):
738+
tm.assert_not(tm.assert_frame_equal, a, b)
739+
740+
a = pd.Categorical([1, 2, 3, 4])
741+
b = pd.Categorical([1, 2, 3, 4])
742+
743+
msg = ("Assertion function \'assert_categorical_equal\' "
744+
"succeeded when expected to fail")
745+
with tm.assert_raises_regex(AssertionError, msg):
746+
tm.assert_not(tm.assert_categorical_equal, a, b)
747+
748+
720749
class TestRNGContext(object):
721750

722751
def test_RNGContext(self):

pandas/util/testing.py

+25
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,31 @@ def round_trip_localpath(writer, reader, path=None):
165165
return obj
166166

167167

168+
def assert_not(method, *args, **kwargs):
169+
"""
170+
Check that an assertion function should fail.
171+
172+
Parameters
173+
----------
174+
method : callable
175+
The assertion function to check (e.g. `assert_frame_equal`).
176+
177+
Raises
178+
------
179+
AssertionError : the assertion function succeeded
180+
when it should have failed.
181+
"""
182+
183+
try:
184+
method(*args, **kwargs)
185+
except AssertionError:
186+
return True
187+
188+
name = method.__name__
189+
raise AssertionError("Assertion function '{name}' succeeded "
190+
"when expected to fail".format(name=name))
191+
192+
168193
def assert_almost_equal(left, right, check_exact=False,
169194
check_dtype='equiv', check_less_precise=False,
170195
**kwargs):

0 commit comments

Comments
 (0)