Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30767

Re: How can I hide my stack frames in a TestCase subclass?

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'output': 0.04; '"__main__":': 0.07; '__name__': 0.07; 'msg': 0.07; 'unittest': 0.07; 'subject:How': 0.09; 'python': 0.09; '(1,': 0.09; 'itself,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:hide': 0.09; 'skip:= 70': 0.10; 'def': 0.10; 'stack': 0.15; '__unittest': 0.16; 'frame,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'sequence,': 0.16; 'wrote:': 0.17; 'tests': 0.18; 'module': 0.19; 'code.': 0.20; 'variable': 0.20; 'define': 0.20; 'import': 0.21; 'closely': 0.22; 'trace': 0.22; "user's": 0.22; 'seems': 0.23; 'raise': 0.24; 'tried': 0.25; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; 'skip:m 30': 0.26; '(most': 0.27; 'separate': 0.27; 'regular': 0.27; 'header:X-Complaints-To:1': 0.28; 'assert': 0.29; 'behaviour': 0.29; 'cat': 0.29; 'testcase': 0.29; 'reporting': 0.29; 'class': 0.29; 'file': 0.32; 'skip:s 30': 0.33; '11,': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'subject:?': 0.35; 'add': 0.36; 'received:org': 0.36; 'but': 0.36; 'stock': 0.36; 'method': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'possible.': 0.38; 'to:addr:python.org': 0.39; 'apply': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'details': 0.63; 'more': 0.63; 'delegate': 0.65; 'banks': 0.71; 'noise,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: How can I hide my stack frames in a TestCase subclass?
Date Fri, 05 Oct 2012 00:41:24 +0200
Organization None
References <52b17563-966c-46bc-bd77-da29e944c909@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084b9f8.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1826.1349400392.27098.python-list@python.org> (permalink)
Lines 118
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1349400392 news.xs4all.nl 6884 [2001:888:2000:d::a6]:51823
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:30767

Show key headers only | View raw


David Banks wrote:

> I want to add a custom assert method to a TestCase subclass.  I tried to
> copy my implementation from the unittest module so that it would match
> the behaviour of the regular TestCase as closely as possible.  (I would
> prefer to just delegate to self.assertEqual() but this causes even more
> backtrace noise, see below.)  The unittest module seems to automatically
> hide some internal details of its implementation when reporting failed
> assertions.
> 
>   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()
> 
> The output of this is as such:
> 
>   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)
> 
> Note that the custom assert method causes a stack trace with two frames,
> one inside the method itself, whereas the stock unittest 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?

Move MyTestCase in a separate module and define a global variable 

__unittest = True

$ cat mytestcase.py 
import unittest

__unittest = True

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)

$ cat mytestcase_demo.py 
import unittest
from mytestcase import MyTestCase

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)

if __name__ == "__main__":
    unittest.main()

$ python mytestcase_demo.py 
FF
======================================================================
FAIL: test_custom_assertion (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "mytestcase_demo.py", line 11, in test_custom_assertion
    self.assertLengthIsOne(self.seq)
AssertionError: length is not one

======================================================================
FAIL: test_stock_unittest_assertion (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "mytestcase_demo.py", line 8, in test_stock_unittest_assertion
    self.assertEqual(len(self.seq), 1)
AssertionError: 5 != 1

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=2)
$ 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How can I hide my stack frames in a TestCase subclass? David Banks <amoebae@gmail.com> - 2012-10-04 08:53 -0700
  Re: How can I hide my stack frames in a TestCase subclass? Peter Otten <__peter__@web.de> - 2012-10-05 00:41 +0200
    Re: How can I hide my stack frames in a TestCase subclass? Manuel Pégourié-Gonnard <mpg@elzevir.fr> - 2012-10-05 08:28 +0200
      Re: How can I hide my stack frames in a TestCase subclass? Peter Otten <__peter__@web.de> - 2012-10-05 10:12 +0200
        Re: How can I hide my stack frames in a TestCase subclass? Manuel Pégourié-Gonnard <mpg@elzevir.fr> - 2012-10-05 10:50 +0200
          Re: How can I hide my stack frames in a TestCase subclass? Peter Otten <__peter__@web.de> - 2012-10-05 11:26 +0200

csiph-web