本文整理汇总了Python中test_result.TestResult类的典型用法代码示例。如果您正苦于以下问题:Python TestResult类的具体用法?Python TestResult怎么用?Python TestResult使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TestResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
def run(self):
"""Delegator method encapsulating the flow for executing a TestCase instance.
This method tracks its progress in a TestResult with test_method 'run'.
This TestResult is used as a signal when running in client/server mode:
when the client is done running a TestCase and its fixtures, it sends
this TestResult to the server during the EVENT_ON_COMPLETE_TEST_CASE
phase.
This could be handled better. See
https://github.com/Yelp/Testify/issues/121.
"""
# The TestResult constructor wants an actual method, which it inspects
# to determine the method name (and class name, so it must be a method
# and not a function!). self.run is as good a method as any.
test_case_result = TestResult(self.run)
test_case_result.start()
self.fire_event(self.EVENT_ON_RUN_TEST_CASE, test_case_result)
self.__run_class_setup_fixtures()
self.__enter_class_context_managers(self.class_setup_teardown_fixtures, self.__run_test_methods)
self.__run_class_teardown_fixtures()
test_case_result.end_in_success()
self.fire_event(self.EVENT_ON_COMPLETE_TEST_CASE, test_case_result)
示例2: __enter_class_context_managers
def __enter_class_context_managers(self, fixture_methods, callback):
"""Transform each fixture_method into a context manager with contextlib.contextmanager, enter them recursively, and call callback"""
if fixture_methods:
fixture_method = fixture_methods[0]
ctm = contextmanager(fixture_method)()
enter_result = TestResult(fixture_method)
enter_result.start()
self.fire_event(self.EVENT_ON_RUN_CLASS_SETUP_METHOD, enter_result)
if self.__execute_block_recording_exceptions(ctm.__enter__, enter_result, is_class_level=True):
enter_result.end_in_success()
self.fire_event(self.EVENT_ON_COMPLETE_CLASS_SETUP_METHOD, enter_result)
self.__enter_context_managers(fixture_methods[1:], callback)
exit_result = TestResult(fixture_method)
exit_result.start()
self.fire_event(self.EVENT_ON_RUN_CLASS_TEARDOWN_METHOD, exit_result)
if self.__execute_block_recording_exceptions(
lambda: ctm.__exit__(None, None, None), exit_result, is_class_level=True
):
exit_result.end_in_success()
self.fire_event(self.EVENT_ON_COMPLETE_CLASS_TEARDOWN_METHOD, exit_result)
else:
callback()
示例3: TestCaseTest
class TestCaseTest(TestCase):
def setUp(self):
self.result = TestResult()
def testTemplateMethod(self):
test = WasRun("testMethod")
test.run(self.result)
# assert("setUp " == self.test.log)
assert("setUp testMethod tearDown" == test.log)
def testResult(self):
test = WasRun("testMethod")
test.run(self.result)
assert("1 run, 0 failed" == self.result.summary() )
def testFailedResult(self):
test = WasRun("testBrokenMethod")
test.run(self.result)
assert("1 run, 1 failed" == self.result.summary() )
def testFailedResultFormatting(self):
result.testStarted()
result.testFailed()
assert("1 run, 1 failed" == self.result.summary() )
def testSuite(self):
suite = TestSuite()
suite.add(WasRun("testMethod"))
suite.add(WasRun("testBrokenMethod"))
suite.run(self.result)
assert("2 run, 1 failed" == self.result.summary() )
示例4: post_result
def post_result(self, url, user, psswd):
self.logger.debug( "Posting result to %s"%url)
conf = {'result' : int(self.result),
'message' : self.summary,
'action' : self.script_name,
'test_id' : int(self.test_id) }
tr = TestResult(conf)
return tr.post(url, user, psswd)
示例5: post_result
def post_result(self, url, user, psswd):
print "Posting result to %s"%url
conf = {'result' : int(self.result),
'message' : self.summary,
'action' : self.project_file,
'test_id' : int(self.test_id) }
tr = TestResult(conf)
return tr.post(url, user, psswd)
示例6: run
def run(self):
"""Delegator method encapsulating the flow for executing a TestCase instance.
This method tracks its progress in a TestResult with test_method 'run'.
This TestResult is used as a signal when running in client/server mode:
when the client is done running a TestCase and its fixtures, it sends
this TestResult to the server during the EVENT_ON_COMPLETE_TEST_CASE
phase.
This could be handled better. See
https://github.com/Yelp/Testify/issues/121.
"""
# The TestResult constructor wants an actual method, which it inspects
# to determine the method name (and class name, so it must be a method
# and not a function!). self.run is as good a method as any.
test_case_result = TestResult(self.run)
test_case_result.start()
self.fire_event(self.EVENT_ON_RUN_TEST_CASE, test_case_result)
self._stage = self.STAGE_CLASS_SETUP
with self.__test_fixtures.class_context(
setup_callbacks=[
functools.partial(self.fire_event, self.EVENT_ON_RUN_CLASS_SETUP_METHOD),
functools.partial(self.fire_event, self.EVENT_ON_COMPLETE_CLASS_SETUP_METHOD),
],
teardown_callbacks=[
functools.partial(self.fire_event, self.EVENT_ON_RUN_CLASS_TEARDOWN_METHOD),
functools.partial(self.fire_event, self.EVENT_ON_COMPLETE_CLASS_TEARDOWN_METHOD),
],
) as class_fixture_failures:
# if we have class fixture failures, we're not going to bother
# running tests, but we need to generate bogus results for them all
# and mark them as failed.
self.__run_test_methods(class_fixture_failures)
self._stage = self.STAGE_CLASS_TEARDOWN
# class fixture failures count towards our total
self.failure_count += len(class_fixture_failures)
# you might think that we would want to do this... but this is a
# bogus test result used for reporting to the server. we always
# have it report success, i guess.
# for exc_info in fixture_failures:
# test_case_result.end_in_failure(exc_info)
if not test_case_result.complete:
test_case_result.end_in_success()
self.fire_event(self.EVENT_ON_COMPLETE_TEST_CASE, test_case_result)
示例7: __run_class_setup_fixtures
def __run_class_setup_fixtures(self):
"""Running the class's class_setup method chain."""
self._stage = self.STAGE_CLASS_SETUP
for fixture_method in self.class_setup_fixtures:
result = TestResult(fixture_method)
try:
for callback in self.__on_run_test_method_callbacks:
callback(self, fixture_method)
result.start()
if self.__execute_block_recording_exceptions(fixture_method, result, is_class_level=True):
result.end_in_success()
except (KeyboardInterrupt, SystemExit):
result.end_in_incomplete(sys.exc_info())
for callback in self.__on_complete_test_method_callbacks:
callback(self, result)
raise
else:
for callback in self.__on_complete_test_method_callbacks:
callback(self, result)
self.__run_deprecated_fixture_method('classSetUp')
示例8: run
def run(self):
"""Delegator method encapsulating the flow for executing a TestCase instance.
This method tracks its progress in a TestResult with test_method 'run'.
This TestResult is used as a signal when running in client/server mode:
when the client is done running a TestCase and its fixtures, it sends
this TestResult to the server during the EVENT_ON_COMPLETE_TEST_CASE
phase.
This could be handled better. See
https://github.com/Yelp/Testify/issues/121.
"""
# The TestResult constructor wants an actual method, which it inspects
# to determine the method name (and class name, so it must be a method
# and not a function!). self.run is as good a method as any.
test_case_result = TestResult(self.run)
test_case_result.start()
self.fire_event(self.EVENT_ON_RUN_TEST_CASE, test_case_result)
fixtures = []
all_class_fixtures = self.class_setup_fixtures + self.class_setup_teardown_fixtures + self.class_teardown_fixtures
for fixture in sorted(all_class_fixtures, key=make_sortable_fixture_key):
# We convert all class-level fixtures to
# class_setup_teardown fixtures a) to handle all
# class-level fixtures the same and b) to make the
# behavior more predictable when a TestCase has different
# fixtures interacting.
if fixture._fixture_type == 'class_teardown':
fixture = self.__convert_class_teardown_to_class_setup_teardown(fixture)
elif fixture._fixture_type == 'class_setup':
fixture = self.__convert_class_setup_to_class_setup_teardown(fixture)
fixtures.append(fixture)
self.__enter_class_context_managers(fixtures, self.__run_test_methods)
test_case_result.end_in_success()
self.fire_event(self.EVENT_ON_COMPLETE_TEST_CASE, test_case_result)
示例9: __run_class_fixtures
def __run_class_fixtures(self, stage, fixtures, callback_on_run_event, callback_on_complete_event):
"""Set the current _stage, run a set of fixtures, calling callbacks before and after each."""
self._stage = stage
for fixture_method in fixtures:
result = TestResult(fixture_method)
try:
for callback in self.__callbacks[callback_on_run_event]:
callback(result.to_dict())
result.start()
if self.__execute_block_recording_exceptions(fixture_method, result, is_class_level=True):
result.end_in_success()
except (KeyboardInterrupt, SystemExit):
result.end_in_interruption(sys.exc_info())
raise
finally:
for callback in self.__callbacks[callback_on_complete_event]:
callback(result.to_dict())
示例10: __run_class_fixture
def __run_class_fixture(self, fixture_method, function_to_call, stage, callback_on_run_event, callback_on_complete_event, fire_events=True):
self._stage = stage
result = TestResult(fixture_method)
try:
result.start()
if fire_events:
self.fire_event(callback_on_run_event, result)
if self.__execute_block_recording_exceptions(function_to_call, result, is_class_level=True):
result.end_in_success()
else:
self.failure_count += 1
except (KeyboardInterrupt, SystemExit):
result.end_in_interruption(sys.exc_info())
raise
finally:
if fire_events:
self.fire_event(callback_on_complete_event, result)
示例11: __run_deprecated_fixture_method
def __run_deprecated_fixture_method(self, fixture_name):
"""This runs an old-style (eg/ 'def setUp') fixture method."""
if hasattr(self, fixture_name):
deprecated_method = getattr(self, fixture_name)
if fixture_name.startswith('class'):
result = TestResult(deprecated_method)
try:
for callback in self.__on_run_test_method_callbacks:
callback(self, deprecated_method)
result.start()
if self.__execute_block_recording_exceptions(deprecated_method, result, is_class_level=True):
result.end_in_success()
except (KeyboardInterrupt, SystemExit):
result.end_in_incomplete(sys.exc_info())
for callback in self.__on_complete_test_method_callbacks:
callback(self, result)
raise
else:
for callback in self.__on_complete_test_method_callbacks:
callback(self, result)
else:
deprecated_method()
示例12: __run_class_teardown_fixtures
def __run_class_teardown_fixtures(self):
"""End the process of running tests. Run the class's class_teardown methods"""
self._stage = self.STAGE_CLASS_TEARDOWN
self.__run_deprecated_fixture_method('classTearDown')
for fixture_method in self.class_teardown_fixtures:
result = TestResult(fixture_method)
try:
for callback in self.__on_run_test_method_callbacks:
callback(self, fixture_method)
result.start()
if self.__execute_block_recording_exceptions(fixture_method, result, is_class_level=True):
result.end_in_success()
except (KeyboardInterrupt, SystemExit):
result.end_in_incomplete(sys.exc_info())
for callback in self.__on_complete_test_method_callbacks:
callback(self, result)
raise
else:
for callback in self.__on_complete_test_method_callbacks:
callback(self, result)
示例13: __run_test_methods
def __run_test_methods(self):
"""Run this class's setup fixtures / test methods / teardown fixtures.
These are run in the obvious order - setup and teardown go before and after,
respectively, every test method. If there was a failure in the class_setup
phase, no method-level fixtures or test methods will be run, and we'll eventually
skip all the way to the class_teardown phase. If a given test method is marked
as disabled, neither it nor its fixtures will be run. If there is an exception
during during the setup phase, the test method will not be run and execution
will continue with the teardown phase.
"""
for test_method in self.runnable_test_methods():
result = TestResult(test_method)
test_method.im_self.test_result = result
try:
self._method_level = True # Flag that we're currently running method-level stuff (rather than class-level)
# run "on-run" callbacks. eg/ print out the test method name
for callback in self.__callbacks[self.EVENT_ON_RUN_TEST_METHOD]:
callback(result.to_dict())
result.start()
if self.__class_level_failure:
result.end_in_failure(self.__class_level_failure)
elif self.__class_level_error:
result.end_in_error(self.__class_level_error)
else:
# first, run setup fixtures
self._stage = self.STAGE_SETUP
def _setup_block():
for fixture_method in self.setup_fixtures + [ self.setUp ]:
fixture_method()
self.__execute_block_recording_exceptions(_setup_block, result)
def _run_test_block():
# then run the test method itself, assuming setup was successful
self._stage = self.STAGE_TEST_METHOD
if not result.complete:
self.__execute_block_recording_exceptions(test_method, result)
def _setup_teardown_block():
self.__enter_context_managers(self.setup_teardown_fixtures, _run_test_block)
# then run any setup_teardown fixtures, assuming setup was successful.
if not result.complete:
self.__execute_block_recording_exceptions(_setup_teardown_block, result)
# finally, run the teardown phase
self._stage = self.STAGE_TEARDOWN
def _teardown_block():
for fixture_method in [ self.tearDown ] + self.teardown_fixtures:
fixture_method()
self.__execute_block_recording_exceptions(_teardown_block, result)
# if nothing's gone wrong, it's not about to start
if not result.complete:
result.end_in_success()
except (KeyboardInterrupt, SystemExit):
result.end_in_interruption(sys.exc_info())
raise
finally:
for callback in self.__callbacks[self.EVENT_ON_COMPLETE_TEST_METHOD]:
callback(result.to_dict())
self._method_level = False
if not result.success:
self.failure_count += 1
if self.failure_limit and self.failure_count >= self.failure_limit:
return
示例14: __run_class_fixtures
def __run_class_fixtures(self, stage, fixtures, callback_on_run_event, callback_on_complete_event):
"""Set the current _stage, run a set of fixtures, calling callbacks before and after each."""
self._stage = stage
for fixture_method in fixtures:
result = TestResult(fixture_method)
try:
self.fire_event(callback_on_run_event, result)
result.start()
if self.__execute_block_recording_exceptions(fixture_method, result, is_class_level=True):
result.end_in_success()
else:
if self.__class_level_failure:
result.end_in_failure(self.__class_level_failure)
### Bump failure count?
### Something about failure_limit?
elif self.__class_level_error:
result.end_in_error(self.__class_level_error)
### Bump failure count?
### Something about failure_limit?
else:
raise Exception("Couldn't find a class-level failure or error even"
" though we failed while executing a class-level fixture."
" This should not be possible. Aborting.")
except (KeyboardInterrupt, SystemExit):
result.end_in_interruption(sys.exc_info())
raise
finally:
self.fire_event(callback_on_complete_event, result)
示例15: __run_test_methods
def __run_test_methods(self, class_fixture_failures):
"""Run this class's setup fixtures / test methods / teardown fixtures.
These are run in the obvious order - setup and teardown go before and after,
respectively, every test method. If there was a failure in the class_setup
phase, no method-level fixtures or test methods will be run, and we'll eventually
skip all the way to the class_teardown phase. If a given test method is marked
as disabled, neither it nor its fixtures will be run. If there is an exception
during the setup phase, the test method will not be run and execution
will continue with the teardown phase.
"""
for test_method in self.runnable_test_methods():
result = TestResult(test_method)
# Sometimes, test cases want to take further action based on
# results, e.g. further clean-up or reporting if a test method
# fails. (Yelp's Selenium test cases do this.) If you need to
# programatically inspect test results, you should use
# self.results().
# NOTE: THIS IS INCORRECT -- im_self is shared among all test
# methods on the TestCase instance. This is preserved for backwards
# compatibility and should be removed eventually.
try:
# run "on-run" callbacks. e.g. print out the test method name
self.fire_event(self.EVENT_ON_RUN_TEST_METHOD, result)
result.start()
self.__all_test_results.append(result)
# if class setup failed, this test has already failed.
self._stage = self.STAGE_CLASS_SETUP
for exc_info in class_fixture_failures:
result.end_in_failure(exc_info)
if result.complete:
continue
# first, run setup fixtures
self._stage = self.STAGE_SETUP
with self.__test_fixtures.instance_context() as fixture_failures:
# we haven't had any problems in class/instance setup, onward!
if not fixture_failures:
self._stage = self.STAGE_TEST_METHOD
result.record(test_method)
self._stage = self.STAGE_TEARDOWN
# maybe something broke during teardown -- record it
for exc_info in fixture_failures:
result.end_in_failure(exc_info)
# if nothing's gone wrong, it's not about to start
if not result.complete:
result.end_in_success()
except (KeyboardInterrupt, SystemExit):
result.end_in_interruption(sys.exc_info())
raise
finally:
self.fire_event(self.EVENT_ON_COMPLETE_TEST_METHOD, result)
if not result.success:
self.failure_count += 1
if self.failure_limit and self.failure_count >= self.failure_limit:
break