Instantiate Python unittest.TestCase with arguments

后端 未结 3 548
暖寄归人
暖寄归人 2021-01-07 22:48

I would like to iterate over a list of items, and run an assertion on each of them. One example might be checking whether each number in a list is odd.

TestCas

3条回答
  •  暖寄归人
    2021-01-07 23:37

    According to "Python unit testing: parametrized test cases", published in Eli Bendersky's blog:

    Python’s standard unittest library is great and I use it all the time. One thing missing from it, however, is a simple way of running parametrized test cases. In other words, you can’t easily pass arguments into a unittest.TestCase from outside.

    Eli's solution is inheriting unittest.TestCase into ParametrizedTestCase. I'm not sure about copyright issues, so I won't copy-paste the code here.

    If there is any better solution, I will be happy to accept it.

提交回复
热议问题