简体   繁体   English

Python、API 测试:断言失败,但实际和预期似乎相同

[英]Python, API testing: assertion failed, but Actual and Expected seems to be same

Im trying to run simple negative api test in Pycharm.我试图在 Pycharm 中运行简单的负 api 测试。 It compares json response of api call with "expected" data.它将 api 调用的 json 响应与“预期”数据进行比较。 Call returns this:调用返回:

{'error_message': 'invalid UUID format'}

And in our test we asserting actual response with ^ data.在我们的测试中,我们用 ^ 数据断言实际响应。 Test:测试:

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

When running this test with pytest in pycharm, it returns在 pycharm 中使用 pytest 运行此测试时,它返回

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

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

Please point me in right direction.请指出我正确的方向。 Thanks in advance提前致谢

As pointed out in a comment, your test should compare jsonResponse (which is a dict ) with another dict , and not a string:正如评论中指出的那样,您的测试应该将jsonResponse (它是一个dict )与另一个dict进行比较,而不是一个字符串:

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

The test output is not very friendly in this case, as it makes you think that both objects are identical.在这种情况下,测试 output 不是很友好,因为它会让您认为两个对象是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM