如何调用函数并更改变量

2024-09-29 23:28:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在测试多个响应时,我多次调用函数。我问的是如何在程序的早期调用一个函数,在这个函数中更改一个变量,然后调用它。下面是一段代码。在

class AbsoluteMove(unittest.TestCase):

     def Ssh(self):

        p=pexpect.spawn('ssh user@10.10.10.10')
        self.command = './ptzpanposition -c 0 -u degx10' 
        p.sendline("cd /bin")
        i=p.expect('user@user-NA:')
        p.sendline(self.command)
        i=p.expect('user@-userNA:')
        self.Value = p.before

class VerifyTilt(AbsoluteMove):

     def runTest(self):

         self.dest.PanTilt._y=2.0

         try:
            result = self.client.service.AbsoluteMove(self.token, self.dest, self.speed)
         except suds.WebFault as detail:
             print detail

         self.command = './ptzpanposition -c 0 -u degx10'
         AbsoluteMove.Ssh(self)

         # Position of the camera verified through Ssh (No decimal point added to the Ssh value)
         self.assertEqual(self.Value, '20')

我想换个自我命令'变量输入绝对移动.Ssh(),然后运行此函数。有人知道怎么做吗?在

谢谢你的帮助


Tags: 函数selfvaluedefsshcommandclassdest
3条回答

能不能给绝对移动增加一个参数?在

看起来是这样的自我命令只是一根线,而且绝对移动.Ssh()不带参数,它必须以某种方式使用该字符串…,因此您可以只更改自我命令. 一个更好的设计应该有两个命令和一个绝对移动.Ssh()在他们之间做出选择。在

您需要有一个runTest函数的包装器。注意,我在runTest中注释了self.command='./ptzpanposition -c 0 -u degx10'

class VerifyTilt(AbsoluteMove):
 def testWrapper(self):
     self.command = './ptzpanposition -c 0 -u degx10'
     runTest()
     self.command = 'some other command'
     runTest() 

 def runTest(self):

     self.dest.PanTilt._y=2

     try:
        result = self.client.service.AbsoluteMove(self.token, self.dest, self.speed)
     except suds.WebFault as detail:
         print detail

     # self.command = './ptzpanposition -c 0 -u degx10'
     AbsoluteMove.Ssh(self)

     # Position of the camera verified through Ssh (No decimal point added to the Ssh value)
     self.assertEqual(self.Value, '200')

相关问题 更多 >

    热门问题