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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:51:05+00:00 2026-05-10T21:51:05+00:00

I’m implementing unit tests for a family of functions that all share a number

  • 0

I’m implementing unit tests for a family of functions that all share a number of invariants. For example, calling the function with two matrices produce a matrix of known shape.

I would like to write unit tests to test the entire family of functions for this property, without having to write an individual test case for each function (particularly since more functions might be added later).

One way to do this would be to iterate over a list of these functions:

import unittest import numpy  from somewhere import the_functions from somewhere.else import TheClass  class Test_the_functions(unittest.TestCase):   def setUp(self):     self.matrix1 = numpy.ones((5,10))     self.matrix2 = numpy.identity(5)    def testOutputShape(unittest.TestCase):      '''Output of functions be of a certain shape'''      for function in all_functions:        output = function(self.matrix1, self.matrix2)        fail_message = '%s produces output of the wrong shape' % str(function)        self.assertEqual(self.matrix1.shape, output.shape, fail_message)  if __name__ == '__main__':   unittest.main() 

I got the idea for this from Dive Into Python. There, it’s not a list of functions being tested but a list of known input-output pairs. The problem with this approach is that if any element of the list fails the test, the later elements don’t get tested.

I looked at subclassing unittest.TestCase and somehow providing the specific function to test as an argument, but as far as I can tell that prevents us from using unittest.main() because there would be no way to pass the argument to the testcase.

I also looked at dynamically attaching ‘testSomething’ functions to the testcase, by using setattr with a lamdba, but the testcase did not recognize them.

How can I rewrite this so it remains trivial to expand the list of tests, while still ensuring every test is run?

  • 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. 2026-05-10T21:51:06+00:00Added an answer on May 10, 2026 at 9:51 pm

    You could use a metaclass to dynamically insert the tests. This works fine for me:

    import unittest  class UnderTest(object):      def f1(self, i):         return i + 1      def f2(self, i):         return i + 2  class TestMeta(type):      def __new__(cls, name, bases, attrs):         funcs = [t for t in dir(UnderTest) if t[0] == 'f']          def doTest(t):             def f(slf):                 ut=UnderTest()                 getattr(ut, t)(3)             return f          for f in funcs:             attrs['test_gen_' + f] = doTest(f)         return type.__new__(cls, name, bases, attrs)  class T(unittest.TestCase):      __metaclass__ = TestMeta      def testOne(self):         self.assertTrue(True)  if __name__ == '__main__':     unittest.main() 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Stack Overflow uses Markdown, for which there are many open… May 11, 2026 at 3:36 pm
  • added an answer It's an attribute. Attributes are a form of metadata that… May 11, 2026 at 3:36 pm
  • added an answer Here's roughly how I solved this: In lighttpd.conf $SERVER['socket'] ==… May 11, 2026 at 3:36 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.