robotframework 多進程 多線程

robotframework 多進程 多線程

一直想找個多進程執行rf case 的第三方庫,都沒找到。
這兩天又上網找了找,從網上的兩個回答裏找到了靈感。
我們可以間接的達到這個目的。

參考答案:
使用Process Library 多進程,後臺執行
https://stackoverflow.com/questions/25427506/robotframework-threads
http://robotframework.org/robotframework/latest/libraries/Process.html
RF 合併測試結果的工具 Rebot:
https://stackoverflow.com/questions/16519277/aggregate-the-reports-from-different-robot-testsuites

下面給出使用Process 的例子:
導入Process 庫
這裏寫圖片描述
使用多進程執行:
Start Process 代表後臺執行,不用等待進程執行完成
Run Process 執行線程,需要等待進程執行完成
這裏寫圖片描述

分析:
Start Process 執行的命令是,執行python 代碼,等待100s
但是我們執行Process 的時候,並沒有等待這個進程執行完
然後就執行了 Run Process 的進程,這個進程會打開計算器程序。
需要我們手動關閉計算機程序,Run Process 這個進程纔會結束。

發散思維:
既然我們可以使用 Start Process 後臺執行命令,那我們可以多起
幾個後臺進程,這個就相當於多進程執行了。
例如:

Start Process pybot -s suite1 -o xx/output1.xml D:/xxx/project
Start Process pybot -s suite2 -o xx/output2.xml D:/xxx/project
Start Process pybot -s suite3 -o xx/output3.xml D:/xxx/project
Start Process pybot -s suite4 -o xx/output4.xml D:/xxx/project

這裏就相當於後臺執行project 的4個測試套件,然後我們會得到四個output?.xml 輸出文件
OK, 這個時候我們合併輸出結果的命令就可以派上用場了
這個命令還有其他參數,詳細使用命令可以在cmd窗口使用命令查看:
rebot –help

python -m robot.rebot outputs/*.xml -r report/merge_report.html

最後得到的merge_report.html 就是我們合併後的最終測試報告
理論上可行,但還是實踐一下最好:
我們新建一個空文件夾
這裏寫圖片描述

再看看我們的case:
一共有3個case, 我們利用 process 去多進程執行 另外兩個case
並把兩個進程生成的測試結果合併生成一個測試報告
這裏寫圖片描述
圖太長截不全,上代碼:

start process   pybot.bat   -t  exec_script -o  D:/Document/outputs/opt1.xml    D:/Document/GitProject/auto-unix/unix.txt   alias=case1
start process   pybot.bat   -t  test_remote -o  D:/Document/outputs/opt2.xml    D:/Document/GitProject/auto-unix/unix.txt   alias=case2
wait for process    case1                       
wait for process    case2                       
Run Process python  -m  robot.rebot -d  D:/Document/outputs -R  D:/Document/outputs/*.xml

最後跑一下,看看我們的結果:
注意哦,裏面加了兩個進程的等待
我們要等所有進程結束,才能合併生成完整的測試報告
這裏寫圖片描述

最後看一下我們的報告:
這裏寫圖片描述
日誌:
這裏寫圖片描述

沒毛病~
那麼我們就可以實現多進程去並行執行case 了

發佈了80 篇原創文章 · 獲贊 38 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章