Czarek Tomczak
Czarek Tomczak

Reputation: 20645

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

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?

Upvotes: 3

Views: 1067

Answers (1)

Czarek Tomczak
Czarek Tomczak

Reputation: 20645

I made it work with unittest. Created _runner.py (sources below) which runs all unit tests in current directory using test discovery (unittest.TestLoader). It loops through all test suites and checks test case names for "IsolatedTest" words. These will be run using new Python instance by calling subprocess.check_output("python.."). Others are run normally in current process. For example I'm declaring class FooIsolatedTest(unittest.TestCase). In isolated tests as a replacement for unittest.main() using such code: import _runner; _runner.main(os.path.basename(__file__)). You can take a look at sources here.

Upvotes: 2

Related Questions