Skip to content

Adding support for startTestRun and stopTestRun #271

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

Open
wants to merge 2 commits into
base: master
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
27 changes: 27 additions & 0 deletions tests/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,33 @@ def test_xmlrunner_non_ascii(self):
u'message="demonstrating non-ascii skipping: éçà"'.encode('utf8'),
output)

def test_start_and_stop_test(self):
def startTestRun(self):
self.stream.writeln('startTestRun called')
def stopTestRun(self):
self.stream.writeln('stopTestRun called')

suite = unittest.TestSuite()
suite.addTest(self.DummyTest('test_pass'))
outdir = BytesIO()
runner = xmlrunner.XMLTestRunner(
stream=self.stream, output=outdir, verbosity=self.verbosity,
**self.runner_kwargs)
old_test_run_start = unittest.result.TestResult.startTestRun
old_test_run_stop = unittest.result.TestResult.stopTestRun
unittest.result.TestResult.startTestRun = startTestRun
unittest.result.TestResult.stopTestRun = stopTestRun
try:
runner.run(suite)
outdir.seek(0)
output = outdir.read()
testsuite_output = self.stream.getvalue()
self.assertIn('startTestRun called',testsuite_output)
self.assertIn('stopTestRun called',testsuite_output)
finally:
unittest.result.TestResult.startTestRun = old_test_run_start
unittest.result.TestResult.stopTestRun = old_test_run_stop

def test_xmlrunner_safe_xml_encoding_name(self):
suite = unittest.TestSuite()
suite.addTest(self.DummyTest('test_pass'))
Expand Down
10 changes: 9 additions & 1 deletion xmlrunner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ def run(self, test):

# Execute tests
start_time = time.monotonic()
test(result)
startTestRun = getattr(result, 'startTestRun', None)
if startTestRun is not None:
startTestRun()
try:
test(result)
finally:
stopTestRun = getattr(result, 'stopTestRun', None)
if stopTestRun is not None:
stopTestRun()
stop_time = time.monotonic()
time_taken = stop_time - start_time

Expand Down