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

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

    You can use nose-ittr plugin (pip install nose-ittr).

    It's very easy to integrate with existing tests, minimal changes (if any) are required. It also supports nose multiprocessing plugin.

    Not that you can also have a customize setup function per test.

    @ittr(number=[1, 2, 3, 4])   
    def test_even(self):   
        assert_equal(self.number % 2, 0)
    

    It is also possible to pass nosetest parameters like with their build-in plugin attrib, this way you can run only a specific test with specific parameter:

    nosetest -a number=2
    

提交回复
热议问题