Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new parameter to allow running tests with custom runner #20

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ your project.

~~~
usage: unittest-parallel [-h] [-v] [-q] [-f] [-b] [-k TESTNAMEPATTERNS]
[-s START] [-p PATTERN] [-t TOP] [-j COUNT]
[-s START] [-p PATTERN] [-t TOP] [-j COUNT] [-r RUNNER]
[--level {module,class,test}]
[--disable-process-pooling] [--coverage]
[--coverage-branch] [--coverage-rcfile RCFILE]
Expand All @@ -79,6 +79,9 @@ options:
-t TOP, --top-level-directory TOP
Top level directory of project (defaults to start
directory)
-r RUNNER, --runner RUNNER
Custom unittest runner module and class (Supported: TeamCity)
E.g.: `-r teamcity.unittestpy TeamcityTestRunner

parallelization options:
-j COUNT, --jobs COUNT
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package_dir =
= src
install_requires =
coverage >= 5.1
teamcity-messages == 1.32

[options.entry_points]
console_scripts =
Expand Down
112 changes: 112 additions & 0 deletions src/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1607,3 +1607,115 @@ def test_coverage_other(self):

Total coverage is 100.00%
''')


def test_invalid_custom_runner(self):
discover_suite = unittest.TestSuite(tests=[
unittest.TestSuite(tests=[
unittest.TestSuite(tests=[SuccessTestCase('mock_1')]),
])
])
with patch('multiprocessing.cpu_count', Mock(return_value=2)), \
patch('multiprocessing.get_context', new=MockMultiprocessingContext), \
patch('multiprocessing.Manager', new=MockMultiprocessingManager), \
patch('sys.stdout', StringIO()) as stdout, \
patch('sys.stderr', StringIO()) as stderr, \
patch('unittest.TestLoader.discover', Mock(return_value=discover_suite)):
with self.assertRaises(ModuleNotFoundError):
main(['-v', '--runner', 'invalid_method', 'InvalidClass'])

def test_teamcity_custom_runner(self):
discover_suite = unittest.TestSuite(tests=[
unittest.TestSuite(tests=[
unittest.TestSuite(tests=[SuccessTestCase('mock_1'), SuccessTestCase('mock_2'), SuccessTestCase('mock_3')])
])
])
with patch('multiprocessing.cpu_count', Mock(return_value=2)), \
patch('multiprocessing.get_context', new=MockMultiprocessingContext), \
patch('multiprocessing.Manager', new=MockMultiprocessingManager), \
patch('sys.stdout', StringIO()) as stdout, \
patch('sys.stderr', StringIO()) as stderr, \
patch('unittest.TestLoader.discover', Mock(return_value=discover_suite)):
main(['-v', '--runner', 'teamcity.unittestpy', 'TeamcityTestRunner'])

self.assertEqual(stdout.getvalue(), '')
if sys.version_info < (3, 11): # pragma: no cover
self.assertEqual(re.sub(r'\d+\.\d{3}s', '<SEC>s', stderr.getvalue()), '''\
Running 1 test suites (3 total tests) across 1 processes

mock_1 (tests.test_main.SuccessTestCase) ...
mock_1 (tests.test_main.SuccessTestCase) ... ok
mock_2 (tests.test_main.SuccessTestCase) ...
mock_2 (tests.test_main.SuccessTestCase) ... ok
mock_3 (tests.test_main.SuccessTestCase) ...
mock_3 (tests.test_main.SuccessTestCase) ... ok

----------------------------------------------------------------------
Ran 3 tests in <SEC>s

OK
''')
else: # pragma: no cover
self.assertEqual(re.sub(r'\d+\.\d{3}s', '<SEC>s', stderr.getvalue()), '''\
Running 1 test suites (3 total tests) across 1 processes

mock_1 (tests.test_main.SuccessTestCase.mock_1) ...
mock_1 (tests.test_main.SuccessTestCase.mock_1) ... ok
mock_2 (tests.test_main.SuccessTestCase.mock_2) ...
mock_2 (tests.test_main.SuccessTestCase.mock_2) ... ok
mock_3 (tests.test_main.SuccessTestCase.mock_3) ...
mock_3 (tests.test_main.SuccessTestCase.mock_3) ... ok

----------------------------------------------------------------------
Ran 3 tests in <SEC>s

OK
''')

def test_unittest_custom_runner_as_param(self):
discover_suite = unittest.TestSuite(tests=[
unittest.TestSuite(tests=[
unittest.TestSuite(tests=[SuccessTestCase('mock_1'), SuccessTestCase('mock_2'), SuccessTestCase('mock_3')])
])
])
with patch('multiprocessing.cpu_count', Mock(return_value=2)), \
patch('multiprocessing.get_context', new=MockMultiprocessingContext), \
patch('multiprocessing.Manager', new=MockMultiprocessingManager), \
patch('sys.stdout', StringIO()) as stdout, \
patch('sys.stderr', StringIO()) as stderr, \
patch('unittest.TestLoader.discover', Mock(return_value=discover_suite)):
main(['-v', '--runner', 'unittest', 'TextTestRunner'])

self.assertEqual(stdout.getvalue(), '')
if sys.version_info < (3, 11): # pragma: no cover
self.assertEqual(re.sub(r'\d+\.\d{3}s', '<SEC>s', stderr.getvalue()), '''\
Running 1 test suites (3 total tests) across 1 processes

mock_1 (tests.test_main.SuccessTestCase) ...
mock_1 (tests.test_main.SuccessTestCase) ... ok
mock_2 (tests.test_main.SuccessTestCase) ...
mock_2 (tests.test_main.SuccessTestCase) ... ok
mock_3 (tests.test_main.SuccessTestCase) ...
mock_3 (tests.test_main.SuccessTestCase) ... ok

----------------------------------------------------------------------
Ran 3 tests in <SEC>s

OK
''')
else: # pragma: no cover
self.assertEqual(re.sub(r'\d+\.\d{3}s', '<SEC>s', stderr.getvalue()), '''\
Running 1 test suites (3 total tests) across 1 processes

mock_1 (tests.test_main.SuccessTestCase.mock_1) ...
mock_1 (tests.test_main.SuccessTestCase.mock_1) ... ok
mock_2 (tests.test_main.SuccessTestCase.mock_2) ...
mock_2 (tests.test_main.SuccessTestCase.mock_2) ... ok
mock_3 (tests.test_main.SuccessTestCase.mock_3) ...
mock_3 (tests.test_main.SuccessTestCase.mock_3) ... ok

----------------------------------------------------------------------
Ran 3 tests in <SEC>s

OK
''')
10 changes: 9 additions & 1 deletion src/unittest_parallel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import tempfile
import time
import unittest
import importlib

import coverage

Expand Down Expand Up @@ -41,6 +42,8 @@ def main(argv=None):
help="Pattern to match tests ('test*.py' default)")
parser.add_argument('-t', '--top-level-directory', metavar='TOP',
help='Top level directory of project (defaults to start directory)')
parser.add_argument('-r', '--runner', nargs=2, metavar='RUNNER', default=unittest.TextTestRunner,
help='Custom unittest runner module and class')
group_parallel = parser.add_argument_group('parallelization options')
group_parallel.add_argument('-j', '--jobs', metavar='COUNT', type=int, default=0,
help='The number of test processes (default is 0, all cores)')
Expand Down Expand Up @@ -104,6 +107,11 @@ def main(argv=None):
if args.verbose > 1:
print(file=sys.stderr)

# Load the customer runner module (if provided)
if args.runner != unittest.TextTestRunner:
custom_module = importlib.import_module(args.runner[0])
args.runner = getattr(custom_module, args.runner[1])

# Run the tests in parallel
start_time = time.perf_counter()
multiprocessing_context = multiprocessing.get_context(method='spawn')
Expand Down Expand Up @@ -274,7 +282,7 @@ def run_tests(self, test_suite):

# Run unit tests
with _coverage(self.args, self.temp_dir):
runner = unittest.TextTestRunner(
runner = self.args.runner(
stream=StringIO(),
resultclass=ParallelTextTestResult,
verbosity=self.args.verbose,
Expand Down