Python unittest and discovery

后端 未结 3 1410
你的背包
你的背包 2021-02-01 14:01

I have directories, which contain files named like: test_foo.py

Each file is a test case.

I would like to

  1. Run all the tests in a direc

3条回答
  •  情歌与酒
    2021-02-01 14:34

    Once you have discovered tests, you can run them with a test runner.

    For Python 2:

    import unittest2
    loader = unittest2.TestLoader()
    tests = loader.discover('.')
    testRunner = unittest2.runner.TextTestRunner()
    testRunner.run(tests)
    

    For Python 3:

    import unittest
    loader = unittest.TestLoader()
    tests = loader.discover('.')
    testRunner = unittest.runner.TextTestRunner()
    testRunner.run(tests)
    

    Running the above code will print the test results to standard out.

提交回复
热议问题