pytest之钩子函数中的参数call


1、参数call是类CallInfo类的对象,理解为测试用例的执行结果(result)或失败(exception)收集在call参数中

@attr.s(repr=False)
class CallInfo:
    """ Result/Exception info a function invocation. """

    _result = attr.ib()
    # Optional[ExceptionInfo]
    excinfo = attr.ib()
    start = attr.ib()
    stop = attr.ib()
    when = attr.ib()

    @property
    def result(self):
        if self.excinfo is not None:
            raise AttributeError("{!r} has no valid result".format(self))
        return self._result

    @classmethod
    def from_call(cls, func, when, reraise=None):
        #: context of invocation: one of "setup", "call",
        #: "teardown", "memocollect"
        start = time()
        excinfo = None
        try:
            result = func()
        except:  # noqa
            excinfo = ExceptionInfo.from_current()
            if reraise is not None and excinfo.errisinstance(reraise):
                raise
            result = None
        stop = time()
        return cls(start=start, stop=stop, when=when, result=result, excinfo=excinfo)

    def __repr__(self):
        if self.excinfo is not None:
            status = "exception"
            value = self.excinfo.value
        else:
            # TODO: investigate unification
            value = repr(self._result)
            status = "result"
        return "<CallInfo when={when!r} {status}: {value}>".format(
            when=self.when, value=value, status=status
        )

2、call有五个属性:call.result指测试用例的每个阶段的执行结果,一般为空列表,不知道原因

      call.when指测试用例的执行阶段,setup、call、teardown

      call.start指每个阶段的开始执行时间

      call.stop指每个阶段的结束执行时间

      call.excinfo指每个阶段如果执行失败,则显示这个阶段的异常信息,它是一个ExceptionInfo类的对象(_pytest._code.code.py.ExceptionInfo)

例子:在测试用例call阶段执行失败,可以看到call.excinfo的属性值为<ExceptionInfo NameError tblen=18>,指出了异常的测试用例的行数和异常类型

注意:如果测试用例失败了(任何阶段), 调用call.result会报属性不存在的错误,详见请看源码_pytest.runner.py.CallIfo的result()方法

testcase/test_getRegionCountry/test_GetRegionCountry.py::test_getRightrequest <CallInfo when='setup' result: []>
setup
[]
1585986048.5431032
1585986048.5431032
<CallInfo when='call' exception: call阶段>
call
<ExceptionInfo NameError tblen=18>
1585986048.5431032
1585986049.7831051
FAILED<CallInfo when='teardown' result: []>
teardown
[]
1585986050.2931058
1585986050.3031058

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM