def assertTestSuccess(test, inner): """The received test runs successfully.""" result = testtools.TestResult() inner.run(result) test.assertEqual(0, len(result.errors) + len(result.failures)) test.assertEqual(1, result.testsRun) return result
def test_test_case_without_context(self): # Use the default context value. test = SSTStringTestCase('ignored') test.script_code = 'assert True' result = testtools.TestResult() test.run(result) self.assertTrue(result.wasSuccessful())
def _attempt_test(self, case): """ Run 'case' with a temporary result. :param testtools.TestCase case: The test to run. :return: a tuple of ``(successful, result, details)``, where ``successful`` is a boolean indicating whether the test was succcessful, ``result`` is a _ResultType indicating what the test result was and ``details`` is a dictionary of testtools details. """ tmp_result = testtools.TestResult() # XXX: Still using internal API of testtools despite improvements in # #165. Will need to do follow-up work on testtools to ensure that # RunTest.run(case); RunTest.run(case) is supported. case._reset() self._run_test(case, tmp_result) result_type = _get_result_type(tmp_result) details = pmap(case.getDetails()) if result_type == _ResultType.skip: # XXX: Work around a testtools bug where it reports stack traces # for skips that aren't passed through its supported # SkipException: https://bugs.launchpad.net/testtools/+bug/1518100 [reason] = list(tmp_result.skip_reasons.keys()) details = details.discard('traceback').set('reason', text_content(reason)) return (tmp_result.wasSuccessful(), result_type, details)
def test_is_skipped(self): class WillSkip(tests.SSTBrowserLessTestCase): def test_it(self): self.skip('test reason') test = WillSkip('test_it') result = testtools.TestResult() test.run(result) self.assertIn('test reason', result.skip_reasons)
def test_screenshot_and_page_dump_disabled(self): test = SSTStringTestCase('ignored') test.script_code = 'assert False' test.screenshots_on = False test.results_directory = 'results' result = testtools.TestResult() test.run(result) self.assertEqual(0, len(result.errors)) self.assertEqual(1, len(result.failures)) # No screenshot required, no files files = os.listdir('results') self.assertEqual(0, len(files))
def test_retry_test_case_when_succeed(self): class MyTest(testtools.TestCase): @tobiko.retry_test_case() def test_success(self): pass result = testtools.TestResult() test_case = MyTest('test_success') test_case.run(result) self.assertEqual(1, result.testsRun) self.assertEqual([], result.failures) self.assertEqual([], result.errors) self.assertEqual({}, result.skip_reasons)
def test_retry_test_case_when_fails_once(self): class MyTest(testtools.TestCase): @tobiko.retry_test_case() def test_one_failure(self): count = next(count_calls) self.assertNotEqual(0, count) count_calls = itertools.count() result = testtools.TestResult() test_case = MyTest('test_one_failure') test_case.run(result) self.assertEqual(2, next(count_calls)) self.assertEqual(1, result.testsRun) self.assertEqual([], result.failures) self.assertEqual([], result.errors) self.assertEqual({}, result.skip_reasons)
def assertTestIsSuccessful(self, expected_value, test_name): result = testtools.TestResult() class TestWithInitctlEnvVar(testtools.TestCase): def setUp(inner): super().setUp() inner.useFixture(self.initctl_env_var) def test_value_set(inner): self.assertValueIs(expected_value) def test_variable_not_set(inner): self.assertVariableIsNotSet() TestWithInitctlEnvVar(test_name).run(result) self.assertTrue( result.wasSuccessful(), 'Failed to set the environment variable.')
def _attempt_test(self, case): """ Run 'case' with a temporary result. :param testtools.TestCase case: The test to run. :return: a tuple of ``(successful, result, details)``, where ``successful`` is a boolean indicating whether the test was succcessful, ``result`` is a _ResultType indicating what the test result was and ``details`` is a dictionary of testtools details. """ tmp_result = testtools.TestResult() # XXX: Still using internal API of testtools despite improvements in # #165. Will need to do follow-up work on testtools to ensure that # RunTest.run(case); RunTest.run(case) is supported. try: case._reset()
def test_results_directory_is_created(self): class WithResultsDir(tests.SSTBrowserLessTestCase): results_directory = 'results' def test_pass_and_leaves_a_results_dir(self): pass test = WithResultsDir('test_pass_and_leaves_a_results_dir') result = testtools.TestResult() test.run(result) # FIXME: The following assertion outlines that config.results_directory # is modified as a side-effect, this violates isolation # -- vila 2013-01-15 from sst import config self.assertTrue(os.path.exists(config.results_directory)) self.assertTrue(os.path.exists('results'))
def test_retry_test_case_when_skip(self): class MyTest(testtools.TestCase): @tobiko.retry_test_case() def test_skip(self): next(count_calls) self.skip("Not the right day!") count_calls = itertools.count() result = testtools.TestResult() test_case = MyTest('test_skip') test_case.run(result) self.assertEqual(1, next(count_calls)) self.assertEqual(1, result.testsRun) self.assertEqual([], result.failures) self.assertEqual([], result.errors) self.assertEqual({"Not the right day!": [test_case]}, result.skip_reasons)
def test_fake_home_fixture_patches_env_var(self): original_home = os.environ.get('HOME') fake_home = tempfile.gettempdir() result = testtools.TestResult() def inner_test(): class TestWithFakeHome(testtools.TestCase): def test_it(self): fake_home_fixture = fixture_setup.FakeHome(fake_home) fake_home_fixture.should_copy_xauthority_file = False self.useFixture(fake_home_fixture) self.assertEqual(fake_home, os.environ.get('HOME')) return TestWithFakeHome('test_it') inner_test().run(result) self.assertTrue(result.wasSuccessful(), 'Failed to fake the home: {}'.format(result.failures)) self.assertEqual(original_home, os.environ.get('HOME'))
def test_screenshot_and_page_dump_enabled(self): test = SSTStringTestCase('ignored') test.script_code = 'assert False' test.screenshots_on = True test.results_directory = 'results' result = testtools.TestResult() test.run(result) self.assertEqual(0, len(result.errors)) self.assertEqual(1, len(result.failures)) # We get a screenshot and a pagesource files = sorted(os.listdir('results')) self.assertEqual(2, len(files)) def byte_size(f): return os.path.getsize(os.path.join(test.results_directory, f)) self.assertGreater(byte_size(files[0]), 0) self.assertGreater(byte_size(files[1]), 0) self.assertThat(files[0], matchers.StartsWith('pagesource-')) self.assertThat(files[1], matchers.StartsWith('screenshot-'))
def test_retry_test_case_when_raises_errors(self): class MyTest(testtools.TestCase): @tobiko.retry_test_case() def test_errors(self): ex = ValueError('pippo') errors.append(ex) raise ex errors = [] result = testtools.TestResult() test_case = MyTest('test_errors') test_case.run(result) self.assertEqual(1, result.testsRun) self.assertEqual([], result.failures) self.assertEqual({}, result.skip_reasons) self.assertEqual(1, len(errors)) self.assertEqual(1, len(result.errors)) failed_test_case, traceback = result.errors[0] self.assertIs(test_case, failed_test_case) self.assertIn(str(errors[-1]), traceback)
def test_retry_test_case_when_fails(self): class MyTest(testtools.TestCase): @tobiko.retry_test_case() def test_failure(self): try: self.fail("this is failing") except tobiko.FailureException as ex: failures.append(ex) raise failures = [] result = testtools.TestResult() test_case = MyTest('test_failure') test_case.run(result) self.assertEqual(1, result.testsRun) self.assertEqual([], result.errors) self.assertEqual({}, result.skip_reasons) self.assertEqual(3, len(failures)) self.assertEqual(1, len(result.failures)) failed_test_case, traceback = result.failures[0] self.assertIs(test_case, failed_test_case) self.assertIn(str(failures[-1]), traceback)
def setUp(self): super(TestHandleExceptions, self).setUp() tests.set_cwd_to_tmp(self) self.result = testtools.TestResult()
def setUp(self): super(TestSSTTestCase, self).setUp() self.test = tests.SSTBrowserLessTestCase('run') self.addCleanup(shutil.rmtree, 'results') self.result = testtools.TestResult() self.test.run(self.result)
def run_successful_test(self, test): result = testtools.TestResult() test.run(result) self.assertTrue(result.wasSuccessful())
def run_failing_test(self, test): result = testtools.TestResult() test.run(result) self.assertEqual(0, len(result.errors)) self.assertEqual(1, len(result.failures))
def setUp(self): super(TestSSTTestCase, self).setUp() tests.set_cwd_to_tmp(self) self.test = tests.SSTBrowserLessTestCase('run') self.result = testtools.TestResult() self.test.run(self.result)
def assertRunSuccessfully(self, test): result = testtools.TestResult() test.run(result) self.assertEqual([], result.errors) self.assertEqual([], result.failures)