【发布时间】:2013-04-25 17:24:38
【问题描述】:
假设我在本地目录中有 2 个测试套件 foo 和 bar,并且我想按照 foo 然后 bar 的顺序运行测试套件。
我尝试运行 pybot -s foo -s bar .,但它只是运行 bar 然后 foo(即按字母顺序)。
有没有办法让 pybot 运行机器人框架套件以按照我定义的顺序执行?
【问题讨论】:
假设我在本地目录中有 2 个测试套件 foo 和 bar,并且我想按照 foo 然后 bar 的顺序运行测试套件。
我尝试运行 pybot -s foo -s bar .,但它只是运行 bar 然后 foo(即按字母顺序)。
有没有办法让 pybot 运行机器人框架套件以按照我定义的顺序执行?
【问题讨论】:
机器人框架可以使用参数文件来指定执行顺序(docs):
这是来自旧文档(不再在线):
参数文件的另一个重要用途是以特定顺序指定输入文件或目录。如果不适合按字母顺序排列的默认执行顺序,这将非常有用:
基本上,您创建的内容类似于启动脚本。
--name My Example Tests
tests/some_tests.html
tests/second.html
tests/more/tests.html
tests/more/another.html
tests/even_more_tests.html
参数文件有一个简洁的功能,你可以调用另一个参数文件,它可以覆盖以前设置的参数。执行是递归的,因此您可以根据需要嵌套任意数量的参数文件
另一种选择是使用启动脚本。比您必须处理其他方面,例如您在哪个操作系统上运行测试。您还可以使用 python 在多个平台上启动脚本。 docs这部分还有更多内容
【讨论】:
如果RF目录中有多个测试用例文件,可以通过给测试用例名称加上数字作为前缀来指定执行顺序,像这样。
01__my_suite.html -> 我的套房 02__another_suite.html -> 另一个套件
如果这些前缀与套件的基本名称用两个下划线分开,则不会包含在生成的测试套件名称中:
更多细节在这里。
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#execution-order
【讨论】:
您可以使用tagging。
将测试标记为 foo 和 bar,以便您可以分别运行每个测试:
pybot -i foo tests
或
pybot -i bar tests
并决定顺序
pybot -i bar tests || pybot -i foo tests
或在脚本中。
缺点是您必须为每个测试运行设置。
【讨论】:
这样的东西有用吗?
pybot tests/test1.txt tests/test2.txt
所以,要反转:
pybot tests/test2.txt tests/test1.txt
【讨论】:
__init__.txt。如果我以这种方式运行它,它不会接收到__init__.txt
我成功使用了监听器:
Listener.py:
class Listener(object):
ROBOT_LISTENER_API_VERSION = 3
def __init__(self):
self.priorities = ['foo', 'bar']
def start_suite(self, data, suite):
#data.suites is a list of <TestSuite> instances
data.suites = self.rearrange(data.suites)
def rearrange(self, suites=[]):
#Do some sorting of suites based on self.priorities e.g. using bubblesort
n = len(suites)
if n > 1:
for i in range(0, n):
for j in range(0, n-i-1):
#Initialize the compared suites with lowest priority
priorityA = 0
priorityB = 0
#If suite[j] is prioritized, get the priority of it
if str(suites[j]) in self.priorities:
priorityA = len(self.priorities)-self.priorities.index(str(suites[j]))
#If suite[j+1] is prioritized, get the priority of it
if str(suites[j+1]) in self.priorities:
priorityB = len(self.priorities)-self.priorities.index(str(suites[j+1]))
#Compare and swap if suite[j] is of lower priority than suite[j+1]
if priorityA < priorityB:
suites[j], suites[j+1] = suites[j+1], suites[j]
return arr
假设 foo.robot 和 bar.robot 包含在名为“tests”的顶级套件中,您可以像这样运行它:
pybot --listener Listener.py tests/
这将即时重新排列子套件。您可以使用 prerunmodifier 预先修改它。
【讨论】: