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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:04:19+00:00 2026-06-18T19:04:19+00:00

I have the following construct, in which I derive a class from unittest.TestCase ,

  • 0

I have the following construct, in which I derive a class from unittest.TestCase, in which a method is called that is defined in the actual test class MyTests.

import unittest

class TestCase1(unittest.TestCase):

    @classmethod
    def setUpClass(cls):  
        print cls.extra()

class TestCase2(TestCase1):

    @classmethod
    def setUpClass(cls):
        TestCase1.setUpClass()

class MyTests(TestCase1):

    @classmethod
    def extra(cls):
        return 42

    def test1(self):
        pass

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

The above code, as it stands, works as expected. When I execute the tests, the setUpClass of TestCase1 is run, which executed extra() defined in MyTests.

The problem comes into play when I want to use TestCase2 instead TestCase1, i.e. when I change the ‘1’ to ‘2’ in the following line:

class MyTests(TestCase2): # using TestCase2 instead

The idea is that setUpClass of TestCase2 is called, which in itself calls setUpClass from the basic class, i.e. TestCase1, which executes the method extra. However, I get an attribute error:

AttributeError: type object 'TestCase1' has no attribute 'extra'

I seem to misunderstand some concepts here. I would have expected my code to work in both cases (maybe there is some error), or to fail in both cases. Any ideas what here might be wrong or how I can fix the problem are greatly appreciated.

  • 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-18T19:04:21+00:00Added an answer on June 18, 2026 at 7:04 pm

    I highly recommend using super instead of TestCase1.setUpClass().

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

Sidebar

Related Questions

I have the following collection contract defined: [CollectionDataContract(Name = Centres)] public class Centres :
I have the following class. Which returns certain fields. I am interested in Reference.
I have the following LINQ query that I am using to construct a structure
I have the following call which runs a method within a cacheListener object asynchronously:
I have a class Wizard which creates a wizard with pages defined in the
I have the following construct, which shows a DataBound ListBox and a Button inside
I have the following sample code. class bar is derived from the base class
I have following situation. In a constructor of a pseudo class I attach a
I have the following coffeescript: class Vehicles constructor: (@name) -> class Car extends Vehicles
I have the following CoffeeScript code: class Person secret = 0 constructor: (@name, @age,

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.