• 015 python接口 自动化测试


    '''
    时间:2018/11/12
    目录: 
      一: 自动化测试
      二: 数据格式  
    '''

    一: 自动化测试

    1 : 建立工程目录。

    # coding:utf-8
    import requests
    import time
    from test_018.login_chandao import Login
    host = "http://127.0.0.1"
    
    class AddBug():
        def __init__(self, s):
            self.s = s
    
        def up_load(self,
                    jpg_path = "C:\001.png",
                    file_name = "001.png",
                    file_type = "imgage/jpeg"):
            '''
            上传文件
            :param jpg_path:
            :param file_name:
            :param file_type:
            :return:
            '''
            url = host + "/zentao/file-ajaxUpload-5be120e20b5af.html?dir=image"
            with open(jpg_path, "rb") as fp:
                f = {
                    "imgFile": (file_name, fp.read(), file_type),
                    "localUrl": (None, file_name)
                }
                fp.close()
    
            # 上传图片,或者文件, files=参数
            r = self.s.post(url, files=f)
    
            try:
                print(r.json())
                return r.json()["url"]
            except:
                print("上传文件失败: %s" %r.text)
                return ""
    
        def add_bug(self, jpg_url=None, title=None):
            '''
            添加BUG
            :param jpg_url:
            :param title:
            :return:
            '''
            url = host + "/zentao/bug-create-1-0-moduleID=0.html"
            timestemp = str(time.time())
            if title == None:
                t = "title_%s" %timestemp
            else:
                t = title
    
            if jpg_url == None:
                jpg = "/zentao/data/upload/1/201811/071737170455846p.png"
            else:
                jpg = jpg_url
            body = {
                    "product": "1",
                    "module": "0",
                    "project": "",
                    "openedBuild[]": "trunk",
                    "assignedTo": "admin",
                    "type": "codeerror",
                    "os": "",
                    "browser": "",
                    "color": "",
                    "title": t,
                    "severity": "3",
                    "pri": "0",
                    "steps": '<p>[步骤]</p> 
                             <p><img src="%s" alt="" /></p> 
                             <p><br /> 
                             </p> 
                             <p><br /> 
                             </p> 
                             <p>[结果]</p> 
                             <p>[期望]</p>' %jpg_url,
                    "story": "0",
                    "task": "0",
                    # "mailto[]": "",
                    # "keywords": "",
                    # "files[]": "",
                    # "labels[]": "",
                    "uid": "5be120e20b5af",
                    "case": "0",
                    "caseVersion": "0",
                    "result": "0",
                    "testtask": "0"
            }
    
            r = self.s.post(url, data=body)
            print("添加BUG: %s" %r.text)
            return r.text
    
        def is_add_bug_succes(self, result):
            '''
            判断是否成功 - 添加BUG
            :param result: 返回数据 - 服务器
            :return:
            '''
            return "self.location=" in result
        
    if __name__ == '__main__':
        s = requests.session()
        zen = Login(s)
        zen.login()
    
        bug = AddBug(s)
        jpg_url = bug.up_load()
    
        bug.add_bug(jpg_url, "title")

    1 : chandao_add_bug.py

    # coding:utf-8
    import requests
    
    
    host = "http://127.0.0.1"
    
    class Login():
        def __init__(self, s):
            self.s = s
    
        def login(self, user="admin", password="123456"):
            '''
            禅道登录
            :param user:        登录用户
            :param password:    登录密码
            :return:            返回数据 - 服务端
            '''
    
            url = host + "/zentao/user-login.html"
            head = {"Content-Type": "application/x-www-form-urlencoded"
                    }
            body = {"account": user,
                    "password": password,
                    "referer": "http://127.0.0.1/zentao/my/",
                    "keepLogin[]": "on",
            }
            r = self.s.post(url, headers=head, data=body)
            print(r.status_code)
            print(r.content.decode("utf-8"))
            return r.content.decode("utf-8")
    
        def decide_login_success(self, result):
            '''
            判断结果
            :param result:  返回数据 - 服务端
            :return:        True - 登录成功
                             False - 登录失败
            '''
    
            if "parent.location=" in result:
                print("登录成功!")
                return True
            elif "登录失败,请检查您的用户名或密码是否填写正确" in result:
                print("登录失败,用户名或密码不对")
                return False
            else:
                print("登录失败,其它问题:%s" %result)
                return False
    
    if __name__ == '__main__':
        s = requests.session()
        zentao = Login(s)
        result = zentao.login()             # 登录禅道
        zentao.decide_login_success(result) # 判断结果

    1 : login_chandao.py

    # coding:utf-8
    import requests
    import unittest
    from test_018.login_chandao import Login
    from test_018.chandao_add_bug import AddBug
    
    class TestAddBug(unittest.TestCase):
        ''' 测试用例 - 添加BUG '''
        def setUp(self):
            self.s = requests.session()
            self.s.verify = False
            self.zen = Login(self.s)
            self.bug = AddBug(self.s)
            self.zen.login()
    
        def tearDown(self):
            self.s.close()
    
        def test_upload(self):
            ''' 测试用例 - 文件上传 '''
            jpgurl = self.bug.up_load()
            self.assertTrue(jpgurl != "")
    
        def test_add_bug(self):
            jpgurl = self.bug.up_load()
            result = self.bug.add_bug(jpgurl)
            actucl_result = self.bug.is_add_bug_succes(result)
            self.assertTrue(actucl_result)
    
    if __name__ == '__main__':
        unittest.main()

    1 : test_case_chandao.py


    二: 数据格式

    par = (
        ("a", "111"),
        ("a", "xxxx"),
        ("b", "1222"),
    )
    
    
    body = [
        ["a", "xxxx"],
        ["b", "1222"],
    ]
    import json
    
    jsondata = {"a": "111",
                "b": "222",
                "c": "cccc"}
    body2 = {
        "a": "111",
        "b":  json.dumps(jsondata)
    }
    r = s.post(url, data=body2)
  • 相关阅读:
    ---Install Oracle Java 11 SE under Ubuntu
    ---个人英语单词收集!
    ---Android alarm使用
    ---FLAG_NO_CREATE 的用途!
    ---Englist Word Memo
    ---Ubuntu安装后要做的几件重要的事情 (适合Ubuntu机子的开发人员)
    ---Android egl/egls 概念
    --- Checking fs of the mounted partitions on Android device
    移动应用论坛——如何“玩赚”微信
    Android复制assets目录下的图片到内存
  • 原文地址:https://www.cnblogs.com/huafan/p/9949491.html
Copyright © 2020-2023  润新知