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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:16:28+00:00 2026-05-16T14:16:28+00:00

I have a group of test cases that all should have exactly the same

  • 0

I have a group of test cases that all should have exactly the same test done, along the lines of “Does method x return the name of an existing file?”

I thought that the best way to do it would be a base class deriving from TestCase that they all share, and simply add the test to that class. Unfortunately, the testing framework still tries to run the test for the base class, where it doesn’t make sense.

class SharedTest(TestCase):
    def x(self):
        ...do test...

class OneTestCase(SharedTest):
    ...my tests are performed, and 'SharedTest.x()'...

I tried to hack in a check to simply skip the test if it’s called on an object of the base class rather than a derived class like this:

    class SharedTest(TestCase):
        def x(self):
            if type(self) != type(SharedTest()):
                ...do test...
            else:
                pass

but got this error:

ValueError: no such test method in <class 'tests.SharedTest'>: runTest

First, I’d like any elegant suggestions for doing this. Second, though I don’t really want to use the type() hack, I would like to understand why it’s not working.

  • 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-16T14:16:28+00:00Added an answer on May 16, 2026 at 2:16 pm

    I faced a similar problem. I couldn’t prevent the test method in the base class being executed but I ensured that it did not exercise any actual code. I did this by checking for an attribute and returning immediately if it was set. This attribute was only set for the Base class and hence the tests ran everywhere else but the base class.

    class SharedTest(TestCase):
        def setUp(self):
            self.do_not_run = True
    
        def test_foo(self):
            if getattr(self, 'do_not_run', False):
                return
            # Rest of the test body.
    
    class OneTestCase(SharedTest):
        def setUp(self):
            super(OneTestCase, self).setUp()
            self.do_not_run = False
    

    This is a bit of a hack. There is probably a better way to do this but I am not sure how.

    Update

    As sdolan says a mixin is the right way. Why didn’t I see that before?

    Update 2

    (After reading comments) It would be nice if (1) the superclass method could avoid the hackish if getattr(self, 'do_not_run', False): check; (2) if the number of tests were counted accurately.

    There is a possible way to do this. Django picks up and executes all test classes in tests, be it tests.py or a package with that name. If the test superclass is declared outside the tests module then this won’t happen. It can still be inherited by test classes. For instance SharedTest can be located in app.utils and then used by the test cases. This would be a cleaner version of the above solution.

    # module app.utils.test
    class SharedTest(TestCase):
        def test_foo(self):
            # Rest of the test body.
    
    # module app.tests
    from app.utils import test
    class OneTestCase(test.SharedTest):
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a group of buttons that should act like toggle buttons, but also
i have class named Group i tried to test configuration: var cfg = new
currently in my update controller method I have: @group.attributes = { :title => params[:group][:title],
I have a group of links that I call fancybox on with the arrows
I have a group of radio buttons in my Swing application that have a
I have a group of numeric functions in Clojure that I want to validate
I have a simple test example that works fine, I set the drag/drop properties
I have a JUnit 4 test that loops through an array of test data:
Last year, my group developed a web service that included basic search functionality. All
Hi in my project we have hundreds of test cases.These test cases are part

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.