所以我正在练习一些单元测试,我有一个关于错误消息的问题。我正在尝试创建一条自定义错误消息,该消息将在测试失败时显示。这是一个基本的 Hello World 程序。测试运行良好,一切正常,但这是我收到的错误消息。
F ====================================================================== FAIL: test_StringContains
(__main__.TestSuite) ----------------------------------------------------------------------
Traceback (most recent call last): File "Testsuite.py", line 8, in test_StringContains
self.assertEqual("Hello, World", hello_world,"This is a custom error") AssertionError: 'Hello,
World' != 'Hello World' - Hello, World ? - + Hello World : This is a custom error ---------------
-------------------------------------------------------
Ran 1 test in 0.000s FAILED (failures=1)
所以这是预料之中的。这是我的测试套件
from HelloWorld import*
import unittest
class TestSuite(unittest.TestCase):
def test_StringContains(self):
self.assertEqual("Hello World", hello_world,"This is a custom")
if __name__ == "__main__":
unittest.main()
这是我的运行代码
hello_world = "Hello World"
print (hello_world)
超级基础,只想了解如何抛出自定义错误消息。我唯一想知道测试是否失败的是This is a custom error
,我尝试按照此处有关错误和异常的 Python 文档进行操作https://docs.python.org/2/tutorial/errors.html https://docs.python.org/2/tutorial/errors.html,但不知道如何实现。
我不知道这是否可行。任何帮助是极大的赞赏。谢谢