moanna

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

接口数据有关联,有三种解决方法:1setup 2全局变量(详见以下代码) 3反射
第一种:test_http_quanju.py 全局变量测试用例文件

import unittest
from self_study.test_case.http_request import HttpRequest
TOKEN=None  #全局变量
UUID=None
class TestHttp(unittest.TestCase):
    def setUp(self):
        self.login_url = 'http://api.yesapi.net/api/App/User/Login'
        self.login_data = {"service": "App.User.Login", "return_data": "0",
                "username": "hnh", "password": "3e8c0d71fd528beb8a254731d5ca921a",
                "is_allow_many": "1",
                "app_key": "5FB7DB5AF42166F95453197722D72076",
                "sign": "613B0638F5C1C957006E9488D9937B79"}
        # 查询url
        self.reserch_url = 'http://api.yesapi.net/api/App/User/Profile'
        # #获取登录后的token
        # self.token=HttpRequest().http_request(self.login_url,self.login_data,'post').json()['data']['token']
        # # 获取登录后的uuid
        # self.uuid = HttpRequest().http_request(self.login_url, self.login_data, 'post').json()['data']['uuid']
    def test_login_normal(self):
        res=HttpRequest().http_request(self.login_url,self.login_data,'post')
        global TOKEN#声明全局变量
        global UUID#声明全局变量
        if res.json()['data']['token']: #如果有值则为真,否则为假,如果token有的话,就更新全局变量TOKEN
            TOKEN=res.json()['data']['token']
        global uuid#如果token有的话,就更新全局变量TOKEN
        if res.json()['data']['uuid']: #如果有值则为真,否则为假
            UUID=res.json()['data']['uuid']
        try:
            self.assertEqual(200,res.json()['ret'])
        except AssertionError as e:
            print("test_login_normal's error is{}".format(e))
            raise e
        print("正常登录的结果是:", res.json())
    def test_login_erro_pwd(self):
        data = {"service": "App.User.Login", "return_data": "0",
                "username": "hnh", "password": "3e8c0d71fd528beb8a254731d5ca921a1",
                "is_allow_many": "1",
                "app_key": "5FB7DB5AF42166F95453197722D72076",
                "sign": "613B0638F5C1C957006E9488D9937B79"}
        res = HttpRequest().http_request(self.login_url,data,'post')
        try:
            self.assertEqual(400,res.json()['ret'])
        except AssertionError as e:
            print("test_login_erro_pwd's error is{}".format(e))
            raise e
        print("错误密码登录的结果是:",res.json())
    def test_reserch_normal(self):
        global TOKEN#声明全局变量
        global UUID#声明全局变量
        reserch_data = {"app_key": "5FB7DB5AF42166F95453197722D72076",
                        "service": "App.User.Profile",
                        "return_data": "0",
                        "token": TOKEN,
                        "uuid": UUID}
        res = HttpRequest().http_request(self.reserch_url, reserch_data, 'get')

        try:
            self.assertEqual(200,res.json()['ret'])
        except AssertionError as e:
            print("test_reserch_normal's error is{}".format(e))
            raise e
        print("正常查询的结果是:", res.json())
    # def test_reserch_erro(self):
    def teardown(self):
        pass``

** 接口数据有关联,有三种解决方法:1setup(详见以下代码,每次重新请求下第一条用例) 2全局变量 3反射**
第二种:test_http.py 测试用例文件代码:

import unittest
from self_study.test_case.http_request import HttpRequest
class TestHttp(unittest.TestCase):
    def setUp(self):
        self.login_url = 'http://api.yesapi.net/api/App/User/Login'
        self.login_data = {"service": "App.User.Login", "return_data": "0",
                "username": "hnh", "password": "3e8c0d71fd528beb8a254731d5ca921a",
                "is_allow_many": "1",
                "app_key": "5FB7DB5AF42166F95453197722D72076",
                "sign": "613B0638F5C1C957006E9488D9937B79"}
        # 查询url
        self.reserch_url = 'http://api.yesapi.net/api/App/User/Profile'
        #登录后的token
        self.token=HttpRequest().http_request(self.login_url,self.login_data,'post').json()['data']['token']
        # 登录后的uuid
        self.uuid = HttpRequest().http_request(self.login_url, self.login_data, 'post').json()['data']['uuid']
    def test_login_normal(self):
        res=HttpRequest().http_request(self.login_url,self.login_data,'post')
        try:
            self.assertEqual(200,res.json()['ret'])
        except AssertionError as e:
            print("test_login_normal's error is{}".format(e))
            raise e
        print("正常登录的结果是:", res.json())
    def test_login_erro_pwd(self):
        data = {"service": "App.User.Login", "return_data": "0",
                "username": "hnh", "password": "3e8c0d71fd528beb8a254731d5ca921a1",
                "is_allow_many": "1",
                "app_key": "5FB7DB5AF42166F95453197722D72076",
                "sign": "613B0638F5C1C957006E9488D9937B79"}
        res = HttpRequest().http_request(self.login_url,data,'post')
        try:
            self.assertEqual(400,res.json()['ret'])
        except AssertionError as e:
            print("test_login_erro_pwd's error is{}".format(e))
            raise e
        print("错误密码登录的结果是:",res.json())
    def test_reserch_normal(self):
        reserch_data = {"app_key": "5FB7DB5AF42166F95453197722D72076",
                        "service": "App.User.Profile",
                        "return_data": "0",
                        "token": self.token,
                        "uuid": self.uuid}
        res = HttpRequest().http_request(self.reserch_url, reserch_data, 'get')
        try:
            self.assertEqual(200,res.json()['ret'])
        except AssertionError as e:
            print("test_reserch_normal's error is{}".format(e))
            raise e
        print("正常查询的结果是:", res.json())
    # def test_reserch_erro(self):
    def teardown(self):
        pass

`# 接口数据有关联,有三种解决方法:1setup 2全局变量 3反射(详见以下代码)
第三种:test_http_fanshe.py 反射测试用例文件:

import unittest
from self_study.test_case.http_request import HttpRequest
from self_study.test_case.get_data import GetData
# TOKEN=None  #全局变量
# UUID=None
class TestHttp(unittest.TestCase):
    def setUp(self):
        self.login_url = 'http://api.yesapi.net/api/App/User/Login'
        self.login_data = {"service": "App.User.Login", "return_data": "0",
                "username": "hnh", "password": "3e8c0d71fd528beb8a254731d5ca921a",
                "is_allow_many": "1",
                "app_key": "5FB7DB5AF42166F95453197722D72076",
                "sign": "613B0638F5C1C957006E9488D9937B79"}
        # 查询url
        self.reserch_url = 'http://api.yesapi.net/api/App/User/Profile'
        # #获取登录后的token
        # self.token=HttpRequest().http_request(self.login_url,self.login_data,'post').json()['data']['token']
        # # 获取登录后的uuid
        # self.uuid = HttpRequest().http_request(self.login_url, self.login_data, 'post').json()['data']['uuid']
    def test_login_normal(self):
        res=HttpRequest().http_request(self.login_url,self.login_data,'post')
        # global token#如果token有的话,就更新全局变量TOKEN
        if res.json()['data']['token']: #如果有值则为真,否则为假
            setattr(GetData,'Token',res.json()['data']['token'])
        # global uuid#如果token有的话,就更新全局变量TOKEN
        if res.json()['data']['uuid']: #如果有值则为真,否则为假
            UUID=res.json()['data']['uuid']
            setattr(GetData,'Uuid',res.json()['data']['uuid'])
            #反射用法:setattr(类名,属性名,所取值)--设置属性值
                    #getattr(类名,属性名)--获取属性值
        try:
            self.assertEqual(200,res.json()['ret'])
        except AssertionError as e:
            print("test_login_normal's error is{}".format(e))
            raise e
        print("正常登录的结果是:", res.json())
    def test_login_erro_pwd(self):
        data = {"service": "App.User.Login", "return_data": "0",
                "username": "hnh", "password": "3e8c0d71fd528beb8a254731d5ca921a1",
                "is_allow_many": "1",
                "app_key": "5FB7DB5AF42166F95453197722D72076",
                "sign": "613B0638F5C1C957006E9488D9937B79"}
        res = HttpRequest().http_request(self.login_url,data,'post')
        try:
            self.assertEqual(400,res.json()['ret'])
        except AssertionError as e:
            print("test_login_erro_pwd's error is{}".format(e))
            raise e
        print("错误密码登录的结果是:",res.json())
    def test_reserch_normal(self):
        reserch_data = {"app_key": "5FB7DB5AF42166F95453197722D72076",
                        "service": "App.User.Profile",
                        "return_data": "0",
                        "token": getattr(GetData,'Token'),
                        "uuid": getattr(GetData,'Uuid')}
        res = HttpRequest().http_request(self.reserch_url, reserch_data, 'get')
        try:
            self.assertEqual(200,res.json()['ret'])
        except AssertionError as e:
            print("test_reserch_normal's error is{}".format(e))
            raise e
        print("正常查询的结果是:", res.json())
    # def test_reserch_erro(self):
    def teardown(self):
        pass
posted on 2025-06-01 15:35  木兰花88  阅读(32)  评论(0)    收藏  举报