Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
0f535013
Commit
0f535013
authored
Apr 03, 2011
by
Ezio Melotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#11282: add back the fail* methods and assertDictContainsSubset.
parent
b7af6207
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
113 additions
and
9 deletions
+113
-9
Lib/unittest/case.py
Lib/unittest/case.py
+36
-5
Lib/unittest/test/_test_warnings.py
Lib/unittest/test/_test_warnings.py
+6
-1
Lib/unittest/test/test_assertions.py
Lib/unittest/test/test_assertions.py
+9
-0
Lib/unittest/test/test_case.py
Lib/unittest/test/test_case.py
+53
-0
Lib/unittest/test/test_runner.py
Lib/unittest/test/test_runner.py
+7
-3
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Lib/unittest/case.py
View file @
0f535013
...
...
@@ -938,6 +938,35 @@ class TestCase(object):
standardMsg
=
self
.
_truncateMessage
(
standardMsg
,
diff
)
self
.
fail
(
self
.
_formatMessage
(
msg
,
standardMsg
))
def
assertDictContainsSubset
(
self
,
subset
,
dictionary
,
msg
=
None
):
"""Checks whether dictionary is a superset of subset."""
warnings
.
warn
(
'assertDictContainsSubset is deprecated'
,
DeprecationWarning
)
missing
=
[]
mismatched
=
[]
for
key
,
value
in
subset
.
items
():
if
key
not
in
dictionary
:
missing
.
append
(
key
)
elif
value
!=
dictionary
[
key
]:
mismatched
.
append
(
'%s, expected: %s, actual: %s'
%
(
safe_repr
(
key
),
safe_repr
(
value
),
safe_repr
(
dictionary
[
key
])))
if
not
(
missing
or
mismatched
):
return
standardMsg
=
''
if
missing
:
standardMsg
=
'Missing: %s'
%
','
.
join
(
safe_repr
(
m
)
for
m
in
missing
)
if
mismatched
:
if
standardMsg
:
standardMsg
+=
'; '
standardMsg
+=
'Mismatched values: %s'
%
','
.
join
(
mismatched
)
self
.
fail
(
self
.
_formatMessage
(
msg
,
standardMsg
))
def
assertCountEqual
(
self
,
first
,
second
,
msg
=
None
):
"""An unordered sequence comparison asserting that the same elements,
regardless of order. If the same element occurs more than once,
...
...
@@ -1111,11 +1140,13 @@ class TestCase(object):
return
deprecated_func
# see #9424
assertEquals
=
_deprecate
(
assertEqual
)
assertNotEquals
=
_deprecate
(
assertNotEqual
)
assertAlmostEquals
=
_deprecate
(
assertAlmostEqual
)
assertNotAlmostEquals
=
_deprecate
(
assertNotAlmostEqual
)
assert_
=
_deprecate
(
assertTrue
)
failUnlessEqual
=
assertEquals
=
_deprecate
(
assertEqual
)
failIfEqual
=
assertNotEquals
=
_deprecate
(
assertNotEqual
)
failUnlessAlmostEqual
=
assertAlmostEquals
=
_deprecate
(
assertAlmostEqual
)
failIfAlmostEqual
=
assertNotAlmostEquals
=
_deprecate
(
assertNotAlmostEqual
)
failUnless
=
assert_
=
_deprecate
(
assertTrue
)
failUnlessRaises
=
_deprecate
(
assertRaises
)
failIf
=
_deprecate
(
assertFalse
)
assertRaisesRegexp
=
_deprecate
(
assertRaisesRegex
)
assertRegexpMatches
=
_deprecate
(
assertRegex
)
...
...
Lib/unittest/test/_test_warnings.py
View file @
0f535013
...
...
@@ -19,12 +19,17 @@ def warnfun():
warnings
.
warn
(
'rw'
,
RuntimeWarning
)
class
TestWarnings
(
unittest
.
TestCase
):
# unittest warnings will be printed at most once per type
# unittest warnings will be printed at most once per type (max one message
# for the fail* methods, and one for the assert* methods)
def
test_assert
(
self
):
self
.
assertEquals
(
2
+
2
,
4
)
self
.
assertEquals
(
2
*
2
,
4
)
self
.
assertEquals
(
2
**
2
,
4
)
def
test_fail
(
self
):
self
.
failUnless
(
1
)
self
.
failUnless
(
True
)
def
test_other_unittest
(
self
):
self
.
assertAlmostEqual
(
2
+
2
,
4
)
self
.
assertNotAlmostEqual
(
4
+
4
,
2
)
...
...
Lib/unittest/test/test_assertions.py
View file @
0f535013
...
...
@@ -223,6 +223,15 @@ class TestLongMessage(unittest.TestCase):
"
\
+
\
{
'key'
:
'value'
\
}
$
",
"
\
+
\
{
'key'
:
'value'
\
}
:
oops
$
"])
def testAssertDictContainsSubset(self):
with warnings.catch_warnings():
warnings.simplefilter("
ignore
", DeprecationWarning)
self.assertMessages('assertDictContainsSubset', ({'key': 'value'}, {}),
["
^
Missing
:
'key'
$
", "
^
oops
$
",
"
^
Missing
:
'key'
$
",
"
^
Missing
:
'key'
:
oops
$
"])
def testAssertMultiLineEqual(self):
self.assertMessages('assertMultiLineEqual', ("", "
foo
"),
[r"
\
+
foo
$
", "
^
oops
$
",
...
...
Lib/unittest/test/test_case.py
View file @
0f535013
...
...
@@ -523,6 +523,36 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
self
.
assertRaises
(
self
.
failureException
,
self
.
assertNotIn
,
'cow'
,
animals
)
def
testAssertDictContainsSubset
(
self
):
with
warnings
.
catch_warnings
():
warnings
.
simplefilter
(
"ignore"
,
DeprecationWarning
)
self
.
assertDictContainsSubset
({},
{})
self
.
assertDictContainsSubset
({},
{
'a'
:
1
})
self
.
assertDictContainsSubset
({
'a'
:
1
},
{
'a'
:
1
})
self
.
assertDictContainsSubset
({
'a'
:
1
},
{
'a'
:
1
,
'b'
:
2
})
self
.
assertDictContainsSubset
({
'a'
:
1
,
'b'
:
2
},
{
'a'
:
1
,
'b'
:
2
})
with
self
.
assertRaises
(
self
.
failureException
):
self
.
assertDictContainsSubset
({
1
:
"one"
},
{})
with
self
.
assertRaises
(
self
.
failureException
):
self
.
assertDictContainsSubset
({
'a'
:
2
},
{
'a'
:
1
})
with
self
.
assertRaises
(
self
.
failureException
):
self
.
assertDictContainsSubset
({
'c'
:
1
},
{
'a'
:
1
})
with
self
.
assertRaises
(
self
.
failureException
):
self
.
assertDictContainsSubset
({
'a'
:
1
,
'c'
:
1
},
{
'a'
:
1
})
with
self
.
assertRaises
(
self
.
failureException
):
self
.
assertDictContainsSubset
({
'a'
:
1
,
'c'
:
1
},
{
'a'
:
1
})
one
=
''
.
join
(
chr
(
i
)
for
i
in
range
(
255
))
# this used to cause a UnicodeDecodeError constructing the failure msg
with
self
.
assertRaises
(
self
.
failureException
):
self
.
assertDictContainsSubset
({
'foo'
:
one
},
{
'foo'
:
'
\
uFFFD
'
})
def
testAssertEqual
(
self
):
equal_pairs
=
[
((),
()),
...
...
@@ -1097,11 +1127,19 @@ test case
Test that the deprecated methods raise a DeprecationWarning. See #9424.
"""
old
=
(
(
self
.
failIfEqual
,
(
3
,
5
)),
(
self
.
assertNotEquals
,
(
3
,
5
)),
(
self
.
failUnlessEqual
,
(
3
,
3
)),
(
self
.
assertEquals
,
(
3
,
3
)),
(
self
.
failUnlessAlmostEqual
,
(
2.0
,
2.0
)),
(
self
.
assertAlmostEquals
,
(
2.0
,
2.0
)),
(
self
.
failIfAlmostEqual
,
(
3.0
,
5.0
)),
(
self
.
assertNotAlmostEquals
,
(
3.0
,
5.0
)),
(
self
.
failUnless
,
(
True
,)),
(
self
.
assert_
,
(
True
,)),
(
self
.
failUnlessRaises
,
(
TypeError
,
lambda
_
:
3.14
+
'spam'
)),
(
self
.
failIf
,
(
False
,)),
(
self
.
assertDictContainsSubset
,
(
dict
(
a
=
1
,
b
=
2
),
dict
(
a
=
1
,
b
=
2
,
c
=
3
))),
(
self
.
assertRaisesRegexp
,
(
KeyError
,
'foo'
,
lambda
:
{}[
'foo'
])),
(
self
.
assertRegexpMatches
,
(
'bar'
,
'bar'
)),
)
...
...
@@ -1109,6 +1147,21 @@ test case
with
self
.
assertWarns
(
DeprecationWarning
):
meth
(
*
args
)
# disable this test for now. When the version where the fail* methods will
# be removed is decided, re-enable it and update the version
def
_testDeprecatedFailMethods
(
self
):
"""Test that the deprecated fail* methods get removed in 3.x"""
if
sys
.
version_info
[:
2
]
<
(
3
,
3
):
return
deprecated_names
=
[
'failIfEqual'
,
'failUnlessEqual'
,
'failUnlessAlmostEqual'
,
'failIfAlmostEqual'
,
'failUnless'
,
'failUnlessRaises'
,
'failIf'
,
'assertDictContainsSubset'
,
]
for
deprecated_name
in
deprecated_names
:
with
self
.
assertRaises
(
AttributeError
):
getattr
(
self
,
deprecated_name
)
# remove these in 3.x
def
testDeepcopy
(
self
):
# Issue: 5660
class
TestableTest
(
unittest
.
TestCase
):
...
...
Lib/unittest/test/test_runner.py
View file @
0f535013
...
...
@@ -257,17 +257,19 @@ class Test_TextTestRunner(unittest.TestCase):
return
[
b
.
splitlines
()
for
b
in
p
.
communicate
()]
opts
=
dict
(
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
cwd
=
os
.
path
.
dirname
(
__file__
))
ae_msg
=
b'Please use assertEqual instead.'
at_msg
=
b'Please use assertTrue instead.'
# no args -> all the warnings are printed, unittest warnings only once
p
=
subprocess
.
Popen
([
sys
.
executable
,
'_test_warnings.py'
],
**
opts
)
out
,
err
=
get_parse_out_err
(
p
)
self
.
assertIn
(
b'OK'
,
err
)
# check that the total number of warnings in the output is correct
self
.
assertEqual
(
len
(
out
),
1
1
)
self
.
assertEqual
(
len
(
out
),
1
2
)
# check that the numbers of the different kind of warnings is correct
for
msg
in
[
b'dw'
,
b'iw'
,
b'uw'
]:
self
.
assertEqual
(
out
.
count
(
msg
),
3
)
for
msg
in
[
b'rw'
]:
for
msg
in
[
ae_msg
,
at_msg
,
b'rw'
]:
self
.
assertEqual
(
out
.
count
(
msg
),
1
)
args_list
=
(
...
...
@@ -292,9 +294,11 @@ class Test_TextTestRunner(unittest.TestCase):
**
opts
)
out
,
err
=
get_parse_out_err
(
p
)
self
.
assertIn
(
b'OK'
,
err
)
self
.
assertEqual
(
len
(
out
),
1
3
)
self
.
assertEqual
(
len
(
out
),
1
4
)
for
msg
in
[
b'dw'
,
b'iw'
,
b'uw'
,
b'rw'
]:
self
.
assertEqual
(
out
.
count
(
msg
),
3
)
for
msg
in
[
ae_msg
,
at_msg
]:
self
.
assertEqual
(
out
.
count
(
msg
),
1
)
def
testStdErrLookedUpAtInstantiationTime
(
self
):
# see issue 10786
...
...
Misc/NEWS
View file @
0f535013
...
...
@@ -87,6 +87,8 @@ Core and Builtins
Library
-------
-
unittest
.
TestCase
.
assertSameElements
has
been
removed
.
-
sys
.
getfilesystemencoding
()
raises
a
RuntimeError
if
initfsencoding
()
was
not
called
yet
:
detect
bootstrap
(
startup
)
issues
earlier
.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment