在 Python 中使用单元测试断言后打印
Printing after assert with unittesting in Python
我正在尝试打印 运行 我的单元测试时出现异常的项目。例如:
def test_string(self):
my_str = "new string"
self.assertTrue(len(my_str) == 5)
这是一个非常简化的示例,但它显示了我正在努力实现的目标。在这种情况下,我希望测试在得到 'F'.
后打印“新字符串”
在常规情况下 assert
我可以通过以下方式实现:
assert (len(my_str) == 5), "new string"
但是使用unittest
就不行了。
有没有办法用 unittest
做到这一点?
您可以使用
self.assertTrue(len(str) == 5, msg="new string")
https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertTrue
我正在尝试打印 运行 我的单元测试时出现异常的项目。例如:
def test_string(self):
my_str = "new string"
self.assertTrue(len(my_str) == 5)
这是一个非常简化的示例,但它显示了我正在努力实现的目标。在这种情况下,我希望测试在得到 'F'.
后打印“新字符串”在常规情况下 assert
我可以通过以下方式实现:
assert (len(my_str) == 5), "new string"
但是使用unittest
就不行了。
有没有办法用 unittest
做到这一点?
您可以使用
self.assertTrue(len(str) == 5, msg="new string")
https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertTrue