Python – Robot Framework + pabot

Loading

測試環境為 CentOS 8 x86_64 (虛擬機)

透過 Robot Framework 做自動化跟一般透過 Python 套件來做的差別在於不懂 Python 程式的人也可以使用,只要會編寫 Robot Framework 設定檔就可以跑自動化測試.

但是我們可以透過 Robot Framework 同時執行多個工作嗎? 是可以透過 pabot 的.

所需套件 (需先安裝 Python + pip – https://benjr.tw/104226) , 這邊除了 robotframework 與 robotframework-pabot 套件外,還需要 robotframework-sshlibrary 用於可以透過 SSH 連線進行測試.

[root@localhost ~]# pip install robotframework
[root@localhost ~]# pip install robotframework-pabot
[root@localhost ~]# pip install robotframework-sshlibrary

如想透過 SSH 對遠端 Linux Server 進行自動化測試,透過 pabot 執行方式有2種

Robot Framework --> Linux UUT1 (IP: 192.168.111.183)
                |
                --> Linux UUT2 (IP: 192.168.111.240)

方式1

這邊用到 robotframework-sshlibrary 可用於 SSH 連線進行測試,詳細說明請參考 – https://benjr.tw/105541

建立 pabot 專屬資料夾.

[root@localhost ~]# mkdir ssh
[root@localhost ~]# cd ssh/

編寫 Robot Framework 設定檔 1.

[root@localhost ~]# vi ssh1.robot
*** Settings ***
Documentation          This example demonstrates executing a command on a remote machine
...                    and getting its output.
...
...                    Notice how connections are handled as part of the suite setup and
...                    teardown. This saves some time when executing several test cases.

Library                SSHLibrary
Suite Setup            Open Connection And Log In
Suite Teardown         Close All Connections

*** Variables ***
${HOST}                192.168.111.183
${USERNAME}            root
${PASSWORD}            111111

*** Test Cases ***
Execute Command And Verify Output
    [Documentation]    Execute Command can be used to run commands on the remote machine.
    ...                The keyword returns the standard output by default.
    ${output}=         Execute Command    echo Hello SSHLibrary!
    Should Be Equal    ${output}          Hello SSHLibrary!

*** Keywords ***
Open Connection And Log In
   Open Connection     ${HOST}
   Login               ${USERNAME}        ${PASSWORD}

編寫 Robot Framework 設定檔 2 ( 與 設定檔 1 差別在於 Variables 的 ${HOST} ).

[root@localhost ~]# vi ssh2.robot
*** Settings ***
Documentation          This example demonstrates executing a command on a remote machine
...                    and getting its output.
...
...                    Notice how connections are handled as part of the suite setup and
...                    teardown. This saves some time when executing several test cases.

Library                SSHLibrary
Suite Setup            Open Connection And Log In
Suite Teardown         Close All Connections

*** Variables ***
${HOST}                192.168.111.240
${USERNAME}            root
${PASSWORD}            111111

*** Test Cases ***
Execute Command And Verify Output
    [Documentation]    Execute Command can be used to run commands on the remote machine.
    ...                The keyword returns the standard output by default.
    ${output}=         Execute Command    echo Hello SSHLibrary!
    Should Be Equal    ${output}          Hello SSHLibrary!

*** Keywords ***
Open Connection And Log In
   Open Connection     ${HOST}
   Login               ${USERNAME}        ${PASSWORD}

執行:
可以指定同時執行現有目錄下的 Robot Framework 檔案.

[root@localhost ssh]# pabot --pabotlib .
Storing .pabotsuitenames file
2023-03-24 11:14:18.223181 [PID:3428] [0] [ID:0] EXECUTING Ssh.Ssh1
2023-03-24 11:14:18.225012 [PID:3429] [1] [ID:1] EXECUTING Ssh.Ssh2
2023-03-24 11:14:20.045405 [PID:3428] [0] [ID:0] PASSED Ssh.Ssh1 in 1.8 seconds
2023-03-24 11:14:20.054723 [PID:3429] [1] [ID:1] PASSED Ssh.Ssh2 in 1.8 seconds
2 tests, 2 passed, 0 failed, 0 skipped.
===================================================
Output:  /root/ssh/output.xml
Log:     /root/ssh/log.html
Report:  /root/ssh/report.html
Total testing: 3.60 seconds
Elapsed time:  1.89 seconds

或是指定執行特定 Robot Framework 檔案名稱.

[root@localhost ssh]# pabot --pabotlib ssh*
Storing .pabotsuitenames file
2023-03-24 11:13:50.292695 [PID:3411] [0] [ID:0] EXECUTING Suites.Ssh1
2023-03-24 11:13:50.294414 [PID:3412] [1] [ID:1] EXECUTING Suites.Ssh2
2023-03-24 11:13:52.139951 [PID:3412] [1] [ID:1] PASSED Suites.Ssh2 in 1.8 seconds
2023-03-24 11:13:52.224778 [PID:3411] [0] [ID:0] PASSED Suites.Ssh1 in 1.9 seconds
2 tests, 2 passed, 0 failed, 0 skipped.
===================================================
Output:  /root/ssh/output.xml
Log:     /root/ssh/log.html
Report:  /root/ssh/report.html
Total testing: 3.70 seconds
Elapsed time:  1.98 seconds

方式2

另外一種方式就是把不同 UUT (Server) IP 等資訊寫在同一個檔案,再透過 pabot 的關鍵字 – https://pabot.org/PabotLib.html 去抓取.

[root@localhost ssh]# vi valueset.dat 
[Linux Server1]
tags=server1
HOST=192.168.111.183
USERNAME=root
PASSWORD=111111
  
[Linux Server2]
tags=server2
HOST=192.168.111.240
USERNAME=root
PASSWORD=111111

編寫 Robot Framework 設定檔 1.

[root@localhost ssh]# vi pabotssh1.robot 
*** Settings ***
Documentation          This example demonstrates executing a command on a remote machine
...                    and getting its output.
...
...                    Notice how connections are handled as part of the suite setup and
...                    teardown. This saves some time when executing several test cases.

Library                SSHLibrary
Library                pabot.PabotLib

*** Test Cases ***
Execute Command And Verify Output
    [Documentation]    Execute Command can be used to run commands on the remote machine.
    ...                The keyword returns the standard output by default.

    Acquire Lock   MyLock
    Log   This part is critical section
    Release Lock   MyLock
    ${valuesetname}=    Acquire Value Set  server1
    ${HOST}=   Get Value From Set   host
    ${USERNAME}=     Get Value From Set   username
    ${PASSWORD}=     Get Value From Set   password
    Log   Do something with the values (for example access host with username and password)
    Release Value Set
    Log   After value set release others can obtain the variable values

    Open Connection     ${HOST}
    Login               ${USERNAME}        ${PASSWORD}
    
    ${output}=         Execute Command    echo Hello SSHLibrary!
    Should Be Equal    ${output}          Hello SSHLibrary!

    Close Connection

主要的差別在於使用了 pabot 的關鍵字去抓取設定檔的 HOST , USERNAME 與 PASSWORD 資料.

    ${valuesetname}=    Acquire Value Set  server1
    ${HOST}=   Get Value From Set   host
    ${USERNAME}=     Get Value From Set   username
    ${PASSWORD}=     Get Value From Set   password

編寫 Robot Framework 設定檔 2 ( 與 設定檔 1 差別在於 Acquire Value Set 為 server2 )..

[root@localhost ssh]# vi pabotssh2.robot 
*** Settings ***
Documentation          This example demonstrates executing a command on a remote machine
...                    and getting its output.
...
...                    Notice how connections are handled as part of the suite setup and
...                    teardown. This saves some time when executing several test cases.

Library                SSHLibrary
Library                pabot.PabotLib

*** Test Cases ***
Execute Command And Verify Output
    [Documentation]    Execute Command can be used to run commands on the remote machine.
    ...                The keyword returns the standard output by default.

    Acquire Lock   MyLock
    Log   This part is critical section
    Release Lock   MyLock
    ${valuesetname}=    Acquire Value Set  server2
    ${HOST}=   Get Value From Set   host
    ${USERNAME}=     Get Value From Set   username
    ${PASSWORD}=     Get Value From Set   password
    Log   Do something with the values (for example access host with username and password)
    Release Value Set
    Log   After value set release others can obtain the variable values

    Open Connection     ${HOST}
    Login               ${USERNAME}        ${PASSWORD}
    
    ${output}=         Execute Command    echo Hello SSHLibrary!
    Should Be Equal    ${output}          Hello SSHLibrary!

    Close Connection

執行需指定設定檔案:

[root@localhost ssh]# pabot --pabotlib --resourcefile valueset.dat pabotssh*
Robot Framework remote server at 127.0.0.1:8270 started.
2023-03-24 12:09:59.886762 [PID:4187] [0] [ID:0] EXECUTING Suites.Pabotssh1
2023-03-24 12:09:59.890622 [PID:4188] [1] [ID:1] EXECUTING Suites.Pabotssh2
2023-03-24 12:10:01.737388 [PID:4188] [1] [ID:1] PASSED Suites.Pabotssh2 in 1.8 seconds
2023-03-24 12:10:01.923012 [PID:4187] [0] [ID:0] PASSED Suites.Pabotssh1 in 2.0 seconds
2 tests, 2 passed, 0 failed, 0 skipped.
===================================================
Output:  /root/ssh/output.xml
Log:     /root/ssh/log.html
Report:  /root/ssh/report.html
Stopping PabotLib process
Robot Framework remote server at 127.0.0.1:8270 stopped.
PabotLib process stopped
Total testing: 3.80 seconds
Elapsed time:  2.43 seconds
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料