【问题标题】:improving performance of behave tests提高行为测试的性能
【发布时间】:2018-11-18 15:19:51
【问题描述】:

我们在管道中运行行为 BDD 测试。作为 jenkins 管道的一部分,我们在 docker 容器中运行测试。目前运行所有测试大约需要 10 分钟。我们正在添加大量测试,几个月后,它可能会长达 30 分钟。它正在输出大量信息。我相信,如果我减少它输出的信息量,我可以让测试运行得更快。有没有办法控制信息行为输出的数量?我只想在出现故障时打印信息。 我看了一下行为并行。看起来它在python 2.7中。我们在python3。 我正在查看各种选项的行为提供。

behave -verbose=false folderName (I assumed that it will not output all the steps) behave --logging-level=ERROR TQXYQ (I assumed it will print only if there is an error) behave --logging-filter="Test Step" TQXYQ (I assumed it will print only the tests that has "Test Step" in it) 以上都不起作用。

当前的输出是这样的

Scenario Outline: IsError is populated correctly based on Test Id -- @1.7 # TestName/Test.feature:187 Given the test file folder is set to /TestName/steps/ # common/common_steps.py:22 0.000s And Service is running # common/common_steps.py:10 0.000s Given request used is current.json # common/common_steps.py:26 0.000s And request is modified to set X to q of type str # common/common_steps.py:111 0.000s And request is modified to set Y to null of type str # common/common_steps.py:111 0.000s And request is modified to set Z to USD of type str # common/common_steps.py:111 0.000s
When make a modification request # common/common_steps.py:37 0.203s Then it returns 200 status code # common/common_steps.py:47 0.000s And transformed result has IsError with 0 of type int # common/common_steps.py:92 0.000s And transformed result has ErrorMessages contain [] # common/common_steps.py:52 0.000s

我只想在出现错误时打印所有这些内容。如果一切顺利,我不想显示此信息。

【问题讨论】:

  • 从输出中可以看出延迟来自请求,而所有其他步骤在一毫秒内完成。所以,不要指望如果减少输出,速度会显着提高。
  • 谢谢克劳斯。我还可以在单​​独的容器中运行测试,看看它是否减少了时间。

标签: python bdd python-behave


【解决方案1】:

我认为默认的日志级别 INFO 不会影响您的测试性能。
我也在使用 docker 容器来运行回归套件,运行 2300 个测试场景大约需要 2 个小时。我花了将近一天的时间,这就是我所做的:
1。并行运行所有测试套件
这是减少执行时间的最重要原因。
我们花了很多精力将回归套件变成可并行的。
- 进行原子、自主和独立的测试,以便您可以有效地并行运行所有测试。
- 创建一个并行运行程序以在多个进程上运行测试。我正在使用多处理和子处理库来执行此操作。
我不推荐行为并行,因为它不再受支持。
您可以参考这个链接:
http://blog.crevise.com/2018/02/executing-parallel-tests-using-behave.html?m=1
- 使用 Docker Swarm 向 Selenium Grid 添加更多节点。
您可以向上扩展以添加更多节点,最大节点数取决于 cpu 的数量。最佳实践是节点数 = cpu 数。
我有 4 台 PC,每台都有 4 个内核,因此我可以扩展到 1 个集线器和 15 个节点。

2。优化框架中的同步。
删除 time.sleep()
删除隐式等待。请改用显式等待。

希望对您有所帮助。

【讨论】:

  • 嗨 Thach Hoang。感谢您的答复。我开始在多个容器中运行相同的测试以检查它是否工作正常。不幸的是,容器在并行运行时会变慢。我们有 2 个服务(一个用于运行测试,另一个用于要测试的实际服务)。我有 4 个容器(每个服务 2 个容器)。第一个测试容器与第一个服务对话。第二个测试容器与第二个服务对话。它们并行运行。你遇到过这个吗?我读到了,因为容器共享相同的内核空间,这有可能发生。
  • 嗨@Bala,我假设您在同一个 docker 主机上有 4 个容器。 2 个用于测试的容器,另一个用于 AUT。在这种情况下,您需要使用用户定义的桥接网络为每个应用程序创建 2 个单独的网络。 $ docker network create --driver bridge custom-net1 。第一个测试容器和第一个应用程序容器将分配给这个网络。第二个测试容器和第二个应用程序容器需​​要分配在不同的网络中,如 custom-net2。所以test1只能到达app1,test2只能到达app2。希望对您有所帮助。
  • 另外,请确保在运行所有容器时您的 PC 性能仍然良好,检查您的 CPU 百分比。如果您有其他可用的 PC,我建议使用 Docker Swarm 获取更多资源。
  • 非常感谢 Thach。那讲得通。我错过了我们需要创建不同网络的部分。我会这样做并让你知道。再次感谢。
【解决方案2】:

好吧,我已经用传统方式解决了这个问题,但我不确定它的效果如何。我昨天才开始做这个,现在正试图用它来构建报告。方法如下,欢迎提出建议

这也解决了驱动示例中的并行执行问题。

parallel_behave.py 运行命令(模仿行为命令的所有参数) py parallel_behave.py -t -d -f ......

initial_command = 'behave -d -t <tags>'
'''
the above command returns the eligible cases. may not be the right approach,                  but works well for me
'''

r = subprocess.Popen(initial_command.split(' '), stdout=subprocess.PIPE, bufsize=0)
finalsclist = []

_tmpstr=''
for out in r.stdout:
    out = out.decode('utf-8')
    # print(out.decode('utf-8'))
    if shellout.startswith('[') :
        _tmpstr+=out
    if shellout.startswith('{') :
        _tmpstr+=out
    if shellout.startswith(']'):
        _tmpstr+=out
        break

scenarionamedt = json.loads(_tmpstr)

for sc in scenarionamedt:
    [finalsclist.append(s['name']) for s in sc['elements']]

now the finalsclist contains the scenario name
ts = int(timestamp.timestamp)
def foo:
   cmd = "behave -n '{}' -o ./report/output{}.json".format(scenarioname,ts)

pool = Pool(<derive based on the power of the processor>) 
pool.map(foo, finalsclist)

这将创建多个单独的行为调用进程并在报告文件夹下生成 json 输出

*** 有来自https://github.com/hugeinc/behave-parallel 的引用,但这是在功能级别。我只是将它扩展到场景和示例****

【讨论】:

    猜你喜欢
    • 2018-10-17
    • 1970-01-01
    • 2020-07-19
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多