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

后端 未结 25 2131
面向向阳花
面向向阳花 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:49

    Following is my solution. I find this useful when: 1. Should work for unittest.Testcase and unittest discover 2. Have a set of tests to be run for different parameter settings. 3. Very simple no dependency on other packages import unittest

        class BaseClass(unittest.TestCase):
            def setUp(self):
                self.param = 2
                self.base = 2
    
            def test_me(self):
                self.assertGreaterEqual(5, self.param+self.base)
    
            def test_me_too(self):
                self.assertLessEqual(3, self.param+self.base)
    
    
    
         class Child_One(BaseClass):
            def setUp(self):
                BaseClass.setUp(self)
                self.param = 4
    
    
         class Child_Two(BaseClass):
            def setUp(self):
                BaseClass.setUp(self)
                self.param = 1
    
    0 讨论(0)
提交回复
热议问题