简体   繁体   中英

Running parametric tests in Python

I usually keep my parametric tests as follow

class ShouldCheckSomeCondition(unittest.TestCase):
  def __init__(self, parameters...):
    ...

  def runTest():
    # given
    ...

  cases = [(p1, ...), (p2, ...), ...]

Then build them at the bottom of the file and create a main to run them:

def suite():
  test_suite = unittest.TestSuite()
  test_suite.add_tests([ShouldCheckSomeCondition(*params) for params in ShouldCheckSomeCondition.cases])
  return test_suite

if __name__ == '__main__':
    unittest.TextTestRunner().run(suite())

This is fine and all and I can run a single test file just fine. However, I can't make eg Nose to support this. Its discovery tries to build each case and fails. The 'main' is ignored. Does anyone have a good way to run many test files written in such manner? Alternatively, are there other ways to nicely write parametric tests in python so that some test runner will handle it?

因为您正在使用鼻子,所以我想看看参数化参数 ,这是鼻子参数化的延续

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM