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 8436291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:08:29+00:00 2026-06-10T07:08:29+00:00

I am having little trouble using the python setUpClass. For example consider the following

  • 0

I am having little trouble using the python setUpClass.

For example consider the following case

class MyTest(unittest.case.TestCase):

    @classmethod
    def setUpClass(cls):
        print "Test setup"
        try:
            1/0
        except:
            raise

    @classmethod
    def tearDownClass(cls):
        print "Test teardown"

A couple of questions

  1. Is the above code the right way to handle test setUpClass exceptions (by raising it so that the python unittest can take care of it), there are fail(), skip() methods, but those can only be used by test instances and not the test classes.

  2. When there is a setUpClass exception, how can we ensure that tearDownClass runs (unittest doesn’t run it, should we manualy call it).

  • 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-10T07:08:30+00:00Added an answer on June 10, 2026 at 7:08 am

    You can call tearDownClass on an exception as Jeff points it out, but you may also implements the __del__(cls) method :

    import unittest
    
    class MyTest(unittest.case.TestCase):
    
        @classmethod
        def setUpClass(cls):
            print "Test setup"
            try:
                1/0
            except:
                raise
    
        @classmethod
        def __del__(cls):
            print "Test teardown"
    
        def test_hello(cls):
            print "Hello"
    
    if __name__ == '__main__':
        unittest.main()
    

    Will have the following output :

    Test setup
    E
    ======================================================================
    ERROR: setUpClass (__main__.MyTest)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "my_test.py", line 8, in setUpClass
        1/0
    ZeroDivisionError: integer division or modulo by zero
    
    ----------------------------------------------------------------------
    Ran 0 tests in 0.000s
    
    FAILED (errors=1)
    Test teardown
    

    Note : you should be aware that the __del__ method will be called at the end of the program execution, which is maybe not what you want if you have a more than one test class.

    Hope it helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a new Python programmer who is having a little trouble using 'self' in
I'm having a little trouble using AmazonCloudWatch to fetch CPU Utilization. When I try
I'm having little trouble creating a script working with URLs. I'm using urllib.urlopen() to
I'm having a little trouble (more like an annoyance, really) when using the Play
Question: I'm having a little trouble with jQuery: Consider this HTML, where I want
I'm having a little trouble with losing my array order after using unset() .
I am having a little trouble using addoperationwithblock in Cocoa. Let's say I have
I'm having a little trouble using jQuery in Rails. I'd like to call the
I'm having a little trouble using Hibernate with a char(6) column in Oracle. Here's
I'm having a little trouble about using multiple Left Joins in a query. Some

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.