In the pytest documentation it says that you can customize the output message when an assert fails. I want to customize the assert message when testing a REST API method it returns an invalid status code:def test_api_call(self, client): response = client.get(reverse('api:my_api_call')) assert response.status_code == 200So I tried to put a piece of code like this in conftest.pydef pytest_assertrepr_compare(op, left, right): if isinstance(left, rest_framework.response.Response): return left.json()But the problem is left is the actual value of response.status_code so it's an int instead of a Response. However the default output messages throws something like:Saying that the error 400 comes from an attribute status_code of a object Response.My point is that there is a kind of introspection of the variable being evaluated. So, how can I customize the assert error message in a comfortable way to get a similar output to example posted above? 解决方案 you could use Python built-in capability to show custom exception message:assert response.status_code == 200, "My custom msg: actual status code {}".format(response.status_code)Or you can built a helper assert functions:def assert_status(response, status=200): # you can assert other status codes too assert response.status_code == status, \ "Expected {} actual status {}. Response text {}".format(status, response.status_code, response.text)# here is how you'd use itdef test_api_call(self, client): response = client.get(reverse('api:my_api_call')) assert_status(response)also checkout: https://wiki.python.org/moin/UsingAssertionsEffectively 这篇关于pytest使用可变的内省声明消息自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!