PyTest/unittest: run multiple tests each with a new instance of Python interpreter

时间:2016-03-02 11:01:11

标签: python pytest

I need to test functions Initialize/Shutdown with different parameters. Each of these functions can be executed only once during app lifetime. Do I have to create 10 files with only one test function each or can I define 10 tests in one file and mark each function to be run using new instance of python interpreter?

Is this possible with either PyTest or the built-in unittest package?

1 个答案:

答案 0 :(得分:2)

我使用unittest工作。创建了_runner.py(下面的源代码),它使用测试发现(unittest.TestLoader)运行当前目录中的所有单元测试。它遍历所有测试套件并检查" IsolatedTest"的测试用例名称。话。这些将通过调用subprocess.check_output("python..")使用新的Python实例运行。其他人在当前流程中正常运行。例如,我宣布class FooIsolatedTest(unittest.TestCase)。在单独的测试中,使用此类代码替换unittest.main()import _runner; _runner.main(os.path.basename(__file__))。您可以查看来源here