craigahobbs / unittest-parallel

Parallel unit test runner for Python with coverage support
MIT License
29 stars 5 forks source link

How to run unittest-parallel from a python code? #18

Closed xhujerr closed 1 month ago

xhujerr commented 5 months ago

Currently I use unittest from a python module something like:

    loader = unittest.TestLoader()
    suite = loader.discover(parser.TestDirectory)

    runner = unittest.TextTestRunner()
    result = runner.run(suite)

To use parallelization I'd probably need to cherry pick snippets of code from your main function (separated by comments like f.e. # Run the tests in parallel). Would you mind splitting your main function so that I can import those parts easily?

craigahobbs commented 5 months ago

Will this work?

from unittest_parallel.main import main as unittest_parallel_main

unittest_parallel_main(['-s', parser.TestDirectory])
xhujerr commented 5 months ago

Yes, I'm able to run the tests as you suggest. Thank you.

result = unittest_parallel_main(['-s', parser.TestDirectory, "--level", "class"])

But that opens new questions (I can open a new issue if you prefer):

craigahobbs commented 5 months ago

No need to open a new issue.

You can wrap your main call with a try-except to catch the SystemExit exception and get the status code. Capturing output is an issue due to multiprocessing though.

A better approach might be to use subprocess:

import subprocess

run_args = ['unittest-parallel', '-s', parser.TestDirectory, '--level', 'class']
result = subprocess.run(run_args, stdout=subprocess.PIPE, text=True, stderr=subprocess.STDOUT)
if result.returncode:
    print(f'ERROR (code {result.returncode}):')
    print(result.stdout)
else:
    print('OK')

Does that work?

As for splitting up classes I'd recommend running unittest-parallel with different "-k" arguments (e.g. -k TestClass1, -k TestClass2) to find which classes run-long. I'd also recommend trying --level test if your tests are long-running.

craigahobbs commented 2 months ago

@xhujerr, curious to know if any of that worked for you. If no, were you able to find a solution for your needs?

craigahobbs commented 1 month ago

Closing due to inactivity