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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:30:22+00:00 2026-05-10T14:30:22+00:00

I have some kind of test data and want to create a unit test

  • 0

I have some kind of test data and want to create a unit test for each item. My first idea was to do it like this:

import unittest  l = [["foo", "a", "a",], ["bar", "a", "b"], ["lee", "b", "b"]]  class TestSequence(unittest.TestCase):     def testsample(self):         for name, a,b in l:             print "test", name             self.assertEqual(a,b)  if __name__ == '__main__':     unittest.main() 

The downside of this is that it handles all data in one test. I would like to generate one test for each item on the fly. Any suggestions?

  • 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-10T14:30:23+00:00Added an answer on May 10, 2026 at 2:30 pm

    This is called "parametrization".

    There are several tools that support this approach. E.g.:

    • pytest’s decorator
    • parameterized

    The resulting code looks like this:

    from parameterized import parameterized  class TestSequence(unittest.TestCase):     @parameterized.expand([         ["foo", "a", "a",],         ["bar", "a", "b"],         ["lee", "b", "b"],     ])     def test_sequence(self, name, a, b):         self.assertEqual(a,b) 

    Which will generate the tests:

    test_sequence_0_foo (__main__.TestSequence) ... ok test_sequence_1_bar (__main__.TestSequence) ... FAIL test_sequence_2_lee (__main__.TestSequence) ... ok  ====================================================================== FAIL: test_sequence_1_bar (__main__.TestSequence) ---------------------------------------------------------------------- Traceback (most recent call last):   File "/usr/local/lib/python2.7/site-packages/parameterized/parameterized.py", line 233, in <lambda>     standalone_func = lambda *a: func(*(a + p.args), **p.kwargs)   File "x.py", line 12, in test_sequence     self.assertEqual(a,b) AssertionError: 'a' != 'b' 

    For historical reasons I’ll leave the original answer circa 2008):

    I use something like this:

    import unittest  l = [["foo", "a", "a",], ["bar", "a", "b"], ["lee", "b", "b"]]  class TestSequense(unittest.TestCase):     pass  def test_generator(a, b):     def test(self):         self.assertEqual(a,b)     return test  if __name__ == '__main__':     for t in l:         test_name = 'test_%s' % t[0]         test = test_generator(t[1], t[2])         setattr(TestSequense, test_name, test)     unittest.main() 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a myism table 'test' which holds some out-dated data, now I want
I've got a unit test I'm writing which seems to have some kind of
I have data which looks like this (this is test data for illustration): test
I have some kind of problem with jQuery selectors. Let's say i want to
I want to have some kind of single list that is initialized in a
I have a nice DataGridView showing what is basically some kind of log data
I know that's some kind of callback concept, but have no idea how to
I have a question about reflection I am trying to have some kind of
Here's a problem. Developers have some kind of a task (develop certain feature), which
I've figured I have to do some kind of API where the Android app

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.