How do you generate dynamic (parameterized) unit tests in python?

后端 未结 25 2173
面向向阳花
面向向阳花 2020-11-22 07:09

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\         


        
25条回答
  •  难免孤独
    2020-11-22 07:47

    I came across ParamUnittest the other day when looking at the source code to radon (example usage on the github repo). It should work with other frameworks that extend TestCase (like Nose).

    Here is an example:

    import unittest
    import paramunittest
    
    
    @paramunittest.parametrized(
        ('1', '2'),
        #(4, 3),    <---- uncomment to have a failing test
        ('2', '3'),
        (('4', ), {'b': '5'}),
        ((), {'a': 5, 'b': 6}),
        {'a': 5, 'b': 6},
    )
    class TestBar(TestCase):
        def setParameters(self, a, b):
            self.a = a
            self.b = b
    
        def testLess(self):
            self.assertLess(self.a, self.b)
    

提交回复
热议问题