def test_toxml_can_accept_absent_package_name(self): ts_origin = 1401278400 test_a = Report( '<a-test>', start_ts=ts_origin+0, end_ts=ts_origin+1, src_location='' ) xunit_result = toxml([test_a], 'unicode-tests', 'test-hostname', package_name=None) validate_schema(xunit_result)
def test_toxml_must_accept_unicode(self): ts_origin = 1401278400 test_a = Report( '<a-test>', start_ts=ts_origin+0, end_ts=ts_origin+1, src_location=u'\u4e16\u754c' ) xunit_result = toxml([test_a], 'unicode-tests', 'test-hostname') validate_schema(xunit_result)
def test_toxml_has_optional_parameters(self): reports = [Report('a-test', start_ts=0, end_ts=1, src_location='foo')] xunit_result = toxml( reports, 'suite-name', ) validate_schema(xunit_result)
def test_toxml_must_escape_its_content(self): test_a = Report( '<a-test>', start_ts=0, end_ts=1, src_location='<"world">' ) test_a.errors.append('<a solid piece of "content">') test_b = Report( '<b-test>', start_ts=0, end_ts=1, src_location='<"world">' ) test_b.errors.append('<a "solid" piece of content>') xunit_result = toxml([test_a, test_b], 'escape-tests', 'test-hostname') validate_schema(xunit_result)
def test_toxml_has_optional_parameters(self): reports = [ Report( 'a-test', start_ts=0, end_ts=1, src_location='foo' ) ] xunit_result = toxml( reports, 'suite-name', ) validate_schema(xunit_result)
def handle_test(extra_data=None, **overwrite): """ create env, run test and record test results :param extra_data: extra data that runner or main passed to test case :param overwrite: args that runner or main want to overwrite :return: None """ # create env instance env_config = DefaultEnvConfig.get_default_config() for key in kwargs: if key in env_config: env_config[key] = kwargs[key] env_config.update(overwrite) env_inst = Env.Env(**env_config) # prepare for xunit test results xunit_file = os.path.join( env_inst.app_cls.get_log_folder(env_config["test_suite_name"]), XUNIT_FILE_NAME) XUNIT_RECEIVER.begin_case(test_func.__name__, time.time(), test_func_file_name) try: Utility.console_log("starting running test: " + test_func.__name__, color="green") # execute test function test_func(env_inst, extra_data) # if finish without exception, test result is True result = True except Exception as e: # handle all the exceptions here traceback.print_exc() result = False # log failure XUNIT_RECEIVER.failure(str(e), test_func_file_name) finally: # do close all DUTs env_inst.close() # end case and output result XUNIT_RECEIVER.end_case(test_func.__name__, time.time()) with open(xunit_file, "ab+") as f: f.write( xunitgen.toxml(XUNIT_RECEIVER.results(), XUNIT_DEFAULT_TEST_SUITE)) if result: Utility.console_log("Test Succeed: " + test_func.__name__, color="green") else: Utility.console_log(("Test Fail: " + test_func.__name__), color="red") TestResult.set_result(result, test_func.__name__) return result
def test_toxml_must_escape_its_content(self): ts_origin = 1401278400 test_a = Report( '<a-test>', start_ts=ts_origin+0, end_ts=ts_origin+1, src_location='<"world">' ) test_a.errors.append('<a solid piece of "content">') test_b = Report( '<b-test>', start_ts=ts_origin+0, end_ts=ts_origin+1, src_location='<"world">' ) test_b.errors.append('<a "solid" piece of content>') xunit_result = toxml([test_a, test_b], 'escape-tests', 'test-hostname') validate_schema(xunit_result)
def test_toxml(self): ts_origin = 1401278400 test_a = Report( 'a-test', start_ts=ts_origin+0, end_ts=ts_origin+1, src_location='foo' ) test_b = Report( 'b-test', start_ts=ts_origin+3, end_ts=ts_origin+5, src_location=os.path.join( 'this', 'is', 'a', 'bar').replace(os.sep, '.') ) test_c = Report( 'c-test', start_ts=ts_origin+3, end_ts=ts_origin+5, src_location=os.path.join( 'this', 'is', 'a', 'baz').replace(os.sep, '.') ) test_a.errors.append('this is an error') test_a.failures.append('this is a failure') test_b.errors.append('this is an error 2') test_b.errors.append('this is another error in the same test') test_reports = [test_a, test_b, test_c] xunit_reference = """<?xml version="1.0" encoding="UTF-8"?> <testsuites> <testsuite errors="2" failures="1" hostname="test-hostname" id="0" name="tests" package="pkg" tests="3" time="5.000000" timestamp="%s"> <testcase classname="foo" name="a-test" time="1.000000"> <failure message="this is a failure" type="exception" /> </testcase> <testcase classname="this.is.a.bar" name="b-test" time="2.000000"> <error message="this is an error 2 this is another error in the same test" type="exception" /> </testcase> <testcase classname="this.is.a.baz" name="c-test" time="2.000000"/> </testsuite> </testsuites> """ % datetime.fromtimestamp(ts_origin+0).isoformat() def xmlnorm(xmlstring): et = ET.fromstring(xmlstring) for e in [et] + et.findall('.//'): if e.tail is not None: e.tail = e.tail.strip(' \t\n\r') if e.text is not None: e.text = e.text.strip(' \t\n\r') return ET.tostring(et) xunit_result = toxml( test_reports, 'tests', hostname='test-hostname', package_name='pkg', ) print(xunit_result) self.assertEquals(xmlnorm(xunit_reference), xmlnorm(xunit_result)) validate_schema(xunit_result)
def handle_test(extra_data=None, **overwrite): """ create env, run test and record test results :param extra_data: extra data that runner or main passed to test case :param overwrite: args that runner or main want to overwrite :return: None """ env_config.update(overwrite) env_inst = Env.Env(**env_config) # prepare for xunit test results xunit_file = os.path.join(env_inst.app_cls.get_log_folder(env_config["test_suite_name"]), XUNIT_FILE_NAME) XUNIT_RECEIVER.begin_case(test_func.__name__, time.time(), test_func_file_name) try: Utility.console_log("starting running test: " + test_func.__name__, color="green") # execute test function test_func(env_inst, extra_data) # if finish without exception, test result is True result = True except Exception as e: # handle all the exceptions here traceback.print_exc() result = False # log failure XUNIT_RECEIVER.failure(str(e), test_func_file_name) finally: # do close all DUTs env_inst.close() # end case and output result XUNIT_RECEIVER.end_case(test_func.__name__, time.time()) with open(xunit_file, "ab+") as f: f.write(xunitgen.toxml(XUNIT_RECEIVER.results(), XUNIT_DEFAULT_TEST_SUITE)) if result: Utility.console_log("Test Succeed: " + test_func.__name__, color="green") else: Utility.console_log(("Test Fail: " + test_func.__name__), color="red") TestResult.set_result(result, test_func.__name__) return result