python notes 39-unittest framework on how to return the results to the next interfaces interfaces applicable (the interview will be asked)

Foreword

Interview must be asked: how the results will be returned on interfaces as the next request interface to the Senate? When used to write the frame unittest cases, how to use the results of a embodiment of a use case b.
Example unittest with each frame are independent, then the test data sharing, need to set a global variable, you may be used Globals () function to solve

globals () function

globals () function returns the current location to a dictionary of all global variables.

print(globals())

# 用法
globals()["a"] = "用例a的返回结果"

# 用例b引用
b = globals()["a"]
print(b)

unittest frame for Example

So Globals () function sharing data between cases, such embodiments use a returned result written globals () [ "a"], treated with Example b call globals () [ "a"] value

import unittest
import requests

class TestA(unittest.TestCase):
    def setUp(self):
        self.s = requests.session()

    def test_a(self):
        '''用例a'''
        result_a = "aaaaaa"    # 用例a的返回值

        # 返回值先给全部办理,存到字典对应key
        globals()["a"] = result_a
        self.assertEqual(result_a, "aaaaaa")

    def test_b(self):
        '''用例b'''
        b = globals()["a"]  # 引用用例a的返回值
        print("用例b引用用例a的返回值:%s"%b)
        result_b = b+"111"
        self.assertEqual(result_b, "aaaaaa111")

if __name__ == '__main__':
    unittest.main()

operation result:

E:\python36\python.exe D:/jiekou9/debug/yilai.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK
用例b引用用例a的返回值:aaaaaa

But this there will be a risk: b is used with the embodiment of a use case, the use case sequence with a first embodiment are performed by the embodiment b

globals () optimization

When used in Example c of the request parameter dependent Examples a and use case b with using too much Globals () will be very good correlation, can Globals () function into setUp front inside, as follows:
Use Case c relies on a return and use case b value result_a return value result_b

import unittest
import requests

class TestA(unittest.TestCase):
    '''上海悠悠:QQ群:779429633'''
    def setUp(self):
        self.s = requests.session()
        self.g = globals()

    def test_a(self):
        '''用例a'''
        result_a = "aaaaaa"    # 用例a的返回值

        # 返回值先给全部办理,存到字典对应key
        self.g["a"] = result_a
        self.assertEqual(result_a, "aaaaaa")

    def test_b(self):
        '''用例b'''
        b = self.g["a"]  # 引用用例a的返回值
        print("用例b引用用例a的返回值:%s"%b)
        result_b = b+"111"
        self.g["b"] = result_b
        self.assertEqual(result_b, "aaaaaa111")

    def test_c(self):
        '''用例c'''
        print("用例c依赖用例a和用例b")

        c_a = self.g["a"]
        c_b = self.g["b"]
        print("用例c的请求入参:%s" % c_a)
        print("用例c的请求入参:%s" % c_b)

if __name__ == '__main__':
    unittest.main()

operation result

.用例b引用用例a的返回值:aaaaaa
.用例c依赖用例a和用例b
用例c的请求入参:aaaaaa
用例c的请求入参:aaaaaa111
.
----------------------------------------------------------------------
Ran 3 tests in 5.001s

OK

python exchange QQ group: 779 429 633

Guess you like

Origin www.cnblogs.com/yoyoketang/p/11241473.html