赞
踩
Is this acceptable or am I making heavy rookie mistake?
是和否.
这是如何编写单元测试的一个很好的例子.
但这是一个用例,首先不应该存在于您的代码中.
class FooBar(object):
def take_a_string_and_do_something(self, param):
# process nonempty string
# If they couldn't provide a non-empty string, they get an exception.
你仍然可以像这样测试它.
class TestFooBar(unittest.TestCase):
def setUp( self ):
self.foo= FooBar()
def test_zero_length_should_fail(self):
self.failUnlessRaises(IndexError, foo.take_a_string_and_do_something, '')
def test_none_should_fail(self):
self.failUnlessRaises(TypeError, foo.take_a_string_and_do_something, None)
def test_non_string_should_fail(self):
self.failUnlessRaises(TypeError, foo.take_a_string_and_do_something, 234)
请注意,它更简单,也更可靠,因为您没有尝试复制Python广泛的内部错误检查.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。