我需要通过ssh在Python上测试某些内容。我不想为每个测试建立ssh连接,因为这太费时间了,所以我写了这个:
class TestCase(unittest.TestCase):
client = None
def setUp(self):
if not hasattr(self.__class__, 'client') or self.__class__.client is None:
self.__class__.client = paramiko.SSHClient()
self.__class__.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.__class__.client.connect(hostname=consts.get_host(), port=consts.get_port(), username=consts.get_user(),
password=consts.get_password())
def test_a(self):
pass
def test_b(self):
pass
def test_c(self):
pass
def disconnect(self):
self.__class__.client.close()
和我的赛跑者
if __name__ == '__main__':
suite = unittest.TestSuite((
unittest.makeSuite(TestCase),
))
result = unittest.TextTestRunner().run(suite)
TestCase.disconnect()
sys.exit(not result.wasSuccessful())
在这个版本中,我遇到了错误 TypeError: unbound method disconnect() must be called with TestCase instance as first argument (got nothing instead)
。那么在所有测试通过后,我该如何调用disconnect呢?
敬礼。
@classmethod
修饰符。 - Løiten