[Necessary for interviews] The core knowledge points of interface automation are condensed to add points to the interview (recommended collection)

The interface automation that we come into contact with daily can be divided into two categories from the actual goal:

for mock test data

interface automation

Most of this interface automation is executed once. The purpose is to create test data for functional testing, save the time and labor costs of artificial data, and improve the testing efficiency of functional testers.

Find bugs early, before functional testing

interface automation

The workflow of this interface automation is the same as that of functional testing. It is necessary to design interface test cases and then execute interface test cases.

To put it bluntly, it is to perform functional verification on a single interface, including whether the interface parameters are required, length character type restrictions, input parameter enumeration values, etc., and whether the response data is correct.

This article mainly explains the second type of interface automation testing. The core points of interface automation are condensed for actual combat, which may become a bonus point for interviews.

  • Interface test case design

  • Code reading interface test case

  • Use pytest framework to realize interface automation

  • Use python-requests to complete the interface request

  • Use jenkins+allure to achieve continuous build and output test report

demand analysis

  • Excel management interface test case

  • Pytest framework to run test cases

  • Jenkins integrated build

  • Allure display test report

data preparation

understand the interface

1. Interface request method: get

2. Interface request header: {"Content-Type": "text/html;charset=utf-8"}

3. Interface request body: {"key": "****************,"type": ""}

4. Parameter value: type=guoji, keji, yule, default top

5. News request: http://v.juhe.cn/toutiao/index

Remarks: The key requested by the interface needs to be applied for by the aggregation data platform.

Design interface test cases

The design idea of ​​the interface test case is consistent with the design of the functional test case, which is basically verification: required items, character type length limit, input parameter content, etc.

The use cases of the interface are designed as follows: (for reference only)

Script directory design

A complete interface automation test is generally divided into four basic folders: data, public configuration, interface script, and report.

  • Data: The Data folder is used to store data, such as test case documents

  • Public scripts: the Common folder is used to store code files of public methods, for example: reading test case code files, etc.

  • Interface script: the Request folder is used to store code files of various business interfaces, for example: get news headline interface code files, etc.

  • Report: Report folder, used to store test result reports

Script design related knowledge points

read test case

Knowledge point 1

The basic method of reading excel by xlrd

1. Open the file object: fb=xlrd.open_workbook('xls test case file path')

2. Get the worksheet name: sheetnames=fb.sheet_names ()

3. Open the worksheet where the test case is located: casesheet=fb.get_by_name(''The name of the worksheet where the test case is located)

4. Get the number of test cases (table rows): rows=casesheet.nrows

5. Get the cell value in the worksheet: cell_value=casesheet.cell_values(x,y), where x and y are the abscissa and ordinate of the cell

6. Since it needs to be parameterized by the decorator of pytest, the read cells need to be combined into a list template

For example:

case1=[‘url_01’,’method_01’,{body_01 },’expect_01’]case2=[‘url_02’,’method_02’,{body_02 },’expect_02’]

(Swipe left and right to view the complete code)

The final data provided to pytest to read should be:

[[‘url_01’,’method_01’,{body_01 },’expect_01’],[‘url_02’,’method_02’,{body_02 },’expect_02’]]

(Swipe left and right to view the complete code)

code segment:

Use the method append() for list appending.

 Remarks: The cell data read by cell_values() defaults to the string str type.

Knowledge point 2: Data conversion

1.url and method are string types, so no need to convert

2. The header and body need to be converted to json format through json.loads()

interface request

Knowledge point 1

requests interface request basics

1. Initiate an interface request:

resq=request.get()resq =request.post()

2. Get the response data and convert it to json format: resq.json()

3. Assertion: assert

4. Extract response content: jsonpath syntax

response[‘msg’]response[‘data’][‘name’]

Knowledge point 2: pytest framework

Environment installation:

pip install pytest

1. Pytest usage rules: use cases and script files must start with test

2. Parameterized execution use case: Pytest decorator @pytest.mark.aprametrize()

a) Single parameter writing: @pytest.mark.parametrize('inData',[10,20]), the first parameter is the variable name, and the second is the parameter value. There is no need to set the loop to read the parameter value, because the decorator of pytest will automatically loop to read.

b) How to write multiple parameters: (parameter values ​​are written in tuple form) @pytest.mark.parametrize('the name of the first parameter, the name of the second parameter'), [(the first value of the first parameter, second value of first argument), (first value of second argument, second value of second argument)].

c) There is no need to set the loop to read the parameter value, because the decorator of pytest will automatically loop to read.

3. Debug and run the script locally:

The interface automatically runs the script to generate the allure-html report, which is divided into 2 steps:

Step 1: alluredir generates the test report data source file json and saves it in the report folder

pytest.main(['Interface request script file.py','-s','--alluredir=../report/'])

Step 2: Convert the test report data source file json to html report from the report folder

os.system('allure generate ../report/ -o ../report_html/')

Continuous build

Knowledge points: jenkins build + allure test report

1. Jenkins environment construction

2. Download the allure report plugin

3. build

a) Construction: use the pytest command to execute the script and generate allure source data

b) Post-build operation: add allure report (jenkins will automatically combine allure source data into html report)

result


END meager strength

Finally, I would like to thank everyone who has read my article carefully. Seeing the fans’ growth and attention all the way, there is always a need for reciprocity. Although it is not a very valuable thing, you can take it away if you need it:

These materials 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 help you too!

加入我的软件测试交流群:110685036免费获取~(同行大佬一起学术交流,每晚都有大佬直播分享技术知识点)

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

method of obtaining:

Guess you like

Origin blog.csdn.net/myh919/article/details/131314306