python - 如何检测 pytest 测试用例何时出现 AssertionError?

标签 python python-2.7 testing pytest

我正在使用 pytest 来自动化项目测试。我想仅在测试用例失败时才采取一些独特的操作,例如“save_snapshot()”。

我们在 pytest 中有类似的东西吗?

我尝试使用teardown_method() 来实现此目的,但是当测试用例失败时,该方法不会被执行。

  • 请勿使用固定装置。

最佳答案

我通过对类中的每个测试使用 python 装饰器找到了解决此问题的方法:

def is_failed_decorator(func):
    def wrapper(*args, **kwargs):
        try:
            func(*args, **kwargs)
        except AssertionError:
            cls_obj = args[0]
            cur_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
            func_name = func.__name__
            # Save_snapshot().
            raise
    return wrapper

# Tests class
@is_failed_decorator
def test_fail(self):
    assert False

为我工作:D

关于python - 如何检测 pytest 测试用例何时出现 AssertionError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38995522/

相关文章:

Python: Mechanize select_form 不返回任何内容

python - 组合回归模型中虚拟变量的影响

python - 匹配两个包含字符串的列表

python - 将二进制格式的字符串(带有前导零)转换为整数并再次转换回来

python - 如何使用 PyXll 将 Excel 单元格添加到 Python 字典

unit-testing - 如何使用 Testng 并行运行测试?

python - 如何将两个向量相乘并得到一个矩阵?

python - Tensorflow embedding_lookup

c# - 测试远程客户端-服务器应用程序

testing - 使用 PMD 规则