Add unittest.TestCase.assertMappingEqual
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Feature or enhancement
Proposal:
unittest.TestCase has assertDictEqual which requires both arguments to be dict's. Would it make sense to add assertMappingEqual which would weaken this requirement to Mapping?
For example, this would be helpful with immutabledict which is not a dict.
Possible implementation (pseudocode) is to change the existing code:
def assertDictEqual(self, d1, d2, msg=None):
# [existing] assert d1 and d2 are dict's
# [existing] check that items are the same
to
def assertMappingEqual(self, d1, d2, msg=None):
# [new] assert d1 and d2 are Mapping's
# [existing] check that items are the same
def assertDictEqual(self, d1, d2, msg=None):
# [existing] assert d1 and d2 are dict's
self.assertMappingEqual(d2, d2, msg)
Considerations and comparison:
-
Alternatively, the checks in
assertDictEqualcould be weakened toMapping, however this might introduce problems into existing tests. -
For sequences, there are
assertListEqualandassertSequenceEqual. HereassertListEqualsimply callsassertSequenceEqualwithout any type checks. -
For sets, there are
assertSetEqualandassertCountEqual. Here the implementations are different.assertSetEqualdoes not check the types of the arguments, but requires them to have the methoddifference.assertCountEqualonly requires the arguments to be iterables. -
It is possible to replace
assertMappingEqual(a, b)withassertDictEqual({**a}, {**b}), however this is arguably ugly.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
- gh-121113
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start from the unittest.TestCase.assertDictEqual entry point and review the linked PR gh-121113. Determine whether a separate assertMappingEqual API is appropriate and preserve the existing dictionary behavior; done means the proposed mapping comparison is implemented with coverage for Mapping inputs and existing behavior remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100