【问题标题】:Python, API testing: assertion failed, but Actual and Expected seems to be samePython,API 测试:断言失败,但实际和预期似乎相同
【发布时间】:2020-06-18 13:09:32
【问题描述】:

我试图在 Pycharm 中运行简单的否定 api 测试。 它将 api 调用的 json 响应与“预期”数据进行比较。 调用返回:

{'error_message': 'invalid UUID format'}

在我们的测试中,我们用 ^ 数据断言实际响应。 测试:

def test(environment):
resp = getSomeData(environment, negative)
respString = resp.read().decode('utf-8')
jsonResponse = json.loads(respString)
assert jsonResponse == "{'error_message': 'invalid UUID format'}"

在 pycharm 中使用 pytest 运行此测试时,它会返回

{'error_message': 'invalid UUID format'} != {'error_message': 'invalid UUID format'}

    Expected :{'error_message': 'invalid UUID format'}
    Actual   :{'error_message': 'invalid UUID format'}

请指出正确的方向。提前致谢

【问题讨论】:

  • 您将dictstr 进行比较。

标签: python json python-3.x api pycharm


【解决方案1】:

正如评论中所指出的,您的测试应该将jsonResponse(这是一个dict)与另一个dict进行比较,而不是一个字符串:

assert jsonResponse == {'error_message': 'invalid UUID format'}

在这种情况下,测试输出不是很友好,因为它会让你认为两个对象是相同的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2021-07-29
    • 2013-09-07
    • 2021-07-03
    • 2022-01-23
    • 2012-08-17
    相关资源
    最近更新 更多