Python 删除AssertDict8中等于8的空白
我正在修复代码,以便在PEP8中通过Flake8进行规范化。其中一个问题是我需要跳行来完成第8页的长行 这是我的报税表,我使用以下方法跳过该行:Python 删除AssertDict8中等于8的空白,python,django,python-unittest,flake8,Python,Django,Python Unittest,Flake8,我正在修复代码,以便在PEP8中通过Flake8进行规范化。其中一个问题是我需要跳行来完成第8页的长行 这是我的报税表,我使用以下方法跳过该行: expected_return = { "error": { "message": None, "type": "You are logged as admin. This end
expected_return = {
"error": {
"message": None,
"type": "You are logged as admin. This endpoint requires\
a customer or an anonymous user.",
"detail": None,
}
}
使用django.test中的将TestCase导入django,我的断言是:
self.assertDictEqual(self.deserialize(response.content),预期返回)
我的测试未通过,因为断言使用空格读取:
AssertionError: {u'error': {u'message': None, u'type': u'You are logged as admin. This endpoint [truncated]... != {u'error': {u'message': None, u'type': u'You are logged as admin. This endpoint [truncated]...
{u'error': {u'detail': None,
u'message': None,
- u'type': u'You are logged as admin. This endpoint requires a customer or an anonymous user.'}}
+ u'type': u'You are logged as admin. This endpoint requires a customer or an anonymous user.'}}
我也尝试使用self.assertmultiliequal,但出现了其他错误
根据PEP8,您通常使用什么解决方案跳过行,而不使用unittest接收此错误
致以最诚挚的问候。Python支持将字符串串联在一起,方法是让字符串显示在后面:
expected_return = {
"error": {
"message": None,
"type": "You are logged as admin. This endpoint requires "\
"a customer or an anonymous user.",
"detail": None,
}
}
最终结果是一个没有任何额外空格的串联字符串:
"You are logged as admin. This endpoint requires a customer or an anonymous user."
您可以使用括号缩小长文本,如下所示:
import unittest
def my_cool_function():
return {
"error": {
"message": None,
"type": "You are logged as admin. This endpoint requires a customer or an anonymous user.",
"detail": None,
}
}
class TestStringMethods(unittest.TestCase):
def test_flake8_length(self):
result = my_cool_function()
self.assertEqual(
result,
{
"error": {
"message": None,
"type": (
"You are "
"logged as admin. "
"This endpoint "
"requires a customer "
"or an anonymous user."
),
"detail": None,
}
}
)
if __name__ == '__main__':
unittest.main()
导入单元测试
定义my_cool_函数():
返回{
“错误”:{
“信息”:无,
“类型”:“您以管理员身份登录。此终结点需要客户或匿名用户。”,
“细节”:无,
}
}
类TestStringMethods(unittest.TestCase):
def测试片8长度(自身):
结果=my_cool_函数()
自我评价资格(
结果,,
{
“错误”:{
“信息”:无,
“类型”:(
“你是”
“以管理员身份登录。”
“此终结点”
“需要客户”
“或匿名用户。”
),
“细节”:无,
}
}
)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
unittest.main()
您可以在\
-“此端点需要”\
,然后在下一行“客户或..”
之前结束该行<代码>“foo”“bar”
是python中连接字符串的有效语法。感谢MatsLindh和JPG。两个答案都解决了我的问题。顺致敬意,