[Jenkins] The most detailed automated testing on the entire network

A series of articles about learning Jenkins automated testing

  1. Robot Framework concept
  2. Robot Framework installation
  3. Pycharm + Robot Framework environment construction
  4. Introduction to Robot Framework
  5. Jenkins automated testing
1. Robot Framework concept

Robot Framework is a Python-based, extensible keyword-driven automated testing framework.

It has several main features:

  1. Create test cases using easy-to-use tabular syntax;
  2. Provides the ability to expand from existing keywords to higher-level keywords;
  3. Provides easy-to-read results reports and HTML logs;
  4. Provide markers to classify and select test cases for execution;
  5. Platforms and applications are independent;
  6. Support the creation of data-driven test cases;

The test data is in a simple, easy-to-edit tabular format. When Robot Framework is started, it processes the test data, executes test cases and generates logs and reports. The core framework knows nothing about the target under test, interaction with it is handled by the test library, which can use the API directly or use low-level testing tools as drivers

2. Robot Framework installation

Install Robot Framework via pip:

  1. Download python from the link, set the environment variables, and enter python in the CMD console to verify whether the installation is successful.
  2. Download the pip link, decompress it, enter the decompression directory in the CMD console, enter python setup.py install, the installation is successful, set the environment variables, enter pip to verify whether the installation is successful.

3. pip install wxpython 2.8.12.1: pip install wxpython==2.8.12.1

4. pip install robotframework: pip install robotframework

5. pip install robotframework-selenium2library: pip install robotframework-selenium2library

6. pip install rtomac-robotframework-selenium2library: pip install rtomac-robotframework-selenium2library

7. pip install decorator-3.3.3.tar: pip install decorator

8. pip install robotframework-ride: pip install robotframework-ride

9. Enter ride.py in CMD. When the RIDE interface pops up, the Robot Framework is successfully built.

10. Create a desktop RIDE icon: right-click on the desktop to create a shortcut, enter C:\Programs\Python27\pythonw.exe -c “from robotide import main; main()” in the input object field, and click Next set the icon name RIDE. Change the icon to the robot icon, right-click RIDE, select Properties, click Change Icon, select the directory F:\Python27\Lib\site-packages\robotide\widgets in the browser, find robot.ico, and click OK. The icon changes to the robot icon RIDE, Done!

Tip: If the following error occurs during pip installation:

The timeout is caused by not setting a proxy, which can be solved by setting the proxy pip install wxpython==2.8.12.1 –proxy= and downloading.

3. Pycharm + Robot Framework environment construction

  • Download Pycharm
  • Configure intelliBot in Pycharm: In Pycharm, click File -> Settings -> Plugins, enter intelliBot in the search bar, and click Install. If you need to set up a proxy, click HTTP Proxy Settings to set the proxy to download and install.
  • Configure the running environment of suite and case in Pycharm:

Configuration suite: Robot Run TestSuite / -d results $FileName$ / $FileDir$

Configuration case: Robot Run SingleTestCase / -d results -t “$SelectedText$” ./ / $FileDir$

4. Whether the test environment is set up successfully:

A simple case:

*** Settings ***
Documentation Example case for test
Force Tags owner
Library Selenium2Library
Library Collections
 
***Variables***
${var} 1
${result} 1
 
***Test Cases***
First Case
    [Documentation] this is a first case for test example
    [Tags] person
    Log to console ${var}
    Log to console ${result}
    should be equal ${result} ${var}

Right-click on the file name -> External Tools -> Robot Run TestSuite, the test results are displayed, and the environment is set up successfully.

4. Introduction to Robot Framework

4.1 Variables

Robot Framework uses $, @, & amp; to represent scalars, lists, and dictionaries respectively. Create variables under Variables. A simple case looks like this:

*** Variables ***
${var} value
@{list} a b c ${var}
 & amp;{dict} key1=name key2=${list}
 
***Test Cases***
First Case
Log to console ${var}
Log to console ${list}
Log to console ${dict} 

The syntax corresponding to python is var = “value”, list1 = [a’,’b’,’c’,var], dict1={key1’:’name’, key2’:list1}.

The spaces used as separators can vary, as long as they are larger than two spaces, so the data can be well aligned. It is recommended to use 4 spaces between keywords and parameters.

4.2 Keywords

Keywords keywords are divided into built-in keywords and custom keywords.

Commonly used built-in keywords:

  1. Set Variable If: ${var2} Set Variable If condition value1 value2, given a condition and two values, if the condition is true, return the first value, otherwise return the second value;
  2. Should contain: Should Contain ${result} value, if result does not include value one or more times, the test fails;
  3. log: record the given information;
  4. log to console: Print the given information to the console;
  5. set suite variable: Set Suite Variable ${Scalar} ${Hello world}, making the variables available within the current suite scope;
  6. Run Keyword If: Run Keyword If condition action arg, if condition is true, use the given parameters to run the specified keyword;

Custom keywords:

resource.txt
 
***Test Cases***
First Case
    [Documentation] this is a first case for resource
    [Tags] anan
    Calculate and Check Equals
    Calculate and Check Equals expression=6 + 3 expected=${9}
 
***Keywords***
Calculate and Check Equals
    [Arguments] ${expression}=3 + 3 ${expected}=${6}
    ${res} Evaluate ${expression}
    Log to console expression=${expression}
    should be equal ${res} ${expected}

The custom keyword Calculate and Check Equals is similar to the Python method. It has two parameters, expression and expected. The default values are both 6. If the input parameter is not specified, the default value is used.

4.3 resource/Library

The resource import file is used in the setting table, and the file contains the definition of keywords.

Use library in the setting table to import built-in libraries and custom libraries, which define a series of methods.

*** Settings ***
Documentation Example case for resource
Force Tags owner
Resource ./resource/resource.txt
Library Selenium2Library
Library Collections
Library ./lib/robot.py

Where robot.py is a custom library.

4.4 Custom library

A series of keyword methods are defined in the custom library.

robot.py
 
import hashlib
 
 
def gen_sign(*args):
    m = hashlib.md5()
    m.update(''.join(args))
    return m.hexdigest()
def gen_sign_keywords(*args):
    m = hashlib.md5()
    m.update(''.join(args))
resource.txt
 
***Test Cases***
First Case
    [Documentation] this is a first case for resource
    [Tags] anan
    Calculate and Check Equals
    Calculate and Check Equals expression=6 + 3 expected=${9}
 
***Keywords***
Calculate and Check Equals
    [Arguments] ${expression}=3 + 3 ${expected}=${6}
    ${res} Evaluate ${expression}
    Log to console expression=${expression}
    should be equal ${res} ${expected}
 
    ${result} gen_sign @{list1}
    log to console ${result}

4.5 setup & teardown

Test Setup is executed before test case execution, and Teardown is executed after execution.

In this way, if multiple test cases need to execute Test Setup and Teardown, the common parts can be put into Suite Setup and Suite Teardown for execution, so that they only need to be executed once.

*** Settings ***
Suite Setup Commen_Suite_Setup
Suite Teardown Commen_Suite_Teardown
Documentation Example case for resource
Force Tags owner
Resource ./resource/resource.txt
Library Selenium2Library
Library Collections
Library ./lib/robot.py

robot framework test example:

*** Settings ***
Suite Setup Commen_Suite_Setup
Suite Teardown Commen_Suite_Teardown
Documentation Example case for resource
Force Tags owner
Resource ./resource/resource.txt
Library Selenium2Library
Library Collections
Library ./lib/robot.py
 
***Test Cases***
 
First Case
    [Documentation] this is a first case for resource
    [Tags] anan
    Calculate and Check Equals
    Calculate and Check Equals expression=6 + 3 expected=${9}
    ${result_first_case} gen_sign @{list1}
    Set Suite Variable ${test_id} ${result_first_case}
    Log to console ${test_id}
Second Case
    [Documentation] this is a second case for custom lib
    [Tags] anan
    ${sign} gen_sign @{list1}
    Log to console ${sign}
    Log to console ${test_id}
*** Settings ***
Documentation this is a test demo case
Library ./../lib/robot.py
 
***Variables***
${var1} value
@{list1} a b c d
 & amp;{dict1} key1=sf key2=${list1}
${INSTANTIATE_JSON_FILE}
${INSTANCEID}
 
***Keywords***
Calculate and Check Equals
    [Arguments] ${expression}=3 + 3 ${expected}=${6}
    ${res} Evaluate ${expression}
    Log to console expression=${expression}
    should be equal ${res} ${expected}
 
    ${result} gen_sign @{list1}
    log to console ${result}
    should contain ${result} c
    log to console ${result}
 
Commen_Suite_Setup
    Common_Case_Setup
    Common_Case_Log
 
Commen_Suite_Teardown
    Undeploy_Case_Setup
 
Common_Case_Setup
    Log to console ${var1}
 
Common_Case_Log
    Log to console huyun's test case
Undeploy_Case_Setup
    Log to console undeploy case setup
import hashlib
 
 
def gen_sign(*args):
    m = hashlib.md5()
    m.update(''.join(args))
    return m.hexdigest()
 
 
def gen_sign_keywords(*args):
    m = hashlib.md5()
    m.update(''.join(args))

5. Jenkins automated testing

5.1 Continuous integration build

Continuous integration build steps: Code integration -> Compile -> Packaging -> Deployment -> Test

Code integration: branch management, using tools such as SVN or Git.

Compilation/packaging: Tools used include maven, IDE, etc.

Deployment: Install the software to the server or client.

Test: daily build, continuous integration build, CI (Continuous integration) version.

5.2 Automated testing

Automated testing concept: Using machines to perform tests instead of humans by writing scripts.

Automated testing process:

  • Functional testing process: Requirements analysis – test plan – use case design – test execution – test report
  • Automated testing process: test case selection – use case coding implementation – test execution – test report

5.3 Automated testing framework

Automated testing framework (Robot Framework):

Automated testing frameworks are divided into several types:

  • Keyword-driven framework: Abstract the operation methods of the test process into keywords;
  • Data-driven framework: Automatically load data used in testing during the automated testing process;
  • Hybrid framework: A framework that integrates data-driven, keyword-driven and other technologies;

5.4 Jenkins automated testing

Jenkins is a powerful application that allows continuous integration and continuous delivery projects.

Thank you to everyone who read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!Yes Friends who need it can click on the small card below to get it