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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:14:41+00:00 2026-05-14T22:14:41+00:00

Possible Duplicate: How do you generate dynamic (parameterized) unit tests in Python? I have

  • 0

Possible Duplicate:
How do you generate dynamic (parameterized) unit tests in Python?

I have a function to test, under_test, and a set of expected input/output pairs:

[
(2, 332),
(234, 99213),
(9, 3),
# ...
]

I would like each one of these input/output pairs to be tested in its own test_* method. Is that possible?

This is sort of what I want, but forcing every single input/output pair into a single test:

class TestPreReqs(unittest.TestCase):

    def setUp(self):
        self.expected_pairs = [(23, 55), (4, 32)]

    def test_expected(self):
        for exp in self.expected_pairs:
            self.assertEqual(under_test(exp[0]), exp[1])

if __name__ == '__main__':
    unittest.main()

(Also, do I really want to be putting that definition of self.expected_pairs in setUp?)

UPDATE: Trying doublep‘s advice:

class TestPreReqs(unittest.TestCase):

    def setUp(self):
        expected_pairs = [
                          (2, 3),
                          (42, 11),
                          (3, None),
                          (31, 99),
                         ]

        for k, pair in expected_pairs:
            setattr(TestPreReqs, 'test_expected_%d' % k, create_test(pair))

    def create_test (pair):
        def do_test_expected(self):
            self.assertEqual(get_pre_reqs(pair[0]), pair[1])
        return do_test_expected


if __name__ == '__main__':
    unittest.main()

This does not work. 0 tests are run. Did I adapt the example incorrectly?

  • 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-14T22:14:42+00:00Added an answer on May 14, 2026 at 10:14 pm

    Not tested:

    class TestPreReqs(unittest.TestCase):
        ...
    
    def create_test (pair):
        def do_test_expected(self):
            self.assertEqual(under_test(pair[0]), pair[1])
        return do_test_expected
    
    for k, pair in enumerate ([(23, 55), (4, 32)]):
        test_method = create_test (pair)
        test_method.__name__ = 'test_expected_%d' % k
        setattr (TestPreReqs, test_method.__name__, test_method)
    

    If you use this often, you could prettify this by using utility functions and/or decorators, I guess. Note that pairs are not an attribute of TestPreReqs object in this example (and so setUp is gone). Rather, they are “hardwired” in a sense to the TestPreReqs class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 514k
  • Answers 514k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The problem is that the default arguments must be constants.… May 16, 2026 at 6:21 pm
  • Editorial Team
    Editorial Team added an answer This custom UITableViewCell tutorial is good for starting. This is… May 16, 2026 at 6:21 pm
  • Editorial Team
    Editorial Team added an answer I generally put my LinearLayouts inside of a ScrollView to… May 16, 2026 at 6:21 pm

Trending Tags

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

Top Members

Related Questions

Possible Duplicate: How do you generate dynamic (parameterized) unit tests in Python? Is there
Possible Duplicate: py2exe - generate single executable file A friend of mine managed to
Possible Duplicate: How to generate a random number from within a range - C
Possible Duplicate: How do I generate a hashcode from a byte array in c#
Possible Duplicate: How does StackOverflow generate its SEO-friendly URLs? I made a simple form
Possible Duplicate: How do you efficiently generate a list of K non-repeating integers between
Possible Duplicate: How can I generate a tag cloud in Java, with OpenCloud? I
Possible Duplicate: c# random string generator I need to generate a random string with
Possible Duplicate: Secure random number generation in PHP We need to generate a cryptographically
Possible Duplicate: Generate MD5 hash in Java Can some one tell me how to

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.