2 methods to teach you how to use Python to implement parameter association in interface automation

Preface

Usually in interface automation, there is often the problem of parameter association. So what is parameter association?

Parameter association means that the return value of the previous interface will be used as a parameter by the next interface. There are many ways to implement parameter association in Python. Today I will introduce to you how to implement parameter association in interface automation through Python. .

UnitTest

Although the Pytest framework is relatively popular at present, most companies should still use the UnitTest framework. So the editor will first introduce how to realize the parameter association of interface automation through UnitTest.

method one

Below, the editor implements parameter association in the form of test case return parameters.

# coding:utf-8
import requests
import unittest
class Test(unittest.TestCase):

    def test_01(self):
        '''查询天气接口测试用例'''
        url = 'http://apis.juhe.cn/simpleWeather/query'
        data = {
            'city': '上海',
            'key': 'xxxxxxxxxxxx'
        }
        r = requests.post(url, data=data).json()
        info = r['result']['realtime']['info']
        print(info)
        return info

    def test_02(self):
        '''查询字典测试用例'''
        a = self.test_01()
        url = 'http://v.juhe.cn/xhzd/query'
        data = {
            'word': a,
            'key': 'xxxxxxxxxxxx'
        ''
        }
        r = requests.post(url,data=data)
        result = r.json()
        print(result)
if __name__ == '__main__':
    unittest.main()

By using the results returned by the above method to supply the next interface, it will not be affected by the execution order, but the above use case will be executed one more time. It can be seen from the execution results below that the interface association has been successfully implemented.

现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036

Method Two

The editor uses a global variable method here. We first define a name, then use this name to receive the parameter value we are about to return, and finally use it in the next use case.

# coding:utf-8
import requests
import unittest

class Test(unittest.TestCase):

    def test_01(self):
        '''查询天气接口测试用例'''
        global info
        url = 'http://apis.juhe.cn/simpleWeather/query'
        data = {
            'city': '上海',
            'key': 'xxxxxxxxxxx'
        }
        r = requests.post(url, data=data).json()
        info = r['result']['realtime']['info']
        print(info)

    def test_02(self):
        '''查询字典测试用例'''
        url = 'http://v.juhe.cn/xhzd/query'
        data = {
            'word': info,
            'key': 'xxxxxxxx'
        ''
        }
        r = requests.post(url,data=data)
        result = r.json()
        print(result)
if __name__ == '__main__':
    unittest.main()

By running the above code, it is found that the parameter "clear" in the weather interface has been successfully extracted, and then provided for query by the next interface.

Pytest

Now that we have introduced UnitTest, let me introduce how to implement parameter association through Pytest. In fact, the principles are the same, but I will introduce a few more similar methods.

# coding:utf-8
import requests
import re
def test_01():
    '''查询天气接口测试用例'''
    url = 'http://apis.juhe.cn/simpleWeather/query'
    data = {
        'city': '上海',
        'key': 'xxxxxxxx'
    }
    r = requests.post(url, data=data).json()
    # 通过正则获取想要的数据
    a = re.findall("'info': '(.*?)', 'wid': '00', ", str(r))
    info = globals()['info'] = a[0]
    print(info)

def test_02():
    '''查询字典测试用例'''
    url = 'http://v.juhe.cn/xhzd/query'
    data = {
        'word': globals()['info'],
        'key': 'xxxxxxx'
    ''
    }
    r = requests.post(url,data=data)
    result = r.json()
    print(result)

In the above test case, the editor uses the globals() function to obtain the global scope, and the result is returned in the form of a dictionary. Moreover, when the editor obtains the parameter value of the next excuse, the editor uses regular expression extraction to form There are many ways to achieve our functionality.

Summarize

The editor introduced the two frameworks of Pytest and UnitTest respectively, how to associate parameters in the form of global variables when doing interface testing, and also introduced different methods for extracting parameter values. Friends can do it themselves. Give it a try.

If there is a better method, you can also leave a message. Let's learn together. Of course, as for how to apply it to the company's projects, this depends on your friends' mastery of parameter correlation and global variables and the actual situation of the project. Thank you. Reading, I hope this article will be helpful to you.

Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
 

Insert image description here

Guess you like

Origin blog.csdn.net/IT_LanTian/article/details/132839657