是的,我知道你不应该向unittest发送参数,但是在这种情况下,我必须让它能够访问当前框架中的一部分硬件。由于细节不相关的原因,我不能在测试中使用一个简单的board = myBoard.getBoard()
方法,否则我不会在这里询问所有人。在
class Test(object):
@staticmethod
def __call__(board):
_board = board
global _board
logging.info('Entering a Unit Test')
suite = unittest.TestLoader()
suite = suite.loadTestsFromTestCase(TestCase)
unittest.TextTestRunner().run(suite)
将调用此方法在某个时刻执行单元测试。这就是当前框架中的设置方式,我只是遵循规则。在
你可以看到我在这里分配了一个全局变量(可怕)。然后可以通过测试方法轻松访问:
^{pr2}$结果是它不会进入runTest
,但会在每个test_
方法之前setUp
。我很喜欢,我可以不输入runTest
来处理它
我想执行上述操作,但没有全局变量,所以我尝试了以下方法:
class Test(object):
@staticmethod
def __call__(board):
logging.info('Entering a basic Unit Test')
suite = unittest.TestSuite() #creates a suite for testing
suite.addTest(BasicCase(board))
unittest.TextTestRunner().run(suite)
class BasicCase(unittest.TestCase):
def __init__(self, board):
self.board = board
logging.critical('init')
super(BasicCase, self).__init__()
def runTest(self):
logging.critical('runtest')
def setUp(self):
logging.critical('reset')
infra.Reset.__call__(self.board) #allows me to pass the board in a much nicer way
def test_Connect(self):
logging.critical('connection')
上面的问题是它显然只做init
、setUp
和runTest
,但它将错过任何{
如果我加上:
suite.addTest(BasicCase(board).test_Connect)
然后它不必要地调用init
两次,我必须向test_Connect
添加一个额外的成员变量来接受test
对象。我也可以处理这个问题,因为我可以返回一个test_
方法的列表,但这样做似乎很痛苦。另外,我不认为它每次进入一个新的test_
方法时都会运行setUp
。不太理想。在
那么,有没有其他方法可以让我像unittest一样快速运行所有东西,但是允许我传递board
对象?在
如果我应该使用不同的python模块,那么请说,但是我使用的是unittest
,因为我得到了可爱的输出和assert
方法。在
目前没有回答
相关问题 更多 >
编程相关推荐