Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_outcome_with_failed_test(self):
test_function = _test_func_fail
try:
test_function()
except:
exc_info = sys.exc_info()
ev = events.TestOutcomeEvent(test_function, None, result.FAIL, exc_info=exc_info)
reporter = create_plugin_instance()
reporter.testOutcome(ev)
self.assertEqual(len(reporter.test_results), 1, 'Actual contents: %s' % reporter.test_results)
test_result = reporter.test_results[0]
self.assertEqual(test_result['result'], result.FAIL)
self.assertEqual(test_result['name'], test_function.__name__)
self.assertEqual(test_result['description'], test_function.__doc__)
self.assertIsNotNone(test_result['traceback'])
self.assertIn('assert 1 == 2', test_result['traceback'])
def test_summary_stats_new_test(self):
ev = events.TestOutcomeEvent(_test_func, None, result.PASS)
reporter = create_plugin_instance()
reporter.testOutcome(ev)
self.assertIn('passed', reporter.summary_stats)
self.assertEqual(reporter.summary_stats['passed'], 1)
def test_outcome_processing_successful_test(self):
test_function = _test_func
ev = events.TestOutcomeEvent(test_function, None, result.PASS)
reporter = create_plugin_instance()
reporter.testOutcome(ev)
self.assertEqual(len(reporter.test_results), 1, 'Actual contents: %s' % reporter.test_results)
test_result = reporter.test_results[0]
self.assertEqual(test_result['result'], result.PASS)
self.assertEqual(test_result['name'], test_function.__name__)
self.assertEqual(test_result['description'], test_function.__doc__)
self.assertIsNone(test_result['traceback'])
def test_outcome_with_error_test_result(self):
test_function = _test_func_fail
try:
test_function()
except:
exc_info = sys.exc_info()
ev = events.TestOutcomeEvent(test_function, None, result.ERROR, exc_info=exc_info)
reporter = create_plugin_instance()
reporter.testOutcome(ev)
self.assertEqual(len(reporter.test_results), 1, 'Actual contents: %s' % reporter.test_results)
test_result = reporter.test_results[0]
self.assertEqual(test_result['result'], result.ERROR)
self.assertEqual(test_result['name'], test_function.__name__)
self.assertEqual(test_result['description'], test_function.__doc__)
self.assertIsNotNone(test_result['traceback'])
self.assertIn('assert 1 == 2', test_result['traceback'])
- prints test outcome label
- fires reporting hooks (:func:`reportSuccess`, :func:`reportFailure`,
etc)
"""
if event.outcome == result.ERROR:
self.reportCategories['errors'].append(event)
self._reportError(event)
elif event.outcome == result.FAIL:
if not event.expected:
self.reportCategories['failures'].append(event)
self._reportFailure(event)
else:
self.reportCategories['expectedFailures'].append(event)
self._reportExpectedFailure(event)
elif event.outcome == result.SKIP:
self.reportCategories['skipped'].append(event)
self._reportSkip(event)
elif event.outcome == result.PASS:
if event.expected:
self._reportSuccess(event)
else:
self.reportCategories['unexpectedSuccesses'].append(event)
self._reportUnexpectedSuccess(event)
else:
# generic outcome handling
self.reportCategories.setdefault(event.outcome, []).append(event)
self._reportOtherOutcome(event)
testcase = ET.SubElement(self.tree, 'testcase')
testcase.set('time', "%.6f" % self._time())
if not classname:
classname = test.__module__
testcase.set('classname', classname)
testcase.set('name', method)
msg = ''
if event.exc_info:
msg = util.exc_info_to_string(event.exc_info, test)
elif event.reason:
msg = event.reason
msg = string_cleanup(msg, self.keep_restricted)
if event.outcome == result.ERROR:
self.errors += 1
error = ET.SubElement(testcase, 'error')
error.set('message', 'test failure')
error.text = msg
elif event.outcome == result.FAIL and not event.expected:
self.failed += 1
failure = ET.SubElement(testcase, 'failure')
failure.set('message', 'test failure')
failure.text = msg
elif event.outcome == result.PASS and not event.expected:
self.skipped += 1
skipped = ET.SubElement(testcase, 'skipped')
skipped.set('message', 'test passes unexpectedly')
elif event.outcome == result.SKIP:
self.skipped += 1
skipped = ET.SubElement(testcase, 'skipped')
def testOutcome(self, event):
"""Handle testOutcome hook
- records test outcome in reportCategories
- prints test outcome label
- fires reporting hooks (:func:`reportSuccess`, :func:`reportFailure`,
etc)
"""
if event.outcome == result.ERROR:
self.reportCategories['errors'].append(event)
self._reportError(event)
elif event.outcome == result.FAIL:
if not event.expected:
self.reportCategories['failures'].append(event)
self._reportFailure(event)
else:
self.reportCategories['expectedFailures'].append(event)
self._reportExpectedFailure(event)
elif event.outcome == result.SKIP:
self.reportCategories['skipped'].append(event)
self._reportSkip(event)
elif event.outcome == result.PASS:
if event.expected:
self._reportSuccess(event)
else:
def import_session(rlog, session_export):
ssn = session.Session()
ssn.config = session_export['config']
ssn.hooks = RecordingPluginInterface()
ssn.verbosity = session_export['verbosity']
ssn.startDir = session_export['startDir']
ssn.topLevelDir = session_export['topLevelDir']
ssn.prepareSysPath()
loader_ = loader.PluggableTestLoader(ssn)
ssn.testLoader = loader_
result_ = result.PluggableTestResult(ssn)
ssn.testResult = result_
runner_ = runner.PluggableTestRunner(ssn) # needed??
ssn.testRunner = runner_
# load and register plugins, forcing multiprocess to the end
ssn.plugins = [
plugin(session=ssn) for plugin in session_export['pluginClasses']
if plugin is not MultiProcess
]
rlog.debug("Plugins loaded: %s", ssn.plugins)
for plugin in ssn.plugins:
plugin.register()
rlog.debug("Registered %s in subprocess", plugin)
# instantiating the plugin will register it.
ssn.plugins.append(MultiProcess(session=ssn))
failure.set('message', 'test failure')
failure.text = msg
elif event.outcome == result.PASS and not event.expected:
self.skipped += 1
skipped = ET.SubElement(testcase, 'skipped')
skipped.set('message', 'test passes unexpectedly')
elif event.outcome == result.SKIP:
self.skipped += 1
skipped = ET.SubElement(testcase, 'skipped')
if msg:
skipmsg = 'test skipped'
if event.reason:
skipmsg = 'test skipped: {}'.format(event.reason)
skipped.set('message', skipmsg)
skipped.text = msg
elif event.outcome == result.FAIL and event.expected:
self.skipped += 1
skipped = ET.SubElement(testcase, 'skipped')
skipped.set('message', 'expected test failure')
skipped.text = msg
system_out = ET.SubElement(testcase, 'system-out')
system_out.text = string_cleanup(
'\n'.join(event.metadata.get('logs', '')),
self.keep_restricted)
__unittest = True
class PluggableTestRunner(object):
"""Test runner that defers most work to plugins.
:param session: Test run session
.. attribute :: resultClass
Class to instantiate to create test result. Default:
:class:`nose2.result.PluggableTestResult`.
"""
resultClass = result.PluggableTestResult
def __init__(self, session):
self.session = session
def run(self, test):
"""Run tests.
:param test: A unittest :class:`TestSuite`` or :class:`TestClass`.
:returns: Test result
Fires :func:`startTestRun` and :func:`stopTestRun` hooks.
"""
result = self._makeResult()
executor = lambda suite, result: suite(result)
startTime = time.time()