代码改变世界

论《unittest框架中测试用例依赖的解决》重要性

2020-03-10 21:35  那个杰克  阅读(1216)  评论(1编辑  收藏  举报

论一下unittest测试框架间测试用例依赖的重要性,是值得每一个自动化开发工程师,必修的武功宝典了,且解决方法的姿势也有千差万别,且跟我细细品尝。

使用公共方法,在其他用例获取Token值
class InvestTests(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        pass

    @classmethod
    def tearDownClass(self):
        pass

    def setUp(self):
        self.baseurl = "http://218.17.157.55/:18087"
        self.headers = {"Content-Type":"application/json"}

    def tearDown(self):
        pass
    
    def getToken(self,username):
        url = self.baseurl + "/pj-common/login"
        payload = {"username":username,"password":"123456w","imageCode":"111111","clientType":"WEB"}
        response = requests.post(url, headers=self.headers,json=payload)
        rsp = response.json()
        if rsp:
          return  rsp["data"]["token"]
            
    def test_invest(self):
        #获取Token
        token = self.getToek(username='123')
        #出借
        try:
            if token:
                self.headers.update({"Authorization": "Bearer %s" % self.token})
                url = self.baseurl + "/pj-p2p-core/appFront/planInvest/v3.0/submitInvestOrder"
                payload = {"investAmt":int(300),"couponId":-1,"planId":int(49)} # 出借30天产品
                response = requests.post(url,headers=self.headers,json=payload)
                if response.status_code == 200:
                    print("出借成功1笔")
                else:
                    print("出借失败1笔")   
            else:
                print("没有获取到token!!!")
        except Exception as e:
            print(e)
        
优缺点:
优点是用例需要的token参数,不会受到其他用例的影响
确定是每次请求动作,都需要执行一次登录获取token的接口,对接口的请求频繁

使用全局变量
# 全局变量,解决用例间的依赖
global token
class InvestTests(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        pass

    @classmethod
    def tearDownClass(self):
        pass

    def setUp(self):
        self.baseurl = "http://218.17.157.55:18087"
        self.headers = {"Content-Type":"application/json"}

    def tearDown(self):
        pass

    def getToken(self,username):
        url = self.baseurl + "/pj-common/login"
        payload = {"username":username,"password":"123456w","imageCode":"111111","clientType":"WEB"}
        response = requests.post(url, headers=self.headers,json=payload)
        rsp = response.json()
        global token
        if rsp:
            print(rsp["data"]["token"])
            token = rsp["data"]["token"] #需要传出去的参数 -- 设置成全局变量

    def test_001_login_200(self):
        self.getToken(username='13715384224')


    def test_002_invest_200(self):
        
        #出借
        try:
            if token:
                self.headers.update({"Authorization": "Bearer %s" % token})
                url = self.baseurl + "/pj-p2p-core/appFront/planInvest/v3.0/submitInvestOrder"
                payload = {"investAmt":int(300),"couponId":-1,"planId":int(49)} # 出借30天产品
                for num in range(int(1)):
                    response = requests.post(url,headers=self.headers,json=payload)
                    if response.status_code == 200:
                        print("出借成功1笔")
                    else:
                        print("出借失败1笔")
            else:
                print("没有获取到token!!!")
        except Exception as e:
            print(e)


使用setUpClass方法和类方法

# 使用classmethod方法,执行unittest框架时只获取一次token,解决用例间的依赖

class InvestTests(unittest.TestCase):

    @classmethod
    def getToken(cls,username):
        url = cls.baseurl + "/pj-common/login"
        payload = {"username":username,"password":"123456w","imageCode":"111111","clientType":"WEB"}
        response = requests.post(url,json=payload)
        rsp = response.json()
        if rsp:
            print(rsp["data"]["token"])
            return rsp["data"]["token"] #需要传出去的参数 


    @classmethod
    def setUpClass(cls):
        cls.baseurl = "http://218.17.157.55:18087"
        cls.headers = {"Content-Type":"application/json"}
        cls.token = cls.getToken(username='13715384224')

    @classmethod
    def tearDownClass(self):
        pass

    def setUp(self):
        self.baseurl = "http://218.17.157.55:18087"
        self.headers = {"Content-Type":"application/json"}

    def tearDown(self):
        pass

   

    def test_001_login_200(self):
        pass



    def test_002_invest_200(self):
        
        #出借
        try:
            if self.token:
                self.headers.update({"Authorization": "Bearer %s" % self.token})
                url = self.baseurl + "/pj-p2p-core/appFront/planInvest/v3.0/submitInvestOrder"
                payload = {"investAmt":int(300),"couponId":-1,"planId":int(49)} # 出借30天产品
                for num in range(int(1)):
                    response = requests.post(url,headers=self.headers,json=payload)
                    if response.status_code == 200:
                        print("出借成功1笔")
                    else:
                        print("出借失败1笔")
            else:
                print("没有获取到token!!!")
        except Exception as e:
            print(e)