Interface test combat: Jmeter and Python combined to test the initial interface scenario


Follow me, share software testing technology dry goods every day, interview experience, want to receive test materials, and enter the software testing exchange group can directly send me a private message~~

One, test scenario

Stress testing the "user login" scenario, after the user logs in, a large number of interfaces will be called to test the load of the scenario

Second, the scheme used

plan 1. The Jmeter interface is used directly for pressure measurement, but Jmeter calls multiple interfaces in a thread are synchronized, so the pressure measurement results are not accurate.

Scenario 2. Jmeter uses multiple thread groups for stress testing, but this solution is parallel when tested and does not fit the scenario, because although the browser is an initialization call, it is essentially executed by one thread.

Three, the final plan

Jmeter combines Python3.x's asyncio and aiohttp.

Because Python's asyncio can simulate the browser's initialization call method

Fourth, use case design

Step 1 [User]: Use the CSV data configuration to read the user name and password

Step 2 [Encryption Password]: Use BeanShell to call the encrypted JAR to encrypt the password

3 steps [login clinical page]: log in to the system

4 steps [login design]: select login category

Five core implementation

Jmeter part

Components used: transaction controller, HttpSampler, BeanShell sampler, response assertion

Transaction controller

Consists of two parts

  1. HttpSampler

This part of the user name and password login verification interface

  1. BeanShell sampler

Explanation: Why use BeanShell Sampler instead of JSR, because JSR does not support Python 3.x

Key code description:

1: Preset Python parameters

# Visited server address

String host="200.200.101.97";

#Execute the Python file

String pyPath = "D: \\ sites \\ eClinical4.0_testing \\ PT \\ py_script \\ asyncio_http.py";

#The interface that needs to be initialized asynchronously, configured in a json file

String apis = "D: \\ sites \\ eClinical4.0_testing \\ PT \\ py_script \\ admin_login_api.json";

2: Start the Python process

8-10 lines

3: Wait for the Python progress to return, and save the result in the variable "${name} _admin_login"

Lines 11-23

Respond to assertions

Judge the return of Python by string, if all interfaces return to normal, the variable "${name} _admin_login" contains API OK

Python part

1. Libraries used

Import json

Import aiohttp

Import system

Import asynchronous

2. According to the interface that needs to be accessed, the initial task is generated and called

Asynchronously define async_run (host, apis, headers):

Result = []

Coroutine = []

For the information in apis.values():

If info.get("method") == "GET":

coroutines.append(asyncio.create_task(get(“ {0} / {1}”。format(host,info.get(“ url”)),headers,info.get(“ params”),results))))

elif info.get(“ method”)==“ POST”:

coroutines.append(asyncio.create_task(post(“ {0} / {1}”。format(host,info.get(“ url”)),headers,info.get(“ params”),results))))

Other: Pass

Wait for asyncio.gather (*coroutine)

Return result:

If type (ret) == exception: raise ret

The last 2 lines, if the Exception is returned, an exception is thrown

3. Encapsulate Post and Get requests, they are similar, so only Get requests are posted

异步def get(url,headers,params,results):

Asynchronous with aiohttp.ClientSession() as a session:

Asynchronously with session.get(url, headers = headers, json = params) is rsp:

r = wait for rsp.text()

ret = json.loads(r)

If ret.get("procCode")! = 200:

e = Exception()

e.args =(“ {0} {1}”。format(ret.get(“ procCode”),url),ret.get(“ exception”))

results.append(e)

Other:

results.append(ret)

If the interface returns incorrectly! = 200, an exception instance is generated and saved in the result, if it returns to normal, the returned result is directly saved in the result

4. Main functions

host =“ http:// {0}” .format(sys.argv [1])

Authorization = sys.argv[2]

apis_path = sys.argv [3]

And open(apis_path, "r") is f:

apis = json.load(f)

Header = {

"Authorization": authorization,

“ Accept”:“ application / json,text / plain,* / *”,

“ Content-Type”:“ application / json”

}

try:

asyncio.run(async_run(host,apis,headers))

打印(json.dumps(dict(procCode =“ API OK”)))

Exception, such as e:

如果len(e.args)== 1:print(json.dumps(dict(procCode = e.args [0])))

其他:print(json.dumps(dict(procCode = e.args [0],exception = e.args [1])))

Configure execution parameters and read json files according to jmeter parameters

If there is no error, output to jmeter through print

If there is an error, there is 1 error parameter, which is a network error, and output through print

There are 2 error parameters, which are returned by the interface and output through print


Finally: a wave of software testing data sharing!

In the technology industry, you must improve your technical skills and enrich your practical experience in automation projects. This will be very helpful for your career planning in the next few years and the depth of your test technology mastery.

In the interview season of the Golden 9th and the Silver 10th, the season of job-hopping, organizing interview questions has become my habit for many years! The following is my collection and sorting in recent years, the whole is organized around [software testing], the main content includes: python automation test exclusive video, Python automation details, a full set of interview questions and other knowledge content.

May you and I meet and you will find something! If you want to exchange experience in software testing, interface testing, automated testing, and interviews. Follow WeChat public account:[Sad Spicy Strips]Receive a 216-page software test engineer interview book for free. And the corresponding video learning tutorials are free to share! Communication learning skirt:313782132

Recommend good articles:

Packaged as a test engineer with 1 year of work experience, my advice before the interview is as follows

What exactly should I learn in automated testing?

Why not consider Tencent for job-hopping? Talk about a little bit of the past between me and the goose factory

Which is more advanced, automated testing or manual testing?

Novice must see: How to write a qualified test case?

Python login interface test problem record and solution (dry goods)

Guess you like

Origin blog.csdn.net/weixin_50829653/article/details/113989737