How to easily switch the environment address of the automated test through the Pytest plug-in?

foreword

The previous editor introduced how to switch the automated test environment through the Pytest plug-in. The method used at that time was to obtain the command line parameter value through the hook function, and then switch the test environment address through the pre-configured parameters. Add me VX: atstudy-js Reply "Test", enter the automated test learning exchange group~~​

Today, the editor introduces a method again, which is realized through the Pytest plug-in: pytest-base-url.

pytest-base-url

pytest-base-url is a third-party plug-in of Pytest, which is mainly used to help us switch the test environment address. There are two configuration methods, one is to add url parameters directly on the command line, and the other is to pass pytest.ini method to store the test environment address in advance, and then follow the editor to see how the two methods are implemented.

Pass in command line parameters

The command line parameter is passed in. Here we need to add the parameter name of base-url in advance in the test case we need to pass in, so that it is convenient to pass in the parameter directly in the subsequent command line.

#test_01.py

#coding:utf-8

import requests

def test_01(base_url):

data={

'city': "Shanghai",

'key':'xxxxxxxxxx'

}

r=requests.post(base_url,data=data)

result=r.json()['reason']

print(r.json())

assert result=='query successful!'

After the test case is written, we open the command line and execute the corresponding command. Normally, we only need to add the pytest-vs execution program to run successfully, but here we need to add a new parameter, which is our Test environment address.

pytest-vs--base-url test environment address execution program.

pytest-vs--base-url http://apis.juhe.cn/simpleWeather/query test_01.py

After running, we will find that our test case has been successfully executed.

If you don't want to execute it through the command line, we can also directly write the command line parameters into the Pytest running program:

#coding:utf-8

import pytest

import requests

def test_01(base_url):

data={

'city': "Shanghai",

'key':'xxxxxxxxx'

}

r=requests.post(base_url,data=data)

result=r.json()['reason']

print(r.json())

assert result=='query successful!'

if__name__=='__main__':

pytest.main(['-vs','--base-url','http://apis.juhe.cn/simpleWeather/query',])

Note: When writing the command line here, be sure to write --base-url and the test environment address separately, otherwise the program will report an error.

pytest.ini method

Among them, pytest.ini mainly belongs to the configuration file tool of Pytest. Some simple parameters can be stored in this file. I will need to store our test environment through the pytest.ini file later.

Create a pytest.ini file and store its base_url parameters into:

[pytest]

base_url=http://apis.juhe.cn/simpleWeather/query

After the configuration file is written, run it directly through the top code, and there is no need to add the corresponding url address and base_url command to the command line parameters.

#coding:utf-8

import pytest

import requests

def test_01(base_url):

data={

'city': "Shanghai",

'key':'xxxxxxxx'

}

r=requests.post(base_url,data=data)

result=r.json()['reason']

print(r.json())

assert result=='query successful!'

Some friends here will ask, how to address multiple test environments, and how should I switch here? This is simple, we still help us realize this function by running the pytest.ini configuration file, we add the parameter addopts parameter in the pytest.ini configuration file, and we need to create two ini files, representing two test environments respectively.

pytest_dev.ini file:

#pytest_dev. ini

[pytest]

addopts=--base-url=https://test.anjing.com

pytest_qa.ini file:

#pytest_qa. ini

[pytest]

addopts=--base-url=https://qa.anjing.com

Then simply write an assertion to judge whether the parameters we want are successfully passed in.

#coding:utf-8

def test_home_page(base_url):

print(base_url)

assert base_url=="https://test.anjing.com"

Next, we continue to execute through the command line parameters, but at this time we need to add the -o parameter. The so-called -o parameter is to read the configuration information of different ini files. First read the pytest_dev.ini configuration file and find that the execution program is normal.

Next, we read the configuration file of pytest_qa.ini and found that the program assertion failed, and the addresses of the two test environments were inconsistent.

Summarize

The editor introduces switching our test environment in the process of automated testing through two simple methods. Of course, the method is not fixed. The main thing is how you can use it flexibly according to the project.

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. 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, and I hope it can help you! Partners can click the small card below to receive 

Guess you like

Origin blog.csdn.net/qq_73332379/article/details/132691206