def _exc_info_to_details(self, exc_info): """Translate 'exc_info' into a details dictionary usable with subunit. """ # In an ideal world, we'd use the pre-bundled 'TracebackContent' class # from testtools. However, 'OutputFormatter' contains special logic to # handle errors from doctests, so we have to use that and manually # create an object equivalent to an instance of 'TracebackContent'. formatter = OutputFormatter(None) traceback = formatter.format_traceback(exc_info) # We have no idea if the traceback is a unicode object or a bytestring # with non-ASCII characters. We had best be careful when handling it. if isinstance(traceback, unicode): unicode_tb = traceback else: # Assume the traceback was utf-8 encoded, but still be careful. unicode_tb = traceback.decode('utf8', 'replace') return { 'traceback': content.Content( self.TRACEBACK_CONTENT_TYPE, lambda: [unicode_tb.encode('utf8')])}
def simple_app(environ, start_response): # 修改窗口名,目前只对windows有效 try: os.system("title {}_bdd_server".format(self_name)) except: pass setup_testing_defaults(environ) status = '200 OK' headers = [('Content-type', 'text/plain')] start_response(status, headers) # the environment variable CONTENT_LENGTH may be empty or missing try: request_body_size = int(environ.get('CONTENT_LENGTH', 0)) except (ValueError): request_body_size = 0 # When the method is POST the query string will be sent # in the HTTP request body which is passed by the WSGI server # in the file like wsgi.input environment variable. # 从http request解析请求 request_body = environ['wsgi.input'].read(request_body_size) post = parse_qs(request_body) step_data = json.loads(post['data'][0]) step = step_data['step'].strip() # 1. 解析step # 2. 执行step # 3. 分类step执行结果 # 4. 返回http response # 0:成功,1:业务失败,2:异常 result = 0 if step == '__reset__': print('*********************** run step **********************') print(u'Reset bdd environment...') environment.after_scenario(context, context.scenario) environment.before_scenario(context, context.scenario) resp = {'result': result, 'bdd_server_name': self_name} return base64.b64encode(json.dumps(resp)) else: # 解析请求携带的context _set_context_attrs(context, json.loads(step_data['context_attrs'])) if step_data['context_text']: step_content = step_data['context_text'] else: step_content = step_data['context_table'] step = u'%s\n"""\n%s\n"""' % (step_data['step'], step_content) print('*********************** run step **********************') print(step) context_attrs = {} traceback = '' try: context.execute_steps(step) except AssertionError: result = 1 print('*********************** failure **********************') traceback = full_stack() print(traceback.decode('utf-8')) except: result = 2 print( '*********************** exception **********************') traceback = full_stack() print(traceback.decode('utf-8')) else: result = 0 context_attrs = context._stack[0] resp = { 'result': result, 'traceback': traceback, 'context_attrs': context_attrs, 'bdd_server_name': self_name } # 传递context时忽略基本类型外的对象 return base64.b64encode(json.dumps(resp, default=_default))