Pytest based interface test

Recently interface testing to be carried out, the company initially intends to use existing tools to test Fitnesse interface testing. Found during the interface configuration field data, the test data are more difficult, more time interface parameters, the amount of use cases will be much slower execution speed critical. So give up.

Find some other tools, we are not able to solve the problem of data structure difficult. Can not find tool, the direct use of code to implement. Considering the amount of code, with some recommended online, we decided to use python + pytest to write an interface automation use cases.

Determines the language and framework, then have to consider the implementation requirements.

1. Example of a use environment can be multiple sets of test

2. The scheduling may be performed jenkins

3. have test report

4. Interface certain field value is not repeated in each request

The multi-interface may be associated with the test

The data in Table 6. The data structure may be associated with the interface and field

7.pytest actual use case examples and use the data to be separated, easy maintenance

8. The content for a variety of response includes various assertions manner.

 A demand: a plurality sets of test cases with the environment

More than one set's test environment, it is desirable when using the interface with the automated embodiment, the test environment can freely switch.

To meet this demand, we must first complete interface address and other configuration information independently, but in accordance with an environmental dimension to the management of information.

 My approach as shown above, first of all I have to set up an alias for each environment, such as the image above lion environment, and then design a service to the enduring value of the variable information (variable names consistent with all environmental preservation).

 

 

Next the variable name and the name of the environment, in combination, to save redis (above), the interface for subsequent read and used in Example Automation.

 

After the environmental information to get, the next task is to design a way to make use of environmental information automation interfaces use cases.

The method here is that, when executed, specify the alias environment.

There are several use cases pytest implementation, as used herein, pytest.main () to start writing a file py, as in the following code by pytest.main ().

 
 
memberCenter.py

1
if __name__ == "__main__": 2 if (len(sys.argv) == 2): 3 _, env = sys.argv 4 else: 5 env = 'lion' 6 BaseUtil.initTest(env) 7 pytest.main(['--alluredir=./allure-results', '--maxfail=5','-s','-rA', 'testcase/membercenter/'])

 At startup, accepts a parameter env, and env as attributes added to the Context, for use cases.

BaseUtil.py
from context import Context as ct
def initTest(env):
 ct.env = env 

Invoke the command:

 

 Here designated test environment alias lion.

With aliases environment, coupled with the uniform variable name, you can use the following ways to get redis corresponding to the value of the variable.

 

 The need to achieve more and more environmental testing. As long as the subsequent maintenance of good environmental aliases, variable name and variable values ​​on it.

 

Demand II: scheduling can be performed jenkins

This is relatively simple, it can be constructed by parameterization.

 

 However, in order not to affect Jenkins host server, I used to perform the use cases docker

The following configuration is Dockerfile

 

 The following are the Execute shell jenkins

. 1 echo " Clear History Report record " 
2  CD $ {WORKSPACE}
 . 3 CD-Report Allure && * RM -rf
 . 4  CD $ {WORKSPACE}
 . 5 CD-Allure Results && * RM -rf
 . 6  
. 7 echo " Start Run " 
. 8  CD WORKSPACE} {$
 . 9  
10  function {del_ci
 . 11    echo " $. 1 " 
12 is    Docker STOP chbifacetest
 13 is    Docker RM chbifacetest
 14    Docker RMI hbifacetest: 1.1
 15  }
 16  
. 17 Docker --format Inspect '{{.State.Running}}' chbifacetest && del_ci "删除容器和镜像"
18 
19 
20 docker build -t hbifacetest:1.1 .
21 docker run  -v ${WORKSPACE}/allure-results:/usr/local/hbifacetest/allure-results -v ${WORKSPACE}/allure-report:/usr/local/hbifacetest/allure-report --name chbifacetest hbifacetest:1.1 ${pymainfile} ${testEnv}
22 echo "执行结束"

 Demand has three test report

 updating

Guess you like

Origin www.cnblogs.com/moonpool/p/11649996.html