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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:08:36+00:00 2026-05-20T05:08:36+00:00

I have several functions on graph f(), g() and h() that implements different algorithms

  • 0

I have several functions on graph f(), g() and h() that implements different algorithms for the same problem. I would like to unit-test these functions using unittest framework.

For each algorithm, several constraints should always be valid (such as empty graph, graph with only one node, and so on). The code for those common constraints checking should not be duplicated. So, the test architecture I started to design was the following:

class AbstractTest(TestCase):
  def test_empty(self):
      result = self.function(make_empty_graph())
      assertTrue(result....) # etc..
  def test_single_node(self):
      ...

Then the specific test cases

class TestF(AbstractTest):
  def setup(self):
      self.function = f
  def test_random(self):
      #specific test for algorithm 'f'

class TestG(AbstractTest):
  def setup(self):
      self.function = g
  def test_complete_graph(self):
      #specific test for algorithm 'g'

… And so on for each algorithm

Unfortunately, nosetests, tries to execute each test in AbstractTest and it does not work since the actual self.function is specified in subclasses. I tried setting __test__ = False in the AbstractTest Case, but in this case, no test is executed at all (since this field is inherited I suppose). I tried with abstract base class (abc.ABCMeta) without success. I have read about MixIn without any result (I am not really confident with it).

I am quite confident I am not the only one that try to factorize the test code. How do you do that in Python?

Thanks.

  • 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-20T05:08:37+00:00Added an answer on May 20, 2026 at 5:08 am

    Nose gathers classes that match a regex or are subclasses of unittest.TestCase, so the simplest solution is to do neither of those:

    class AlgoMixin(object):
      # Does not end in "Test"; not a subclass of unittest.TestCase.
      # You may prefer "AbstractBase" or something else.
    
      def test_empty(self):
        result = self.function(make_empty_graph())
        self.assertTrue(result)
    
    class TestF(AlgoMixin, unittest.TestCase):
      function = staticmethod(f)
      # Doesn't need to be in setup, nor be an instance attribute.
      # But doesn't take either self or class parameter, so use staticmethod.
    
      def test_random(self):
        pass  # Specific test for algorithm 'f'.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several old 3.5in floppy disks that I would like to backup. My
I have several functions which use jquery that are called across different pages. I'm
I have several functions that I wrote and I use regularly on my servers,
We have several jobs that run concurrently that have to use the same config
I would like a webpage that constantly updates a graph with new data as
I have an UI application that serves several functions (<800KB). I wanted to allow
In my library, I have several initialize() and cleanup() functions, for different modules it
I have several RequiredFieldValidators in an ASP.NET 1.1 web application that are firing on
I have several applications that are part of a suite of tools that various
I've a visual C++ project I'd like to debug. However, several functions are actually

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.