The whole network is popular, Python interface automated testing, from 0 to 1 layered packaging framework coding (with interface)


foreword

As the industry's involution becomes more and more serious, the requirements for testing positions are also rising, and the wording of automated testing will appear in the job recruitment requirements, so it is also a must-have weapon for us to change jobs for interviews, promotions and salary increases.

Automated testing can be divided into interface automation, Web UI automation, and App automation. Today we will talk about interface automation testing.

Python interface automated testing: https://www.bilibili.com/video/BV16G411x76E/

The value of interface automation testing

1. Compared with UI automation testing, interface automation testing has greater benefits, is easier to implement, has lower maintenance costs, and has a higher input-output ratio, so it is the company's first choice for automated testing.

2. Front-end pages change quickly, and UI automation is time-consuming. For example, waiting for page elements to load, adding waiting time, positioning elements, operating elements, and simulating page actions all take time, so UI automation is difficult to implement.

3. The interface is relatively stable, and the response time of the interface is basically at the level of seconds and milliseconds, which is fast, and the interface automation itself can also perform some related operations and full-process operations, such as: registration --> login -- > Modify personal information.

Interface automation testing, high test reusability, fast regression (improving regression coverage, efficiency, stability), and more and more tedious tests can be run. An obvious benefit of automation is that it can be implemented in less time run more tests

tool selection

Python/Java + Requests + Unittest/Pytest + HTMLTestRunner/Allure
RobotFramework: Keyword-driven automatic testing framework
Web-based automated testing platform (independently developed by the company, high cost, easy to use without programming knowledge)

There are many automated testing tools, and each tool has its own advantages and disadvantages. The key point is to choose a framework that suits your actual situation and implement it.

Used here: Python + Requests + Pytest + Allure

Interface automation test practice

Interface Document
Find an open interface for testing
Interface information:

  • Name: National University Information Query Interface
  • Description: Used to query national university information
  • Host:https://www.iamwawa.cn/daxue.html
  • Request URL:/home/daxue/ajax
  • Request Method:POST
  • Content-Type: application/x-www-form-urlencoded
  • headers:user-agent:Chrome

parameter:

name type Is it required? describe
type String yes name, query by name
keyword String yes University name, such as: xxxxxx University

Example request:

POST /home/daxue/ajax HTTP/1.1
Host: www.iamwawa.cn
user-agent: Chrome
Content-Type: application/x-www-form-urlencoded
Cookie: PHPSESSID=sjsrn0drje6ds5fq9kouoo2r23
Content-Length: 54

type=name&keyword=厦门大学

Example response:

{
    
    
    "status": 1,
    "info": "查询成功!",
    "data": [
        {
    
    
            "id": "1124",
            "name": "厦门大学",
            "code": "4135010384",
            "department": "教育部",
            "city": "厦门市",
            "level": "本科",
            "remark": ""
        }
    ]
}

Linear script:

import requests

res = requests.post(url="https://www.iamwawa.cn/daxue.html",
                    headers={
    
    "user-agent": "Chrome"},
                    data={
    
    "type": "name", "keyword": "厦门大学"})
assert res.status_code == 200
res_json = res.json()
print(res_json)
assert res_json["status"] == 1

Using the above code, the most basic and simplest interface test is done, and a good start is half the battle.

However, problems also follow, and the shortcomings of linear scripts are also exposed: the
input and assertions of the test are bundled in the script, which is poor in readability;
there is no shared or reused script, and the reusability is low;
the cost of linear script modification Large, high maintenance cost, not easy to optimize later;
easily affected by software changes, easily affected by unexpected events, causing the entire test to fail;

Therefore, the next step is how to optimize the linear script, that is, how to achieve high cohesion and low coupling of the code, which is also a problem to be solved by the interface automation testing framework.

Please add a picture description

illustrate:

base_api.py: Secondary encapsulation of the Requests library to complete the api driver

api: Inherit base_api, encapsulate the http request interface into a Python method

utils: CommonUtil, public module, encapsulates some public functions, methods and general operations, such as: log module, yaml operation module, time module

config: configuration file module, configuration information storage, such as: URL, Port, Headers, Token, database information, etc.

data: test data module, used to manage test data, separate data from scripts, reduce maintenance costs and improve portability, such as: yml file data

cases: test case module, used for test case management, unit test framework will be used here, such as: Pytest, Unittest

run.py: The main program for batch execution of test cases, assembled according to different requirements and scenarios, following the flexibility and scalability of the framework

logs: The log module is used to record and manage logs, and set different log levels for different situations to facilitate problem location

reports: test report module, used to generate and manage test reports, such as: customized reports generated based on Allure

Please add a picture description

The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

Only by doing your best can you see your true self. No matter how bumpy the road ahead is, as long as you are firm in your heart, you will break out of the cocoon and become a butterfly, and fly high in the rainbow after the storm!

Only by doing our best can we make life more exciting. Don't be afraid of difficulties, don't shrink back and give up, as long as you have faith, keep your feet on the ground, and believe that your dreams will definitely become reality!

If you want to change the world, you must first change yourself. Only by constantly overcoming difficulties and constantly pursuing progress can we go wider and wider on the road of struggle and eventually achieve a brilliant life!

Guess you like

Origin blog.csdn.net/shuang_waiwai/article/details/130202132
Recommended