Python 访问after_all hook中的错误消息的方法

Python 访问after_all hook中的错误消息的方法,python,python-behave,Python,Python Behave,我正在尝试访问所有失败的消息,以便在测试失败时创建通知。尝试添加以下内容: def after_all(context): if context.failed == True: print('Why does none of this work?') print('stdout_capture length %d' % context.stdout_capture.len) print('stdout value: %s' % contex

我正在尝试访问所有失败的消息,以便在测试失败时创建通知。尝试添加以下内容:

def after_all(context):
    if context.failed == True:
        print('Why does none of this work?')
        print('stdout_capture length %d' % context.stdout_capture.len)
        print('stdout value: %s' % context.stdout_capture.getvalue())
        print('stderr_capture length %d' % context.stderr_capture.len)
        print('stderr value: %s' % context.stderr_capture.getvalue())
        print('context.log_capture.buffer: %s' % context.log_capture.buffer)
        for item in context.log_capture.buffer:
            print('this is a log message: %s' % item.getMessage())

def after_feature(context, feature):
    if context.feature.status == 'failed':
        logging.warning( context.feature.name)
并将断言设置为失败,但最后的输出总是:

stdout_capture length 0
stdout value: 
stderr_capture length 0
stderr value: 
context.log_capture.buffer: []

真的觉得应该有一个简单的方法来获得一个功能或场景列表,这些功能或场景无法添加到电子邮件或webhook中,从而使其更具描述性,但感觉我遗漏了一些东西。访问“事后处理”步骤中失败事件列表的最简单方法是什么?

Behave捕获测试运行期间发生的所有异常,以便在给定场景失败后仍能继续运行,这意味着永远不会真正抛出异常。您可以在类
步骤中的
behave\model
下的behave的源代码中查看它是如何工作的

其中一种方法(我在我的一个项目中就是这么做的)是使用monkey补丁来存储异常上下文

def store_exception_context(self, exception):
    self.exception = exception
    self.exc_traceback = sys.exc_info()[2]
就我个人而言,我使用了
before\u步骤
hook来实现这一点

def before_step(context, step):
    """This is executed before each step"""

    step.store_exception_context = store_exception_context_patch

另一种方法是通过Behave的运行选项以任何给定的输出格式(如json)生成结果,然后在测试运行后对结果输出进行后期处理。

Behave捕获测试运行期间发生的所有异常,为了能够在给定场景失败后继续运行,这意味着永远不会真正抛出异常。您可以在类
步骤中的
behave\model
下的behave的源代码中查看它是如何工作的

其中一种方法(我在我的一个项目中就是这么做的)是使用monkey补丁来存储异常上下文

def store_exception_context(self, exception):
    self.exception = exception
    self.exc_traceback = sys.exc_info()[2]
就我个人而言,我使用了
before\u步骤
hook来实现这一点

def before_step(context, step):
    """This is executed before each step"""

    step.store_exception_context = store_exception_context_patch

另一种方法是通过Behave的运行选项以任何给定的输出格式(如json)生成结果,然后在测试运行后对结果输出进行后期处理。

如果解释了如何访问,
step.store\u exception\u context
在after\u all函数中。我不记得我到底做了什么,这是从一个旧项目中做的,但您想在这里做的是,如我的示例所示,在before all中替换调用store\u exception\u context\u补丁,然后,你自己替换它会将相关数据存储在其他地方,你可以在你的after\u allI中检索。如果它解释了你如何访问,
步骤,我会接受这个答案。在after\u all函数中存储例外上下文。我不记得我到底做了什么,这是从一个旧项目中获取的,但你想在这里做的是,正如在我的示例中,在before all中替换存储\u exception\u context\u补丁的调用,然后,您自己的替换将把相关数据存储在其他地方,您可以在after all中检索这些数据