跟踪错误只发生在有时在CI [英] Trace Bug which happends only sometimes in CI

查看:137
本文介绍了跟踪错误只发生在有时在CI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们无法重现。



测试代码在哪里:

  response = self.admin_client.post(url,post)
self.assertEqual(200,response.status_code,response)

有时候我们得到一个302,表单被保存。



我的想法来调试:

  with some_magic_trace.trace()as trace:
response = self.admin_client.post(url,post)
self.assertEqual(200,response.status_code,trace)

跟踪应包含由解释器执行的python行(filename,line offset,line as string)。



如何实现 some_magic_trace.trace()

解决方案

trace 模块为您提供了一个非常简单的解决方案n(不同于你要求的,但简单到足以试试)。

 从跟踪导入跟踪

tracer = Trace()
response = tracer.runfunc(self.admin_client.post,url,post)
self.assertEqual(200,response.status_code,response)

一个更复杂的解决方案,需要创建一个上下文管理器来保存跟踪并仅在异常情况下打印,需要使用 sys.settrace
只有您自己实现的模板可以是:

  class MyTracer():

def __init __(self):
self.trace = None

def newscope(self,frame,event,arg):
##实际工作应该在这里完成最小的例子
self.trace.append((frame,event,arg))
return None

def pprint(self):
##真正漂亮的打印跟踪信息应该在这里完成
print(self.trace)

def __enter __(self):
self.trace = []
sys.settrace(self。 $ new $)
返回自己

def __exit __(self,exc_type,exc_val,exc_tb):
sys.settrace(无)
如果exc_type不为None:
self.pprint()
##打印从exc_type,exc_val,exc_tb收集的一些信息

然后你可以:

  with MyTracer():
response = self.admin_clien t.post(url,post)
self.assertEqual(200,response.status_code,response)

这个想法是,MyTracer实例有一个跟踪方法 newscope ,可以在 self.trace 中保存一些有用的信息。在上下文的异常退出中,调用 pprint 方法;在正常退出时,跟踪信息被丢弃。



大多数工作必须在跟踪方法 newscope中完成
可以在此处找到跟踪功能的一些具体示例。


I have a strange bug in python code which only happens sometimes in CI.

We can't reproduce it.

Where is the test code:

response=self.admin_client.post(url, post)
self.assertEqual(200, response.status_code, response)

Sometimes we get a 302 which happens since the form gets saved.

My idea to debug this:

with some_magic_trace.trace() as trace:
    response=self.admin_client.post(url, post)
    self.assertEqual(200, response.status_code, trace)

The trace should contain the python lines (filename, line offset, line as string) executed by the interpreter.

How to implement some_magic_trace.trace()?

解决方案

The trace module gives you a very simple solution (different from what you are asking for, but simple enough to have a try.)

from trace import Trace

tracer = Trace()
response = tracer.runfunc(self.admin_client.post, url, post)
self.assertEqual(200, response.status_code, response)

A more complex solution that entails creating a context manager that saves the trace and prints it only on exceptions, requires the use of sys.settrace. Just a template for your own implementation could be:

class MyTracer():

    def __init__(self):
        self.trace = None

    def newscope(self, frame, event, arg):
        ## real work should be done here, just minimal example
        self.trace.append((frame, event, arg))
        return None

    def pprint(self):
        ## real pretty printing of trace info should be done here
        print(self.trace)

    def __enter__(self):
        self.trace = []
        sys.settrace(self.newscope)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.settrace(None)
        if exc_type is not None:
            self.pprint()
            ## print some info gathered from exc_type, exc_val, exc_tb

Then you can:

with MyTracer():
    response=self.admin_client.post(url, post)
    self.assertEqual(200, response.status_code, response)

The idea is that a MyTracer instance has a tracer method newscope that saves some useful info in self.trace. On an abnormal exit from the context the pprint method is called; on a normal exit the trace info is discarded.

Most of the work has to be done in the tracing method newscope. Some concrete examples of tracing functions can be found here.

这篇关于跟踪错误只发生在有时在CI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆