15
15
16
16
import unittest
17
17
18
- from unittest . test .support import (
18
+ from test . test_unittest .support import (
19
19
TestEquality , TestHashing , LoggingResult , LegacyLoggingResult ,
20
20
ResultWithNoStartTestRunStopTestRun
21
21
)
@@ -304,7 +304,8 @@ def defaultTestResult(self):
304
304
def test (self ):
305
305
pass
306
306
307
- Foo ('test' ).run ()
307
+ with self .assertWarns (RuntimeWarning ):
308
+ Foo ('test' ).run ()
308
309
309
310
def test_deprecation_of_return_val_from_test (self ):
310
311
# Issue 41322 - deprecate return of value that is not None from a test
@@ -709,36 +710,6 @@ def testAssertIn(self):
709
710
self .assertRaises (self .failureException , self .assertNotIn , 'cow' ,
710
711
animals )
711
712
712
- def testAssertDictContainsSubset (self ):
713
- with warnings .catch_warnings ():
714
- warnings .simplefilter ("ignore" , DeprecationWarning )
715
-
716
- self .assertDictContainsSubset ({}, {})
717
- self .assertDictContainsSubset ({}, {'a' : 1 })
718
- self .assertDictContainsSubset ({'a' : 1 }, {'a' : 1 })
719
- self .assertDictContainsSubset ({'a' : 1 }, {'a' : 1 , 'b' : 2 })
720
- self .assertDictContainsSubset ({'a' : 1 , 'b' : 2 }, {'a' : 1 , 'b' : 2 })
721
-
722
- with self .assertRaises (self .failureException ):
723
- self .assertDictContainsSubset ({1 : "one" }, {})
724
-
725
- with self .assertRaises (self .failureException ):
726
- self .assertDictContainsSubset ({'a' : 2 }, {'a' : 1 })
727
-
728
- with self .assertRaises (self .failureException ):
729
- self .assertDictContainsSubset ({'c' : 1 }, {'a' : 1 })
730
-
731
- with self .assertRaises (self .failureException ):
732
- self .assertDictContainsSubset ({'a' : 1 , 'c' : 1 }, {'a' : 1 })
733
-
734
- with self .assertRaises (self .failureException ):
735
- self .assertDictContainsSubset ({'a' : 1 , 'c' : 1 }, {'a' : 1 })
736
-
737
- one = '' .join (chr (i ) for i in range (255 ))
738
- # this used to cause a UnicodeDecodeError constructing the failure msg
739
- with self .assertRaises (self .failureException ):
740
- self .assertDictContainsSubset ({'foo' : one }, {'foo' : '\uFFFD ' })
741
-
742
713
def testAssertEqual (self ):
743
714
equal_pairs = [
744
715
((), ()),
@@ -1161,6 +1132,8 @@ def testAssertMultiLineEqual(self):
1161
1132
# need to remove the first line of the error message
1162
1133
error = str (e ).split ('\n ' , 1 )[1 ]
1163
1134
self .assertEqual (sample_text_error , error )
1135
+ else :
1136
+ self .fail (f'{ self .failureException } not raised' )
1164
1137
1165
1138
def testAssertEqualSingleLine (self ):
1166
1139
sample_text = "laden swallows fly slowly"
@@ -1177,6 +1150,74 @@ def testAssertEqualSingleLine(self):
1177
1150
# need to remove the first line of the error message
1178
1151
error = str (e ).split ('\n ' , 1 )[1 ]
1179
1152
self .assertEqual (sample_text_error , error )
1153
+ else :
1154
+ self .fail (f'{ self .failureException } not raised' )
1155
+
1156
+ def testAssertEqualwithEmptyString (self ):
1157
+ '''Verify when there is an empty string involved, the diff output
1158
+ does not treat the empty string as a single empty line. It should
1159
+ instead be handled as a non-line.
1160
+ '''
1161
+ sample_text = ''
1162
+ revised_sample_text = 'unladen swallows fly quickly'
1163
+ sample_text_error = '''\
1164
+ + unladen swallows fly quickly
1165
+ '''
1166
+ try :
1167
+ self .assertEqual (sample_text , revised_sample_text )
1168
+ except self .failureException as e :
1169
+ # need to remove the first line of the error message
1170
+ error = str (e ).split ('\n ' , 1 )[1 ]
1171
+ self .assertEqual (sample_text_error , error )
1172
+ else :
1173
+ self .fail (f'{ self .failureException } not raised' )
1174
+
1175
+ def testAssertEqualMultipleLinesMissingNewlineTerminator (self ):
1176
+ '''Verifying format of diff output from assertEqual involving strings
1177
+ with multiple lines, but missing the terminating newline on both.
1178
+ '''
1179
+ sample_text = 'laden swallows\n fly sloely'
1180
+ revised_sample_text = 'laden swallows\n fly slowly'
1181
+ sample_text_error = '''\
1182
+ laden swallows
1183
+ - fly sloely
1184
+ ? ^
1185
+ + fly slowly
1186
+ ? ^
1187
+ '''
1188
+ try :
1189
+ self .assertEqual (sample_text , revised_sample_text )
1190
+ except self .failureException as e :
1191
+ # need to remove the first line of the error message
1192
+ error = str (e ).split ('\n ' , 1 )[1 ]
1193
+ self .assertEqual (sample_text_error , error )
1194
+ else :
1195
+ self .fail (f'{ self .failureException } not raised' )
1196
+
1197
+ def testAssertEqualMultipleLinesMismatchedNewlinesTerminators (self ):
1198
+ '''Verifying format of diff output from assertEqual involving strings
1199
+ with multiple lines and mismatched newlines. The output should
1200
+ include a - on it's own line to indicate the newline difference
1201
+ between the two strings
1202
+ '''
1203
+ sample_text = 'laden swallows\n fly sloely\n '
1204
+ revised_sample_text = 'laden swallows\n fly slowly'
1205
+ sample_text_error = '''\
1206
+ laden swallows
1207
+ - fly sloely
1208
+ ? ^
1209
+ + fly slowly
1210
+ ? ^
1211
+ -\x20
1212
+ '''
1213
+ try :
1214
+ self .assertEqual (sample_text , revised_sample_text )
1215
+ except self .failureException as e :
1216
+ # need to remove the first line of the error message
1217
+ error = str (e ).split ('\n ' , 1 )[1 ]
1218
+ self .assertEqual (sample_text_error , error )
1219
+ else :
1220
+ self .fail (f'{ self .failureException } not raised' )
1180
1221
1181
1222
def testEqualityBytesWarning (self ):
1182
1223
if sys .flags .bytes_warning :
@@ -1801,45 +1842,18 @@ def testAssertNoLogsYieldsNone(self):
1801
1842
pass
1802
1843
self .assertIsNone (value )
1803
1844
1804
- def testDeprecatedMethodNames (self ):
1805
- """
1806
- Test that the deprecated methods raise a DeprecationWarning. See #9424.
1807
- """
1808
- old = (
1809
- (self .failIfEqual , (3 , 5 )),
1810
- (self .assertNotEquals , (3 , 5 )),
1811
- (self .failUnlessEqual , (3 , 3 )),
1812
- (self .assertEquals , (3 , 3 )),
1813
- (self .failUnlessAlmostEqual , (2.0 , 2.0 )),
1814
- (self .assertAlmostEquals , (2.0 , 2.0 )),
1815
- (self .failIfAlmostEqual , (3.0 , 5.0 )),
1816
- (self .assertNotAlmostEquals , (3.0 , 5.0 )),
1817
- (self .failUnless , (True ,)),
1818
- (self .assert_ , (True ,)),
1819
- (self .failUnlessRaises , (TypeError , lambda _ : 3.14 + 'spam' )),
1820
- (self .failIf , (False ,)),
1821
- (self .assertDictContainsSubset , (dict (a = 1 , b = 2 ), dict (a = 1 , b = 2 , c = 3 ))),
1822
- (self .assertRaisesRegexp , (KeyError , 'foo' , lambda : {}['foo' ])),
1823
- (self .assertRegexpMatches , ('bar' , 'bar' )),
1824
- )
1825
- for meth , args in old :
1826
- with self .assertWarns (DeprecationWarning ):
1827
- meth (* args )
1828
-
1829
- # disable this test for now. When the version where the fail* methods will
1830
- # be removed is decided, re-enable it and update the version
1831
- def _testDeprecatedFailMethods (self ):
1832
- """Test that the deprecated fail* methods get removed in 3.x"""
1833
- if sys .version_info [:2 ] < (3 , 3 ):
1834
- return
1845
+ def testDeprecatedFailMethods (self ):
1846
+ """Test that the deprecated fail* methods get removed in 3.12"""
1835
1847
deprecated_names = [
1836
1848
'failIfEqual' , 'failUnlessEqual' , 'failUnlessAlmostEqual' ,
1837
1849
'failIfAlmostEqual' , 'failUnless' , 'failUnlessRaises' , 'failIf' ,
1838
- 'assertDictContainsSubset' ,
1850
+ 'assertNotEquals' , 'assertEquals' , 'assertAlmostEquals' ,
1851
+ 'assertNotAlmostEquals' , 'assert_' , 'assertDictContainsSubset' ,
1852
+ 'assertRaisesRegexp' , 'assertRegexpMatches'
1839
1853
]
1840
1854
for deprecated_name in deprecated_names :
1841
1855
with self .assertRaises (AttributeError ):
1842
- getattr (self , deprecated_name ) # remove these in 3.x
1856
+ getattr (self , deprecated_name )
1843
1857
1844
1858
def testDeepcopy (self ):
1845
1859
# Issue: 5660
@@ -1956,7 +1970,7 @@ def testNoCycles(self):
1956
1970
del case
1957
1971
self .assertFalse (wr ())
1958
1972
1959
- # TODO: RUSTPYTHON; destructors
1973
+ # TODO: RUSTPYTHON
1960
1974
@unittest .expectedFailure
1961
1975
def test_no_exception_leak (self ):
1962
1976
# Issue #19880: TestCase.run() should not keep a reference
0 commit comments