Skip to content

Commit b198b5a

Browse files
committed
Implemented rouch support for class based tests
1 parent ad68e4b commit b198b5a

File tree

3 files changed

+47
-30
lines changed

3 files changed

+47
-30
lines changed

README.rst

-6
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,6 @@ Some plugins work, but you have to specify to load them in setup.cfg:
8484
pytest_django
8585
8686
87-
Notable missing features
88-
------------------------
89-
90-
* no support for class based tests
91-
92-
9387
Usage
9488
------
9589

hammett/__init__.py

+34-23
Original file line numberDiff line numberDiff line change
@@ -496,35 +496,46 @@ def main(verbose=False, fail_fast=False, quiet=False, filenames=None, drop_into_
496496
if not isinstance(module_markers, list):
497497
module_markers = [module_markers]
498498

499-
from hammett.impl import execute_test_function
499+
from hammett.impl import execute_test_function, execute_test_class
500+
from unittest import TestCase
500501
for name, f in list(module.__dict__.items()):
501502
if g.results['abort']:
502503
break
503-
if name.startswith('test_') and callable(f):
504-
for m in module_markers:
505-
f = m(f)
506-
507-
if match is not None:
508-
if match not in name:
509-
continue
510-
511-
if markers is not None:
512-
keep = False
513-
for f_marker in getattr(f, 'hammett_markers', []):
514-
if f_marker.name in markers:
515-
arg = markers[f_marker.name]
516-
if arg is not None:
517-
assert len(f_marker.args) == 1, 'hammett only supports filtering on single arguments to markers right now'
518-
if str(f_marker.args[0]) == arg:
519-
keep = True
520-
break
521-
else:
504+
505+
if match is not None:
506+
if match not in name:
507+
continue
508+
509+
for m in module_markers:
510+
f = m(f)
511+
512+
is_test_function = name.startswith('test_') and callable(f)
513+
is_test_class = isinstance(f, type) and issubclass(f, TestCase)
514+
515+
if not is_test_function and not is_test_class:
516+
continue
517+
518+
if markers is not None:
519+
keep = False
520+
for f_marker in getattr(f, 'hammett_markers', []):
521+
if f_marker.name in markers:
522+
arg = markers[f_marker.name]
523+
if arg is not None:
524+
assert len(f_marker.args) == 1, 'hammett only supports filtering on single arguments to markers right now'
525+
if str(f_marker.args[0]) == arg:
522526
keep = True
523527
break
524-
if not keep:
525-
continue
528+
else:
529+
keep = True
530+
break
531+
if not keep:
532+
continue
533+
534+
if is_test_function:
535+
execute_test_function(f'{module_name}.{name}', f, module_request)
536+
elif is_test_class:
537+
execute_test_class(f'{module_name}.{name}', f, module_request)
526538

527-
execute_test_function(module_name + '.' + name, f, module_request)
528539
if should_stop():
529540
break
530541

hammett/impl.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,6 @@ def inc_test_result(status, _name, _f, duration, stdout, stderr):
390390
hammett.g.should_stop = True
391391

392392
filename = _f.__module__.replace('.', os.sep) + '.py'
393-
assert _name.startswith(_f.__module__)
394393
_name = _name[len(_f.__module__) + 1:]
395394
hammett.g.result_db['test_results'][filename][_name] = dict(stdout=stdout, stderr=stderr, status=status)
396395

@@ -530,6 +529,19 @@ def execute_test_function(_name, _f, module_request):
530529
return run_test(_name, _f, module_request)
531530

532531

532+
def execute_test_class(_name, _c, module_request):
533+
test_case = _c()
534+
test_case.setUp()
535+
536+
for member_name in dir(test_case):
537+
if member_name.startswith('test_'):
538+
execute_test_function(f'{_name}.{member_name}', getattr(test_case, member_name), module_request)
539+
540+
test_case.tearDown()
541+
542+
# return run_test(_name, _f, module_request)
543+
544+
533545
class FakePytestParser:
534546
def parse_known_args(self, *args):
535547
class Fake:

0 commit comments

Comments
 (0)