Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I hide my stack frames in a TestCase subclass?
    primarykey
    data
    text
    <p>I want to add a custom assert method to a <code>TestCase</code> subclass. I tried to copy my implementation from the <code>unittest</code> module so that it would match the behaviour of the regular <code>TestCase</code> as closely as possible. (I would prefer to just delegate to <code>self.assertEqual()</code> but this causes even more backtrace noise, see below.) The <code>unittest</code> module seems to automatically hide some internal details of its implementation when reporting failed assertions.</p> <pre><code>import unittest class MyTestCase(unittest.TestCase): def assertLengthIsOne(self, sequence, msg=None): if len(sequence) != 1: msg = self._formatMessage(msg, "length is not one") raise self.failureException(msg) class TestFoo(MyTestCase): seq = (1, 2, 3, 4, 5) def test_stock_unittest_assertion(self): self.assertEqual(len(self.seq), 1) def test_custom_assertion(self): self.assertLengthIsOne(self.seq) unittest.main() </code></pre> <p>The output of this is as such:</p> <pre><code>amoe@vuurvlieg $ python unittest-demo.py FF ====================================================================== FAIL: test_custom_assertion (__main__.TestFoo) ---------------------------------------------------------------------- Traceback (most recent call last): File "unittest-demo.py", line 16, in test_custom_assertion self.assertLengthIsOne(self.seq) File "unittest-demo.py", line 7, in assertLengthIsOne raise self.failureException(msg) AssertionError: length is not one ====================================================================== FAIL: test_stock_unittest_assertion (__main__.TestFoo) ---------------------------------------------------------------------- Traceback (most recent call last): File "unittest-demo.py", line 13, in test_stock_unittest_assertion self.assertEqual(len(self.seq), 1) AssertionError: 5 != 1 ---------------------------------------------------------------------- Ran 2 tests in 0.000s FAILED (failures=2) </code></pre> <p>Note that the custom assert method causes a stack trace with two frames, one inside the method itself, whereas the stock <code>unittest</code> method only has one frame, the relevant line in the user's code. How can I apply this frame-hiding behaviour to my own method?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload