This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Split enhanced assertion support out as a unittest.TestCase base class
Type: Stage:
Components: Versions:
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: michael.foord, ncoghlan
Priority: normal Keywords:

Created on 2012-06-07 07:40 by ncoghlan, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg162463 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2012-06-07 07:40
The unittest module has some lovely enhanced comparison operations, but they're currently hard to access outside a test run.

It would be rather convenient if they were split out into a separate base class so they could be used directly without needing to create an actual test case.

Possible API:

  class Assertions:
    # All the enhanced assertion machinery goes here
    # Nothing related to actually running test cases

  class TestCase(Assertions):
    # This adds support for setUp, tearDown, addCleanup etc, etc

My current workaround is to just define a dummy "runTest" method in a subclass and use that:

>>> class Assertions(unittest.TestCase):
...     def runTest(self): pass
... 
>>> check = Assertions()
>>> check.assertEqual([], ())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/unittest/case.py", line 511, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib64/python2.7/unittest/case.py", line 504, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: [] != ()
>>> check.assertEqual([1], [2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/unittest/case.py", line 511, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib64/python2.7/unittest/case.py", line 740, in assertListEqual
    self.assertSequenceEqual(list1, list2, msg, seq_type=list)
  File "/usr/lib64/python2.7/unittest/case.py", line 722, in assertSequenceEqual
    self.fail(msg)
  File "/usr/lib64/python2.7/unittest/case.py", line 408, in fail
    raise self.failureException(msg)
AssertionError: Lists differ: [1] != [2]

First differing element 0:
1
2

- [1]
+ [2]
msg162466 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2012-06-07 08:56
Why not just instantiate a TestCase instance and use that?

>>> from unittest import TestCase
>>> t = TestCase()
>>> t.assertEqual('foo', 'bar')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/compile/py3k-cpython/Lib/unittest/case.py", line 642, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/compile/py3k-cpython/Lib/unittest/case.py", line 1021, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))
  File "/compile/py3k-cpython/Lib/unittest/case.py", line 509, in fail
    raise self.failureException(msg)
AssertionError: 'foo' != 'bar'
- foo
+ bar
msg162469 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2012-06-07 11:14
Because that doesn't work in 2.x (it fails with a ValueError due to "runTest" being missing) and I forgot you had already changed that behaviour to make it more convenient in 3.x :)
History
Date User Action Args
2022-04-11 14:57:31adminsetgithub: 59229
2012-06-07 11:14:17ncoghlansetstatus: open -> closed
resolution: out of date
messages: + msg162469
2012-06-07 08:56:43michael.foordsetmessages: + msg162466
2012-06-07 07:40:55ncoghlancreate