【问题标题】:How to re-use the same function but call it from different locations in python in same test script如何重用相同的函数,但在同一测试脚本中从 python 中的不同位置调用它
【发布时间】:2017-08-11 13:16:24
【问题描述】:

我有一个测试用例,我必须从设备上的不同屏幕“结束进程”,并且我有一个模拟不同屏幕的功能。 在 EndProcess() 之后,设备返回到 screen1()。在 Python 中有什么乐观的方法可以做到这一点?我可以在这里使用生成器吗?

目前,我的代码是:

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

【问题讨论】:

    标签: python function generator reusability


    【解决方案1】:

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

    screens = [screen1, screen2, screen3]
    
    while True:
       for x in range(len(screens)):
          for i in range(x+1):
             screens[i]()
          EndProcess()
    

    在 Python 2 中使用 xrange 代替 range

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      相关资源
      最近更新 更多