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

  • Home
  • SEARCH
  • 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 256107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:59:11+00:00 2026-05-11T21:59:11+00:00

I’m learning Python and have been trying to understand more about the details of

  • 0

I’m learning Python and have been trying to understand more about the details of Python’s unittest module. The documentation includes the following:

For the ease of running tests, as we
will see later, it is a good idea to
provide in each test module a callable
object that returns a pre-built test
suite:

def suite():
    suite = unittest.TestSuite()
    suite.addTest(WidgetTestCase('testDefaultSize'))
    suite.addTest(WidgetTestCase('testResize'))
    return suite

As far as I can tell, the purpose of doing this is not explained. In addition, I was unable to figure out how one would use such a method. I tried several things without success (aside from learning about the error messages I got):

import unittest

def average(values):
    return sum(values) / len(values)

class MyTestCase(unittest.TestCase):
    def testFoo(self):
        self.assertEqual(average([10,100]),55)

    def testBar(self):
        self.assertEqual(average([11]),11)

    def testBaz(self):
        self.assertEqual(average([20,20]),20)

    def suite():
        suite = unittest.TestSuite()
        suite.addTest(MyTestCase('testFoo'))
        suite.addTest(MyTestCase('testBar'))
        suite.addTest(MyTestCase('testBaz'))
        return suite

if __name__ == '__main__':
    # s = MyTestCase.suite()
    # TypeError: unbound method suite() must be called 
    # with MyTestCase instance as first argument

    # s = MyTestCase.suite(MyTestCase())
    # ValueError: no such test method in <class '__main__.MyTestCase'>: runTest

    # s = MyTestCase.suite(MyTestCase('testFoo'))
    # TypeError: suite() takes no arguments (1 given)

The following “worked” but seems awkward and it required that I change the method signature of suite() to ‘def suite(self):‘.

s = MyTestCase('testFoo').suite()
unittest.TextTestRunner().run(s)
  • 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-05-11T21:59:11+00:00Added an answer on May 11, 2026 at 9:59 pm

    The very first error message you got is meaningful, and explains a lot.

    print MyTestCase.suite # <unbound method MyTestCase.suite>
    

    Unbound. It means that you cannot call it unless you bind it to an instance. It’s actually the same for MyTestCase.run:

    print MyTestCase.run # <unbound method MyTestCase.run>
    

    Maybe for now you don’t understand why you can’t call suite, but please leave it aside for now. Would you try to call run on the class, like above? Something like:

    MyTestCase.run() # ?
    

    Probably not, right? It does not make sense to write this, and it will not work, because run is an instance method, and needs a self instance to work on. Well it appears that Python “understands” suite in the same way it understands run, as an unbound method.

    Let’s see why:

    If you try to put the suite method out of the class scope, and define it as a global function, it just works:

    import unittest
    
    def average(values):
        return sum(values) / len(values)
    
    class MyTestCase(unittest.TestCase):
        def testFoo(self):
            self.assertEqual(average([10,100]),55)
    
        def testBar(self):
            self.assertEqual(average([11]),11)
    
        def testBaz(self):
            self.assertEqual(average([20,20]),20)
    
    def suite():
        suite = unittest.TestSuite()
        suite.addTest(MyTestCase('testFoo'))
        suite.addTest(MyTestCase('testBar'))
        suite.addTest(MyTestCase('testBaz'))
        return suite
    
    print suite() # <unittest.TestSuite tests=[<__main__.MyTestCase testMethod=testFoo>, <__main__.MyTestCase testMethod=testBar>, <__main__.MyTestCase testMethod=testBaz>]>
    

    But you don’t want it out of the class scope, because you want to call MyTestCase.suite()

    You probably thought that since suite was sort of “static”, or instance-independent, it did not make sense to put a self argument, did you?
    It’s right.

    But if you define a method inside a Python class, Python will expect that method to have a self argument as a first argument. Just omitting the self argument does not make your method static automatically. When you want to define a “static” method, you have to use the staticmethod decorator:

    @staticmethod
    def suite():
        suite = unittest.TestSuite()
        suite.addTest(MyTestCase('testFoo'))
        suite.addTest(MyTestCase('testBar'))
        suite.addTest(MyTestCase('testBaz'))
        return suite
    

    This way Python does not consider MyTestCase as an instance method but as a function:

    print MyTestCase.suite # <function suite at 0x...>
    

    And of course now you can call MyTestCase.suite(), and that will work as expected.

    if __name__ == '__main__':
        s = MyTestCase.suite()
        unittest.TextTestRunner().run(s) # Ran 3 tests in 0.000s, OK
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 135k
  • Answers 135k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Actually admin users + normal users can log in in… May 12, 2026 at 7:04 am
  • Editorial Team
    Editorial Team added an answer The C# specification does not allow inferring half of type… May 12, 2026 at 7:04 am
  • Editorial Team
    Editorial Team added an answer What I've ended up doing is adding the following to… May 12, 2026 at 7:04 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.