Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8765913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:24:47+00:00 2026-06-13T16:24:47+00:00

I want to add a custom assert method to a TestCase subclass. I tried

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:24:48+00:00Added an answer on June 13, 2026 at 4:24 pm

    This question was answered by Peter Otten on comp.lang.python.

    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)
    $ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add a custom model method to the admin filter, however it
I want to add a custom UIView that contains an CABasicAnimation to a UITableViewCell.
I want to add custom data to the insert method of the db library
I am using jQuery's UI dialogs and I want to add a custom method.
I have an NSMutableArray object that I want to add custom methods to. I
I have a view that I want to add some custom drawing to. I
I'm developing j2me application and want to add a custom font into that.So how
I want add my custom sidebar next right column all page. Please check this
I want to add custom jump list entries to my application in Windows 7.
I want to add custom attribute settings to a generated anchor link of Wordpress.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.