【发布时间】:2019-04-19 00:44:59
【问题描述】:
我已经使用了很多 python unittest,并且有一个装饰器方法可以用来有条件地跳过这样的测试:
import unittest
class TestStringMethods(unittest.TestCase):
@unittest.skipIf(1 == 1, 'Skipped because 1 does indeed equal 1')
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
我想为 unittest.TestCase 类添加一些功能,所以我将其子类化以开始添加我自己的自定义代码:
import unittest
class CustomTestCase(unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) # Just use whatever is in TestCase's init + our stuff
self.foo = 'foo' # our stuff
self.bar = 'bar'
def mymethod(self, param1, param2): # Some custom method I wanted to make for these custom test cases
pass
要继续使用@unittest.skipIf,我一直在我的任何CustomTestCase 测试文件的顶部粘贴import unittest,但我想知道这是否是正确的做事方式。我进口的是否超出了我的需要?还是我什么都不担心?
【问题讨论】: