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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:54:36+00:00 2026-06-06T22:54:36+00:00

given a Python class hierarchy, say class Base: def method1 def method2 def method3

  • 0

given a Python class hierarchy, say

class Base:
    def method1
    def method2
    def method3
    ...
class Derived1(Base)
class Derived2(Base)

etc.

and given that Base defines some common interface which is re-implemented differently in each Derived class.
What would be the most straightforward way to unit test all member functions of all classes.
Since i’m having a class hierarchy, i thought of doing something like…

class MyTestCase(unittest.TestCase):
    def testMethod1:
        ## Just as an example. Can be similar
        self.failUnless(self.instance.method1())
    def testMethod2
    ...

And then setting up the test suite with test cases for different instantiations of the class hierarchy
But how do i get the “instance” parameter into the MyTestCase class?

  • 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-06T22:54:38+00:00Added an answer on June 6, 2026 at 10:54 pm

    The skip decorator applies to all of the subclasses as well as the base class so you cannot simply tell unittest to skip the base. The simplest thing is probably to not have the base test class derive from TestCase at all:

    import unittest
    
    class Base: pass
    class Derived1(Base): pass
    
    class BaseTest(object):
        cls = None
    
        def setUp(self):
            self.instance = self.cls()
    
        def test_Method1(self):
            print("run test_Method1, cls={}".format(self.cls))
    
    class TestParent(BaseTest, unittest.TestCase):
        cls = Base
    
    class TestDerived1(BaseTest, unittest.TestCase):
        cls = Derived1
    
    unittest.main()
    

    The equivalent code using pytest would be:

    class Base: pass
    class Derived1(Base): pass
    
    class BaseTest(object):
        cls = None
    
        def __init__(self):
            self.instance = self.cls()
    
        def test_Method1(self):
            print("run test_Method1, cls={}".format(self.cls))
    
    class TestParent(BaseTest):
        cls = Base
    
    class TestDerived1(BaseTest):
        cls = Derived1
    

    pytest by default looks in files with a test_ prefix for functions with a test_ prefix or classes with a Test prefix. In the test classes it looks for methods with the test_ prefix.

    So it should all just work as desired if converted to use pytest.

    Alternatively with pytest you could parametrize test functions with the classes to be tested but that’s probably a bigger change. Code like this would run the test once for each class listed in the instance fixture params:

    import pytest
    
    @pytest.fixture(params=[Base, Derived1])
    def instance(request: FixtureRequest) -> Base:
        cls: Base = request.param
        yield cls()
    
    def test_something(instance: Base) -> None:
        assert instance.method() == 42 # or whatever
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've stumbled in a tricky python question. Given ( updated ): class A(object): def
I have method that returns module path of given class name def findModulePath(path, className):
I was given this Python code that would calculate an MD5 value for any
I'm writing some serialization/deserialization code in Python that will read/write an inheritance hierarchy from
Given a python class class Student(): and a list names = [] ; then
Google Python Class | List Exercise - Given a list of numbers, return a
so i've begun google's python class, and have not had that much difficulty with
I have a third-party GUI program that I'm wrapping with a Python class (using
I have a Python 3 class method for rescaling values that looks like this:
I'm working on a Python class that lets the caller add widgets to a

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.