我想编写一个函数,当它失败时引发错误,然后将其传递给unittest。请考虑以下事项:
class undefinedTerms(unittest.TestCase):
def setUp(self):
self.A = frozenset((1,2,3,2,1,4,2,5,6,3,5,7,1,5,2,4,8))
self.B = frozenset((1,4,5,6,3,4,2,5,4,3,1,3,4,2,5,3,6,7,4,2,3,1))
self.C = frozenset((1,2,3,2,1,4,2,5,6,3,5,7,1,5,2,4))
self.D = (1,2,1)
def is_a_set(self,set_this):
try:
assert isinstance(set_this, (set,frozenset))
except TypeError:
raise TypeError("The object you passed is not a set.")
return True
def test_A_is_a_set(self):
self.assertTrue(self.is_a_set(self.A))
def test_B_is_a_set(self):
self.assertTrue(self.is_a_set(self.B))
def test_C_is_a_set(self):
self.assertTrue(self.is_a_set(self.C))
def test_D_is_a_set(self):
self.assertTrue(self.is_a_set(self.D), self.is_a_set(self.D))
suite = loader.loadTestsFromTestCase(undefinedTerms)
unittest.TextTestRunner(verbosity=2).run(suite)
这给出了以下输出。
test_A_is_a_set (__main__.undefinedTerms) ... ok
test_B_is_a_set (__main__.undefinedTerms) ... ok
test_C_is_a_set (__main__.undefinedTerms) ... ok
test_D_is_a_set (__main__.undefinedTerms) ... FAIL
======================================================================
FAIL: test_D_is_a_set (__main__.undefinedTerms)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<ipython-input-33-4751f8653e7a>", line 27, in test_D_is_a_set
self.assertTrue(self.is_a_set(self.D), self.is_a_set(self.D))
File "<ipython-input-33-4751f8653e7a>", line 12, in is_a_set
(type(set_this) is set))
AssertionError
----------------------------------------------------------------------
Ran 4 tests in 0.001s
FAILED (failures=1)
我想要的是AssertionError
函数中定义的TypeError
。我对完全不同的实现持开放态度。
我想我不清楚我到底在追求什么。阅读评论和答案后,我相信我想要的是创建一个自定义断言。
此前已经解决了here。
答案 0 :(得分:1)
def assert_is_set(self, set_this):
self.assertIsInstance(set_this, (set, frozenset))
此时,您并不真正需要一个功能,只需内联支票即可。
如果条件变得更复杂,您还可以使用更通用的.fail()
方法。
无条件地发出测试失败信号,错误消息为msg或None
if something:
self.fail("Computer broken!")
文档页面在TestCases中有list of all the assertions available
答案 1 :(得分:0)
正如提议here,我相信我所追求的是自定义断言方法。这是我最后的实施。
class SetAssertions:
def assertIsASet(self, set_this):
if not isinstance(set_this, (set,frozenset)):
raise AssertionError("The object you passed is not a set.")
import unittest
class undefinedTerms(unittest.TestCase, SetAssertions):
def setUp(self):
self.A = frozenset((1,2,3,2,1,4,2,5,6,3,5,7,1,5,2,4,8))
self.B = frozenset((1,4,5,6,3,4,2,5,4,3,1,3,4,2,5,3,6,7,4,2,3,1))
self.C = frozenset((1,2,3,2,1,4,2,5,6,3,5,7,1,5,2,4))
self.D = (1,2,1)
def test_A_is_a_set(self):
self.assertIsASet(self.A)
def test_B_is_a_set(self):
self.assertIsASet(self.B)
def test_C_is_a_set(self):
self.assertIsASet(self.C)
def test_D_is_a_set(self):
self.assertIsASet(self.D)
suite = loader.loadTestsFromTestCase(undefinedTerms)
unittest.TextTestRunner(verbosity=2).run(suite)
test_A_is_a_set (__main__.undefinedTerms) ... ok
test_B_is_a_set (__main__.undefinedTerms) ... ok
test_C_is_a_set (__main__.undefinedTerms) ... ok
test_D_is_a_set (__main__.undefinedTerms) ... FAIL
======================================================================
FAIL: test_D_is_a_set (__main__.undefinedTerms)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<ipython-input-39-495718fb2bad>", line 32, in test_D_is_a_set
self.assertIsASet(self.D)
File "<ipython-input-39-495718fb2bad>", line 6, in assertIsASet
raise AssertionError("The object you passed is not a set.")
AssertionError: The object you passed is not a set.
----------------------------------------------------------------------
Ran 4 tests in 0.002s
FAILED (failures=1)