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