[Software Testing] Ultra-fine HttpRunner interface automation framework use cases, a guide to get through…

Table of Contents: Introduction

    • Preface
    • 1. Python programming from entry to proficiency
    • 2. Practical implementation of interface automation projects
    • 3. Web automation project actual combat
    • 4. Practical implementation of App automation project
    • 5. Resumes of first-tier manufacturers
    • 6. Test and develop DevOps system
    • 7. Commonly used automated testing tools
    • 8. JMeter performance test
    • 9. Summary (little surprise at the end)

Foreword

1. HttpRunner framework installation

# Install httprunner
pip install httprunner

# Of course, you can also specify the version to install.
pip install httprunner==2.3.2

# Check whether the installation is successful
hrun -V

2. Introduction to HttpRunner framework demo

#Create HttpRunner project
hrun --startproject demo

#Specify the directory to create the HttpRunner project
hrun --startproject D:\TestSoftware\Pychram\PychramProject\demo

#api file:
 * Used to save a single independent interface, preferably one that can be run independently (for example: save a public method for case calling, calling format: api/XXX.json or api/XXX.yaml)
# testcase file:
 * Used to save test cases composed of one or more interfaces (it can also collect other cases for sequential execution, calling format: testcase/xxx.json or api/XXX.yaml)

# Execute test cases in the project console:
hrun testcases/xx.json
# or
hrun testcases/xx.yaml

# testsuites file:
 * A collection of multiple test cases (the execution order of the test case set is unordered)
# reports file:
 * The location where the test report is generated after running the test case

# debugtalk.py file:
 * Define methods in files (you can write methods in other .py files and then call them, calling format: ${<!-- -->getdemo()))

# .env file:
 * Custom variables (calling format: ${<!-- -->.ENV(demo)}

Since different companies have different requirements, JSON and Yaml files can be transferred to each other at: http://www.json2yaml.com/

3. Use JSON files to handle requests in different ways

1) Send request by get method

# Create a new get request.json under testcases
{<!-- -->
  "config": {<!-- -->
    "name": "Module name",
    "base_url": "Host IP address and port"
  },
  "teststeps": [{<!-- -->
    "name":"Get method",
    "request": {<!-- -->
      "url": "/Api url address",
      "method": "GET",
      "params": {<!-- -->
        "username": "abc",
        "password": "123",
        "phone": "12345678901",
        "email": "[email protected]"
      }
    }
  }
  ]
}

2) Send request by post method

# Create a new post request.json under testcases
{<!-- -->
  "config": {<!-- -->
    "name": "Module name",
    "base_url": "Host IP address and port"
  },
  "teststeps": [
    {<!-- -->
      "name": "Post method",
      "request": {<!-- -->
        "url": "API url address",
        "method": "POST",
        "data": {<!-- -->"username": "zhangsan","password": "123"}
      },
      "validate":[
        {<!-- -->"contains": ["content.msg","Login successful!!"]}
      ]
    }
  ]
}

3) Send request in json mode

# Create a new json request.json under testcases
{<!-- -->
  "config": {<!-- -->
    "name": "Module name",
    "base_url": "Host IP address and port"
  },
  "teststeps": [
    {<!-- -->
      "name": "json message processing",
      "request": {<!-- -->
        "url": "/Api url address",
        "method": "POST",
        "json": {<!-- -->"username": "lisi","password": "123","phone": "123456","email": "[email protected]"}
      },
      "validate": [
        {<!-- -->"eq": ["content.msg","Registration successful"]}
      ]
    }
  ]
}

4) Upload files
First add the getFile function in the debugtalk.py file

def getFile():
    f = open("File storage address\file name.png",mode="rb")
    return f
# Create a new Uploadfile.json under testcases
# Use ${getFile()} to reference the function
{<!-- -->
  "config": {<!-- -->
    "name": "Module name",
    "base_url": "Host IP address and port"
  },
  "teststeps": [
    {<!-- -->
      "name": "Upload file",
      "request": {<!-- -->
        "url": "/Api url address",
        "method": "POST",
        "data": {<!-- -->"username": "lisi"},
        "files":{<!-- -->
          "pic": ["woman0.png","${getFile()}","image/png"]
        }
      }
    }
  ]
}

5) Parameterization

The first way – variables set variables

# Create new parameter.json under testcases
{<!-- -->
  "config": {<!-- -->
    "name": "Module name",
    "base_url": "Host IP address and port",
    "variables": {<!-- -->"name": "a8","pwd": "123456"}
  },
  "teststeps": [
    {<!-- -->
      "name": "Parameterization",
      "request": {<!-- -->
        "url": "/Api url address",
        "method": "POST",
        "data": {<!-- -->
          "username": "$name",
          "password": "$pwd"
        }
      },
      "validate": [
        {<!-- -->"eq":["content.msg","success"]}
      ]
    }
  ]
}

The second way –p gets the function

First, create a new package dada in the project directory and create a user.csv file under data.

name,pwd,extra
a1,123,success
a2,456, login failed
#¥@,123, login failed

Then create a new login_param.json in the testsuites directory

{<!-- -->
  "config": {<!-- -->
    "name": "login_parameterized processing"
  },
  "testcases": [
    {<!-- -->
      "name": "loginTest",
      "testcase": "testcases/login.json",
      "parameters": [
        {<!-- -->
          "name-pwd-extra":"${P(data/user.csv)}"
        }
      ]
    }
  ]
}

Finally, create a new login.json in the testcases directory

# Directly reference the custom name
{<!-- -->
  "config": {<!-- -->
    "name": "Module name",
    "base_url": "Host IP and port"
  },
  "teststeps": [
    {<!-- -->
      "name": "Login",
      "request": {<!-- -->
        "url": "Api url address",
        "method": "POST",
        "data": {<!-- -->"username": "$name","password": "$pwd"}
      },
      "validate":[
        {<!-- -->"contains": ["content.msg","$extra"]}
      ]
    }
  ]
}

6) Interface dependency processing

# Create a new api dependency.json under testcases
{<!-- -->
  "config": {<!-- -->
    "name": "Module name",
    "base_url": "Host IP address and port",
    "variables": {<!-- -->
      "name": "b1","pwd": "123456","email": "[email protected]"
    }
  },
  "teststeps": [
    {<!-- -->
      "name": "Get verification code",
      "request": {<!-- -->
        "url": "/Api url address",
        "method": "GET"
      },
      "extract": [
        {<!-- -->"code": "content.data.code"}
      ],
      "validate": [
        {<!-- -->"eq": ["content.msg","success"]}
      ]
    },
    {<!-- -->
      "name": "Register",
      "request": {<!-- -->
        "url": "/Api url address",
        "method": "POST",
        "data": {<!-- -->
          "username": "$name","password":"$pwd" ,"email":"$email" ,"verifyCode":"$code"
        }
      },
      "validate": [
        {<!-- -->"contains": ["content.msg","Registration successful"]}
      ]
    }
  ]
}

4. Batch execution of cases

Create a new batch execution.json in the testsuites directory

{<!-- -->
  "config": {<!-- -->
    "name": "Interface Automation Learning Case"
  },
  "testcases": [
    {<!-- -->
      "name": "Test_get request",
      "testcase": "testcases/getrequest.json"
    },
    {<!-- -->
      "name": "Test_post request",
      "testcase": "testcases/postrequest.json"
    },
    {<!-- -->
      "name": "Test_json request",
      "testcase": "testcases/jsonrequest.json"
    },
    {<!-- -->
      "name": "Test_json request",
      "testcase": "testcases/Uploadfile.json"
    },
    {<!-- -->
      "name": "Test_Dependency Processing",
      "testcase": "testcases/apidependency.json"
    }
  ]
}

console

hrun testsuites/batch execution.json

5. Output test report

1) Native test report

Through the console hrun testsuites/xx.json After the test is passed, the report will be automatically generated under reports in the project directory.

2) Test report optimization

Enter under Lib\site-packages\httprunner\static, add extent_report_template.html, and under Lib\site-packages\httprunner, modify the default report path in report.py

After modification, it was changed to:

The following is the most comprehensive software testing engineer learning knowledge architecture system diagram I compiled in 2023

1. Getting started with Python programming to becoming proficient

Please add image description

2. Practical implementation of interface automation project

Please add image description

3. Web automation project practice

Please add image description

4. App automation project practice

Please add image description

5. Resumes of first-tier manufacturers

Please add image description

6. Test and develop DevOps system

Please add image description

7. Commonly used automated testing tools

Please add image description

8. JMeter performance test

Please add image description

9. Summary (little surprise at the end)

As long as you have a dream, pursue it bravely and don’t be intimidated by difficulties and obstacles. Believe in your own abilities and value, and keep going. Success is waiting for you ahead!

Every effort is an accumulation, and every struggle is a growth. Don’t be afraid of difficulties and keep chasing your dreams, because only by keeping moving forward can you create your own brilliant life!

Success requires not only talent and opportunity, but also persistence and unremitting efforts. As long as you maintain your enthusiasm and perseverance, you will definitely be able to realize your ideals!