def validate_result(self, test, result, traceback=""): """Validate adding result gives the expected output. Args: test (rotest.core.case.TestCase): the test its result was added. result (str): result to add to the test. traceback (str): the traceback of the test. Raises: AssertionError. the result wasn't added as expected. """ if isinstance(test, TestBlock): return result_xml_file = os.path.join(test.work_dir, XMLHandler.XML_REPORT_PATH) expected_xml_file = self.expected_xml_files.next() expected_xml = xmltodict.parse(open(expected_xml_file, "rt").read(), dict_constructor=dict) result_xml = xmltodict.parse(open(result_xml_file, "rt").read(), dict_constructor=dict) self.assertEqual(expected_xml, result_xml) if __name__ == '__main__': colored_main(defaultTest='TestXMLHandler')
"MockSuite1": self.trimmed_two_test_case, "MockSuite2": self.error_case_descriptor }, "MockSuite1": self.trimmed_two_test_case, "MockSuite2": self.error_case_descriptor } } self.assertEqual(expected_test_descriptor, dict_from_test(MockTestSuite1(), "test_1 or tag2")) def test_non_case_sensitive_matching(self): """Test that tags matching is not case sensitive.""" expected_test_descriptor = { "MockTestSuite1": { "MockTestSuite": { "MockSuite1": self.trimmed_two_test_case, "MockSuite2": self.error_case_descriptor }, "MockSuite1": self.trimmed_two_test_case, "MockSuite2": self.error_case_descriptor } } self.assertEqual(expected_test_descriptor, dict_from_test(MockTestSuite1(), "TEST_1 or TAG2")) if __name__ == '__main__': colored_main(defaultTest='TestTagsMatching')
def get_runner(self): """Create and return the relevant test runner. Returns: BaseTestRunner. test runner object. """ client = BaseTestRunner(outputs=[], config=None, run_name=None, run_delta=False, save_state=False, enable_debug=False, stream=StringIO()) return client class TestRunnerResultSuite(unittest.TestSuite): """A test suite for runner results' tests.""" TESTS = [TestBaseRunnerResult, TestMultiprocessRunnerResult] def __init__(self): """Construct the class.""" super(TestRunnerResultSuite, self).__init__(unittest.makeSuite(test) for test in self.TESTS) if __name__ == '__main__': django.setup() colored_main(defaultTest='TestRunnerResultSuite')
actual_traceback = self.worksheet.cell_value(rowx=test_row, colx=self.TRACEBACK_COLUMN) self.assertEqual(traceback, actual_traceback, "In row %d column %d expected: %r, got: %r" % (displayed_row, self.TRACEBACK_COLUMN, traceback, actual_traceback)) def validate_stop_test_run(self): """Make a binary comparison. Raises: AssertionError. the result file differs from the expected file. """ expected_sheet = \ xlrd.open_workbook(self.EXPECTED_FILE_PATH).sheet_by_index(0) actual_sheet = \ xlrd.open_workbook(self.handler.output_file_path).sheet_by_index(0) self.assertEqual(actual_sheet.nrows, expected_sheet.nrows) self.assertEqual(actual_sheet.ncols, expected_sheet.ncols) for row, col in itertools.product(range(actual_sheet.nrows, actual_sheet.ncols)): actual_cell = actual_sheet.cell(row, col) expected_cell = expected_sheet.cell(row, col) self.assertEqual(actual_cell, expected_cell) if __name__ == '__main__': colored_main(defaultTest='TestExcelHandler')
def test_core_logger(self): """Log in core logger and verify logging occurs in this logger only.""" with open(self.core_log_file_path, 'r') as core_log_file: with open(self.test_log_file_path, 'r') as test_log_file: log_msg = '%s TEST_CORE_LOGGER' % time.ctime() core_log_file.seek(0, os.SEEK_END) test_log_file.seek(0, os.SEEK_END) core_log.debug(log_msg) core_log_file_content = core_log_file.read() test_log_file_content = test_log_file.read() self.assertEquals(core_log_file_content.count(log_msg), 1) self.assertEquals(test_log_file_content.count(log_msg), 0) def test_test_logger(self): """Log in test logger and verify logging occurs in both loggers.""" with open(self.core_log_file_path, 'r') as core_log_file: with open(self.test_log_file_path, 'r') as test_log_file: log_msg = '%s TEST_TEST_LOGGER' % time.ctime() core_log_file.seek(0, os.SEEK_END) test_log_file.seek(0, os.SEEK_END) self.test_log.debug(log_msg) core_log_file_content = core_log_file.read() test_log_file_content = test_log_file.read() self.assertEquals(core_log_file_content.count(log_msg), 1) self.assertEquals(test_log_file_content.count(log_msg), 1) if __name__ == '__main__': colored_main(defaultTest='TestLog')
TestSuite - Suite1 - Suite2 - TestSuite -- Suite1 -- Suite2 - Suite2 """ MockSuite1.components = (SuccessCase, ) MockSuite2.components = (FailureCase, ) MockNestedTestSuite.components = (MockSuite1, MockSuite2) MockTestSuite.components = (MockSuite1, MockSuite2, MockNestedTestSuite, MockSuite2) test_suite = MockTestSuite() self.assertEqual( ROTEST_WORK_DIR.rstrip(os.path.sep), os.path.dirname(test_suite.work_dir).rstrip(os.path.sep), "Test %r work directory %r is not contained in the base work " "directory %r" % (test_suite.data, test_suite.work_dir, ROTEST_WORK_DIR)) self.validate_work_dirs(test_suite) if __name__ == '__main__': django.setup() colored_main(defaultTest='TestTestSuite')
Since the tests mock client doesn't really lock any resources both workers will get the same resource data, but will initiate different resource instance, which is good enough for the test. """ ResourceIdRegistrationCase.pid_queue = self.pid_queue MockSuite1.components = (ResourceIdRegistrationCase, ResourceIdRegistrationCase) self.runner.run(MockSuite1) resources_locked = len(set(self.get_pids())) self.assertEqual( resources_locked, 2, "Number of resource locks was %d instead of 2" % resources_locked) class TestMultiprocessRunnerSuite(unittest.TestSuite): """A test suite for multiprocess runner's tests.""" TESTS = [TestMultiprocessRunner, TestMultipleWorkers] def __init__(self): """Construct the class.""" super(TestMultiprocessRunnerSuite, self).__init__(unittest.makeSuite(test) for test in self.TESTS) if __name__ == '__main__': django.setup() colored_main(defaultTest='TestMultiprocessRunnerSuite')
"Process continued when it should have been " "terminated due to timeout") self.validate_test_processes(2) def test_subprocess_killed_timeout(self): """Test that subprocess get killed when case get killed. * Runs a test which opens a subprocess, and sleeps until timeout. * Validates that two processes were run (worker and subprocess). * Validates that both process got killed. """ TimeoutWithSubprocessCase.pid_queue = self.pid_queue TimeoutWithSubprocessCase.post_timeout_event = self.post_timeout_event MockTestSuite.components = (TimeoutWithSubprocessCase, ) self.runner.run(MockTestSuite) self.assertFalse( self.post_timeout_event.is_set(), "Process continued when it should have been " "terminated due to timeout") self.validate_test_processes(2) if __name__ == '__main__': django.setup() colored_main(defaultTest='TestMultiprocessTimeouts', verbosity=2)
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME) test_resource.validation_result = False test_resource.save() self.assertTrue(test_resource.validate_flag, "Resource wasn't validated as expected") self.assertFalse(test_resource.initialization_flag, "Resource was unexpectedly initialized") def test_no_resource(self): """Test a TestCase with no required resource. * Defines no required resource. * Runs the test under a test suite. * Validates the result is success. """ TempSuccessCase.resources = () case = self._run_case(TempSuccessCase) # === Validate case data object === self.assertTrue(case.data.success) self.assertEqual( case.data.exception_type, TestOutcome.SUCCESS, "Unexpected test outcome, expected %r got %r" % (TestOutcome.SUCCESS, case.data.exception_type)) if __name__ == '__main__': django.setup() colored_main(defaultTest='TestTestCase')
ip_address="1.2.3.5", version=2) descriptors = [resource1.encode(), resource2.encode()] msg = LockResources(descriptors=descriptors, timeout=self.LOCK_RESOURCES_TIMEOUT) self.validate(msg) def test_release_resource_message(self): """Test encoding & decoding of ReleaseResources message.""" request1 = "resource1" request2 = "resource2" msg = ReleaseResources(requests=[request1, request2]) self.validate(msg) class TestXMLParser(AbstractTestParser): """Test the XML parser module.""" __test__ = True @classmethod def setUpClass(cls): """Initialize the parser.""" cls.PARSER = XMLParser() if __name__ == '__main__': django.setup() colored_main(defaultTest='TestXMLParser')
* Runs a suite with success & failure cases with no name. * Validates that both tests were run and one succeeded. * Runs a suite with success & failure cases with name X. * Validates that both tests were run and one succeeded. """ MockTestSuite.components = (SuccessCase, FailureCase) run_data, = run(MockTestSuite, delta_iterations=1, outputs=(DBHandler.NAME, ), run_name=None) self.validate_suite_data(run_data.main_test, False, successes=1, fails=1) run_data, = run(MockTestSuite, delta_iterations=1, outputs=(DBHandler.NAME, ), run_name='run1') self.validate_suite_data(run_data.main_test, False, successes=1, fails=1) if __name__ == '__main__': django.setup() colored_main(defaultTest='TestRunDelta')
FailureBlock.params(mode=MODE_CRITICAL), MockSubFlow.params(mode=MODE_FINALLY), MockSubFlow.params(mode=MODE_CRITICAL), MockSubFlow.params(mode=MODE_OPTIONAL)) test_flow = MockFlow() self.run_test(test_flow) self.assertFalse(self.result.wasSuccessful(), 'Flow succeeded when it should have failed') self.assertEqual(self.result.testsRun, 1, "Result didn't run the correct number of tests") self.assertEqual(len(self.result.failures), 1, "Result didn't fail the correct number of tests") self.validate_blocks(test_flow, successes=2, failures=1, skips=2) # === Validate data object === self.assertFalse(test_flow.data.success, 'Flow data result should have been False') self.assertEqual(test_flow.data.exception_type, TestOutcome.FAILED, 'Flow data status should have been failure') if __name__ == '__main__': django.setup() colored_main(defaultTest='TestTestFlow')
run_data = RunData(run_name=None) main_test = MockTestSuite(run_data=run_data) test_case = next(iter(main_test)) # Simulate starting the test. self.client.start_test_run(main_test) self.client.start_composite(main_test) self.client.start_test(test_case) # Check that the results are still None. self._validate_test_result(main_test, success=None) self._validate_test_result(test_case, success=None, error_tuple=(None, '')) # Simulate ending the test. self.client.stop_test(test_case) ERROR_STRING = 'test error' self.client.add_result(test_case, TestOutcome.ERROR, ERROR_STRING) self.client.stop_composite(main_test) # Check that the results are updated. self._validate_test_result(test_case, success=False, error_tuple=(TestOutcome.ERROR, ERROR_STRING)) self._validate_test_result(main_test, success=False) if __name__ == '__main__': django.setup() colored_main(defaultTest='TestResultManagement')
pass def validate_result(self, test, result, traceback=""): """Validate adding result works. Args: test (rotest.core.case.TestCase): the test its result was added. result (str): the result (e.g. "Success"). traceback (str): the traceback of the test. Raises: AssertionError. the result isn't as expected. """ if result in self.SIG_DICT: expected_name = self.SIG_DICT[result] sig_name = self.get_new_signature() self.assertEqual( sig_name, expected_name, "Signature name written %r does not match " "expected %r" % (sig_name, expected_name)) else: self.assertRaises(IndexError, self.worksheet.cell_value, rowx=self.current_row, colx=0) if __name__ == '__main__': colored_main(defaultTest='TestSignatureHandler')
expected_errors_count = self.NUM_OF_CLIENTS - len(available_resources) server_error_failures = results.count(self.SERVER_ERROR_CODE) self.assertEquals( server_error_failures, expected_errors_count, "Expected %d ServerError failures, found %d." % (expected_errors_count, server_error_failures)) results.remove(self.SERVER_ERROR_CODE) resources_names = [ resources[0].name for resources in results if isinstance(resources, list) ] self.assertEquals(set(resources_names), set(available_resources), "Not all resources were locked as expected.") class ResourceManagementParallelSuite(unittest.TestSuite): """A test suite for parallel tests.""" TESTS = [TwoClientsParallelCase, MultipleClientsParallelCase] def __init__(self): """Construct the class.""" super(ResourceManagementParallelSuite, self).__init__(unittest.makeSuite(test) for test in self.TESTS) if __name__ == '__main__': django.setup() colored_main(defaultTest='ResourceManagementParallelSuite')