理論:https://www.runoob.com/python/python-func-eval.htmlhtml
eval():用來執行一個字符串表達式,並返回表達式的值。python
在實際中的應用舉例:json
from api.alert_info_controller.api_analysisType import ApiAnalysisType class TestAnalysisType: def test_analysisType(self): res = ApiAnalysisType().analysisType() str = "res.json()['data'] is not None" assert res.status_code == 200 assert eval(str) # 此時會返回True或者False
可看到該測試用例的執行結果爲pass
也能夠在數據驅動中使用:api
class TestZone: @pytest.mark.parametrize("keyword,expected", [(None, "res.json() is not None"), ("鼓樓醫院", "not res.json() is None")]) def test_zoneForReport(self, keyword, expected): res = ApiZone().zoneForReport(keyword) assert eval(expected)