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

Foreword

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 1

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):
        '''Query weather interface test case'''
        url = 'http://apis.juhe.cn/simpleWeather/query'
        data = {
            'city': 'Shanghai',
            'key': 'xxxxxxxxxxxx'
        }
        r = requests.post(url, data=data).json()
        info = r['result']['realtime']['info']
        print(info)
        return info

    def test_02(self):
        '''Query dictionary test case'''
        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.

Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected.
If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up.
You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc.
Share their experience, and also share many live lectures and technical salons
You can learn for free! Focus on it! Open source! ! !
QQ group number: 110685036

Method 2

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):
        '''Query weather interface test case'''
        global info
        url = 'http://apis.juhe.cn/simpleWeather/query'
        data = {
            'city': 'Shanghai',
            'key': 'xxxxxxxxxxx'
        }
        r = requests.post(url, data=data).json()
        info = r['result']['realtime']['info']
        print(info)

    def test_02(self):
        '''Query dictionary test case'''
        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():
    '''Query weather interface test case'''
    url = 'http://apis.juhe.cn/simpleWeather/query'
    data = {
        'city': 'Shanghai',
        'key': 'xxxxxxxx'
    }
    r = requests.post(url, data=data).json()
    # Get the desired data through regular expressions
    a = re.findall("'info': '(.*?)', 'wid': '00', ", str(r))
    info = globals()['info'] = a[0]
    print(info)

def test_02():
    '''Query dictionary test case'''
    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.

Summary

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 has 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.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeWeb crawlerSelenium345777 people are learning the system