在django中运行单元测试时语法错误无效

问题描述 投票:0回答:1

我的示例django项目工作正常,我得到结果但问题是,每当我对我的项目运行单元测试时,我在test.py中得到如下语法错误。我已经尝试使用.format()仍然显示错误。为什么我不能使用f'this是我的文本'来格式化我的字符串?错误日志

ERROR: blog.tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: blog.tests
Traceback (most recent call last):
File "/usr/lib/python3.5/unittest/loader.py", line 428, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/lib/python3.5/unittest/loader.py", line 369, in
  _get_module_from_name
__import__(name)
File "/home/blog/tests.py", line 28
self.assertEqual(f'{self.post.title}', 'Content')
                                    ^
SyntaxError: invalid syntax
python django python-3.x unit-testing format-string
1个回答
4
投票

f-strings仅在Python 3.6+中有效。

然而,似乎没有任何理由在这里使用它们,因为你没有插入任何东西。为什么不只是self.assertEqual(self.post.title, 'Content')

© www.soinside.com 2019 - 2024. All rights reserved.