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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:23:08+00:00 2026-05-31T17:23:08+00:00

I am looking for ways / best practices on testing methods defined in an

  • 0

I am looking for ways / best practices on testing methods defined in an abstract base class. One thing I can think of directly is performing the test on all concrete subclasses of the base class, but that seems excessive at some times.

Consider this example:

import abc

class Abstract(object):

    __metaclass__ = abc.ABCMeta

    @abc.abstractproperty
    def id(self):
        return   

    @abc.abstractmethod
    def foo(self):
        print "foo"

    def bar(self):
        print "bar"

Is it possible to test bar without doing any subclassing?

  • 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-31T17:23:10+00:00Added an answer on May 31, 2026 at 5:23 pm

    As properly put by lunaryon, it is not possible. The very purpose of ABCs containing abstract methods is that they are not instantiatable as declared.

    However, it is possible to create a utility function that introspects an ABC, and creates a dummy, non abstract class on the fly. This function could be called directly inside your test method/function and spare you of having to wite boiler plate code on the test file just for testing a few methods.

    def concreter(abclass):
        """
        >>> import abc
        >>> class Abstract(metaclass=abc.ABCMeta):
        ...     @abc.abstractmethod
        ...     def bar(self):
        ...        return None
    
        >>> c = concreter(Abstract)
        >>> c.__name__
        'dummy_concrete_Abstract'
        >>> c().bar() # doctest: +ELLIPSIS
        (<abc_utils.Abstract object at 0x...>, (), {})
        """
        if not "__abstractmethods__" in abclass.__dict__:
            return abclass
        new_dict = abclass.__dict__.copy()
        for abstractmethod in abclass.__abstractmethods__:
            #replace each abc method or property with an identity function:
            new_dict[abstractmethod] = lambda x, *args, **kw: (x, args, kw)
        #creates a new class, with the overriden ABCs:
        return type("dummy_concrete_%s" % abclass.__name__, (abclass,), new_dict)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for some best practices/patterns to transform objects from one to another.
Just looking at ways of getting named constants in python. class constant_list: (A_CONSTANT, B_CONSTANT,
I've been looking at various sites for Zend Framework best practices and information, and
I'm looking for recommendations on best practices when implementing equality in a domain model.
I'm looking for best practices to solve this hard problem: My app has a
I would like to know if there are any ways one can perform 'locking'
I've been looking at ways to implement gmail-like messaging inside a browser, and arrived
I've been looking at ways people test their apps in order decide where to
I'm looking at ways to deploy a Ruby on Rails app (running on JRuby)
I'm currently looking for ways to create automated tests for a JAX-RS (Java API

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.