如何重新使用相同的函数,但在同一测试脚本中从 python 中的不同位置调用它

How to re-use the same function but call it from different locations in python in same test script

我有一个测试用例,其中我必须从设备上的不同屏幕 "End the process" 并且我有一个模拟不同屏幕的功能。 在 EndProcess() 之后,设备 returns 到 screen1()。在 Python 中有什么乐观的方法可以做到这一点?我可以在这里使用发电机吗?

目前我的代码是:

while 1:
    screen1()
    EndProcess()
    screen1()
    screen2()
    EndProcess()
    screen1()
    screen2()
    screen3()
    EndProcess()

当屏幕数量变大时,你会重复很多次。相反,您可以将屏幕放在列表中并使用 for 循环调用它们:

screens = [screen1, screen2, screen3]

while True:
   for x in range(len(screens)):
      for i in range(x+1):
         screens[i]()
      EndProcess()

使用xrange代替Python中的range 2.