cli测试框架和用例

Settings区:

用于描述整个测试套件依赖文件,如下图所示

*** Settings ***
Documentation           通过cli查看bmc证书信息
Resource                ../../test_function/cli/ssh.robot
Suite Setup             Open SSH Connection
Suite Teardown          Close SSH Session    ${ssh}
  1. Documentation:用于描述测试套的用途。
  2. Resource:依赖文件路径,一般依赖文件里面放置可复用的关键字、定义变量和测试用例。
  3. Suite Setup:用于测试套件执行前初始话设置机制,每次执行都会执行一次,图中为连接ssh端口,确保环境的ssh端口是正常连接的。
Open SSH Connection
    [Documentation]    验证ssh连接是否正常
    ${ssh}=    Connect To BMC SSH
    Should Not Be Equal    ${ssh}    ${None}    msg=SSH连接失败
    Set Suite Variable    ${ssh}
  1. Suite Teardown:用于测试套件执行完毕后进行清理操作机制,与Suite Setup对应,图中为关闭通过SSH建立的远程服务器的连接会话。
Close SSH Session
    [Documentation]    关闭SSH连接
    [Arguments]    ${ssh}
    Close SSH Connection    ${ssh}

Variables区:

用于定义cli命令变量以及命令的返回预期值,如下图所示

*** Variables ***
${input}                ipmcget -t certificate -d info
${expected_output}      Issued
  1. ${input} :输入的cli命令。
  2. ${expected_output}:命令返回预期值。

Test Cases区:

测试用例区域,主要是ssh命令发送,以及对比命令返回值与预期值是否一致,一个测试套件可以存放多个测试用例,测试用例存在Get Certificate Info Via cli中,如图所示。

*** Test Cases ***
Get Certificate Info Via cli
    [Documentation]    通过ssh查询bmc证书信息
    [Tags]    Get_Certificate_Info_Via_cli

    # 增加会话稳定性检查
    Wait Until Keyword Succeeds    3x    2s    Verify SSH Connection    ${ssh}
    
    # 增加命令执行重试机制
    ${rsp}=    Run Keyword And Ignore Error    
    ...    Wait Until Keyword Succeeds    3x    1s    
    ...    Execute SSH Command    ${ssh}    ${input}
    
    # 清理内容
    ${clean_rsp}=    Evaluate    re.sub(r'\\x1b\\[[0-9;]*[a-zA-Z]', '', """${rsp[1]}""")    modules=re
    
    Should Not Be Empty    ${clean_rsp}    msg=未获取bmc证书信息
    Should Match Regexp    ${clean_rsp}    ${expected_output}       msg=未找到bmc证书信息
  1. [Documentation]:描述测试套件的用途。
  2. [Tags]:为测试用例添加标签,便于分类和执行。
  3. Wait Until Keyword Succeeds:可用于运行指定关键字,并在失败时进行重试。
  4. Verify SSH Connection:远程登陆连接ssh端口。
Verify SSH Connection
    [Arguments]    ${ssh}
    ${status}=    Run Keyword And Return Status    
    ...    Execute SSH Command    ${ssh}    echo ConnectionCheck
    Should Be True    ${status}    SSH连接已失效,需要重建连接
  1. Execute SSH Command:这是ssh library提供的的关键字,用途是借助SSH协议远程机器上执行命令。
    ${rsp}=    Run Keyword And Ignore Error    
    ...    Wait Until Keyword Succeeds    3x    1s    
    ...    Execute SSH Command    ${ssh}    ${input}
  1. Should Not Be Empty: 内置的关键字,用于验证变量是否为空。
  2. Should Match Regexp: 内置关键字,用于验证给定的字符串与指定的正则表达是匹配