pytest +  登录禅道 :自动提交bug-编辑bug-确认bug-解决bug-关闭bug

一、Pycharm中创建项目结构

1.新建一个工程,工程名称自己定义,如:zentao

2.在工程的根目录下新建一个conftest.py(测试用例的一些fixture配置)和pytest.ini(改变pytest的运行方式)

3.在工程下创建以下package包:

--case:这个包放test开头的测试用例,也可以放一些非test开头的封装接口方法

--common:这个包放一些公共的方法,如:读取excel文件方法,读取mysql、oracle的脚本

--config:放一些配置文件,如邮箱的一些参数:收件人,发件人,密码等

4.在工程下创建以下文件夹:

--logs:这里存放日志信息

--report:这里存放测试报告

二、开始写脚本

思考:1.登录之后,需要提bug,然后操作bug,如何保持登录了?s=requests.session()可以实现保持会话;

   2.假如登录,提bug,操作bug,都写在一个.py文件,可以使用s.get(...),s.post(...)操作所有请求,那若是这些操作在不同的.py文件呢?fixture自定义测试用例前置条件可以实现; 作用范围session(多个文件调用一次,可以跨.py文件调用),conftest.py文件在当前所在目录及以下目录生效;

   3.若是请求地址写死,线下线上切换不方便,怎么办?当然fixture可以搞定了

2.1 由此,conftest.py脚本的内容确定了,如下:

#注意这里的ip改为自己服务器的ip哦

#conftest.py
import requests
import pytest
@pytest.fixture(scope="session")
def s():
ss=requests.session()
return ss
@pytest.fixture(scope="session")
def host():
host="http://47.98.66.11:8899"
return host

2.2 编写登录的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# coding:utf-8
#test_login.py
import re
import pytest
import hashlib
import allure
 
@allure.severity("blocker")
@allure.epic("禅道登录,增删改查测试")
@allure.feature("禅道登录")
@allure.issue("http://47.98.66.11:8899/bug-browse-1-0-all.html")
class Test_login:
    @pytest.fixture()
    def getrand(self,s,host):
        while True:
            = s.get(host+"/user-login.html")
            # print(r.text)
            rand = re.findall("id='verifyRand' value='(.+?)'", r.text)
            #print(rand)
            if len(rand[0])==10:
                #break
                print(rand[0])
                return rand[0]
 
    @pytest.fixture()
    def md5(self,getrand):
        first=hashlib.md5("P@ssw0rd".encode('utf-8')).hexdigest()+getrand
        print(first)
        pwd=hashlib.md5(first.encode('utf-8')).hexdigest()
        print(pwd)
        return (getrand,pwd)
 
    @allure.title("登录用例")
    def test_login(self,md5,s,host):
        data={"account":"admin",
              "password":md5[1],
              "passwordStrength":1,
              "referer":"/",
              "verifyRand":md5[0],
              "keepLogin":1}
        r=s.post(host+"/user-login.html",data=data)
        #print(r.__dict__)
        # print(dir(r))
        #         # print(r.text)
        r1=s.get(host+"/my/")
        #print(r1.text)
        assert '退出' in r1.text

2.3 提交bug

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# coding:utf-8
#test_newbug.py
import pytest
import re
import random
import allure
@pytest.fixture()
def uppic_steps(s,host):#上传图片
    f={"localUrl":"D:\\ch.jpg",
       "imgFile":("ch.jpg",open("D:\\ch.jpg","rb"),"image/jpeg")}
    r=s.post(host+"/file-ajaxUpload-5f37e08f8d109.html?dir=image",files=f)
    #print(r.text)
    print(r.json())
    #picurl=re.findall(',"url":"(.+?)"}',r.text)[0].replace("\\","")
    picurl=r.json()["url"]
    print("上传图片地址:{}".format(picurl))
    return picurl
 
@pytest.fixture()
def get_uid(s,host):#获取uid
    r=s.get(host+"/bug-create-1-0-moduleID=0.html")
    uid=re.findall("kuid = '(.+?)'",r.text)
    return uid
 
@allure.severity("critical")
@allure.title("提bug")
def test_newbug(s,uppic_steps,host,get_uid):
    data={"product":1,
          "module":0,
          "project":1,
          "openedBuild[]":"trunk",
          "assignedTo":"admin",
          "type":"codeerror",
          "title":"接口自动化bug0815_15_附图片、附件:"+str(random.randint(0,1000)),
          "severity":3,
          "pri":3,
          "steps":"<p>[步骤]<img src={} alt="" /></p><br/><p>[结果]</p><br/><p>[期望]</p><br/>".format(uppic_steps),
          "oldTaskID":0,
          "status":"active",
          "uid":get_uid,
          "caseVersion":0,
          "case":0,
          "result":0,
          "testtask":0}
    f={
        ("files[]",("中国气候.jpg",open("D:\\中国气候.jpg","rb"),"image/jpeg")),
        ("labels[]","中国气候.jpg"),
        ("files[]",("hl.jpeg",open("D:\\hl.jpeg","rb"),"image/jpeg")),
        ("labels[]","hl.jpeg")
    }
 
    r=s.post(host+"/bug-create-1-0-moduleID=0.html",data=data,files=f)
    print("提交bug,响应内容:{}".format(r.text))

2.4 编辑bug-确认bug-解决bug-关闭bug

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# coding:utf-8
#test_operate.py
import pytest
from lxml import etree
import re
import time
import allure
import random
 
@pytest.fixture()
def get_id(s,host):#查找未关闭状态的第一条bug的id
    r=s.get(host+"/bug-browse-1-0-unclosed-0-id_desc.html")
    er=etree.HTML(r.text)
    id=er.xpath('//*[@id="bugList"]/tbody/tr[1]/td[1]/a')[0].text #查找第一个的id
    print("提取第一个id为:{}".format(id))
    return id
 
def get_idstatus(s,get_id,host):#根据id,获取bug的状态
    r=s.get(host+"/bug-browse-1-0-all.html")
    er2=etree.HTML(r.text)
    idtitle=er2.xpath('//*[@id="bugList"]/tbody/tr[@data-id={}]/td[6]'.format(get_id))[0].get("title")
    print(get_id,idtitle)
    return idtitle
 
@allure.title("修改bug")
def test_edit(s,get_id,host):
    r=s.get(host+"/bug-edit-{}.html".format(get_id))
    kuid=re.findall("var kuid = '(.+?)'",r.text)
    lasted=re.findall("id='lastEditedDate' value='(.+?)'",r.text)
    data = {"product"1,
            "module"0,
            "project"1,
            "openedBuild[]""trunk",
            "duplicateBug":0,
            "assignedTo""admin",
            "deadline":"0000-00-00",
            "type""codeerror",
            "title""0818bug:" + str(random.randint(01000)),
            "severity"3,
            "pri"3,
            "steps""<p>[步骤]</p><br/><p>[结果]</p><br/><p>[期望]</p><br/>",
            "status""active",
            "lastEditedDate":lasted,
            "uid": kuid,
            "caseVersion"0,
            "comment":"修改bug"}
    s.post(r.url,data=data)
    assert get_idstatus(s, get_id, host)=="激活"
 
@allure.title("确认bug")
def test_confirm(s,get_id,host):
    r=s.get(host+"/bug-confirmBug-{}.html?onlybody=yes".format(get_id))
    kuid = re.findall("var kuid = '(.+?)'", r.text)[0]
    data={
    "assignedTo":"admin",
    "type":"codeerror",
    "pri":3,
    "status":"active",
    "comment":"ok,稍后修复",
    "uid":kuid
    }
    r1 = s.post(r.url, data=data)
    print("确认后,响应内容:{}".format(r1.text))
    print(get_id)
    assert get_idstatus(s, get_id, host) == "激活"
 
@allure.title("解决bug")
def test_resolve(s,get_id,host):#解决bug
    r=s.get(host+"/bug-resolve-{}.html?onlybody=yes".format(get_id))
    kuid=re.findall("var kuid = '(.+?)'",r.text)[0]
    print(type(kuid),kuid,r.url)
    datas={"resolution":"bydesign",
           "resolvedBuild":"trunk",
           "resolvedDate":time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),
           "assignedTo":"admin",
           "status":"resolved",
           "comment":"不予修复",
           "uid":kuid
           }
    r1 = s.post(r.url, data=datas)
    print("修改为已解决,响应内容:{}".format(r1.text))
    print(get_id)
    assert get_idstatus(s,get_id,host)=="已解决"
 
@allure.title("关闭bug")
def test_close(s,host,get_id):#关闭bug
    url=host+"/bug-close-{}.html?onlybody=yes".format(get_id)
    r=s.get(url)
    kuid=re.findall("kuid = '(.+?)'",r.text)
    data={
        "status":"closed",
        "comment":"ok",
        "uid":"kuid"
    }
    r1=s.post(url,data=data)
    print(r1.text)
    assert get_idstatus(s,get_id,host)=='已关闭'

 2.5 为了展示自己的劳动成果,报告是不可少的

1
2
3
4
5
6
7
8
#pytest.ini
 
[pytest]
#pytest-html生成报告
#addopts=-s --html=report/a.html --self-contained-html
 
#allure-pytest 生成报告
addopts=---alluredir report/b

 2.6 zentao文件下,打开cmd,执行pytest

pytest执行完毕,继续输入allure serve report/b 自动展示html格式报告(注意:report/b可以随意命名,但必须与pytest.ini中的内容一致)

注意:pytest可以在zentao目录下执行,也可以在case目录下执行,但这样report/b所在的目录就不同了;根据pytest.ini,执行pytest会在pytest当前所在目录生成report/b目录,若是report/b不存在则直接创建,若是存在则使用已有的;

  执行pytest时,一定要删除b目录,不然可能会产生冗余数据;

三、写代码过程中,想要调试某个脚本;如,提交bug;

那么需要执行登录脚本和提交bug脚本;

在case目录下,打开cmd,执行pytest -s test_login.py test_newbug.py

若是想要调试登录,那直接执行pytest -s test_login.py

原文地址https://www.cnblogs.com/canglongdao/p/13526810.html

pytest + 登录禅道 :自动提交bug-编辑bug-确认bug-解决bug-关闭bug的更多相关文章

  1. 用python实现自动化登录禅道系统 设置定时器自动执行脚本

    由于各种原因,我想试下用python实现自动登录禅道系统,并且每天定时执行.(本人第一次接触自动化,在大佬眼中门槛都没摸到的类型) 首先缕清思路: 1.实现自动登录禅道系统,用selenium实现2. ...

  2. 使用cookies,免密登录禅道(一)

    导言:在做自动化的过程中,很多时候都需要绕过登录验证码来进行测试,可使用cookie 绕过验证码进行登录. 以下以自己搭建的禅道环境登录为例(其他网站也可以同样道理): #coding=gbkimpo ...

  3. jmeter登录禅道案例

    下载jmeter,配置环境变量 变量名:JMETER_HOME 变量值:C:\Program Files\apache-jmeter-2.11 变量名:CLASSPATH 变量值:%JMETER_HO ...

  4. python-自动登录禅道

    from bs4 import BeautifulSoup import hashlib import requests import re from tool.request_data_change ...

  5. post 登录禅道,不成功,无解中

    ]}s.get("http://localhost/zentaopms116/www/misc-checkUpdate-16dd7448451f46bb496a2099b6a9af8c.ht ...

  6. form中的button按钮在IE11中自动提交表单问题导致弹出框关闭之后表单被重置

    最近几天,测试系统,遇到一个兼容性问题,form中有一个button按钮,没有指定type类型,点击按钮弹出框选择值之后回填给form上的一个单行文本框,在IE6.IE7.IE8.IE9.IE10中测 ...

  7. wamp服务下部署禅道或其它项目时访问缓慢的解决办法

    原因其实很简单: WAMP服务默认是不支持外网访问的,如果公司内外网在一起就会引起缓慢甚至超时的问题,直接修改WAPM的配置文件让它可以访问外网即可解决问题.   解决的方法/步骤   1.解决办法: ...

  8. 将BUG管理工具(禅道)部署到服务器(测试服务器、云服务器)

      禅道是一个开源的项目管理软件,用来记录软件项目的开发过程.bug跟踪以及任务分配,它是基于PHP语言开发的.   https://www.zentao.net/download/80111.htm ...

  9. Linux系统(虚拟机)安装禅道

    1.查看linux系统版本 uname -a 2.禅道下载:http://www.zentao.net/download.html,找到要下载的版本,点击进入各平台下载: 3.将下载好的安装包上传到l ...

  10. linux 安装禅道

    1. 查看Linux服务器版本信息 # cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) 2. 禅道开源版安装包下载 # wge ...

随机推荐

  1. k8s集群下node节点使用kubectl命令

    问题描述:The connection to the server localhost:8080 was refused - did you specify the right host or por ...

  2. 在Mac上运行Rainbond,10分钟快速安装

    前言 以往安装部署 Rainbond 的方式都无法绕过 Kubernetes 集群的搭建,无论是作为开发环境还是用于生产交付,部署的过程都非常依赖于服务器或云主机.这在体验 Rainbond 云原生应 ...

  3. kubernetes ingress部署

    ingress概念 ingress与service,deployment同样都是k8s中的一种资源 ingress用于实现域名方式访问k8s内部应用 安装ingress 1. 安装helm: wget ...

  4. scala的基本语法

    区分常量和变量 常量 变量 写一行代码,写多行代码,终端代码 数据类型 byte char short int long float double boolean  数据类型与java相似,但与jav ...

  5. c# HttpWebRequest 解决 请求HTTPS慢

    其实就几行代码 if (strUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { request.Cred ...

  6. Vue3等比例缩放图片组件

    本文由 ChatMoney团队出品 有些情况我们需要在各种刁钻的情况下都要保持图片比例不变,比如用户缩放窗口等改变布局的情况.实现原理就是通过容器的宽度和内边距在保持你想要的比例. 以下是基础功能的组 ...

  7. 高性能版本的零内存分配LikeString函数(ZeroMemAllocLikeOperator)

    继上一篇文章在.NET Core,除了VB的LikeString,还有其它方法吗?(四种LikeString实现分享)分享了四种实现方式,笔者对这四种实现方式,不管是执行性能还是内存分配性能上,都不太 ...

  8. mysql ON DUPLICATE KEY UPDATE 演示

    <e>查询:INSERT INTO member_test(mem_no,flag)VALUE('111','1') 错误代码: 1062Duplicate entry '111' for ...

  9. 百度地图API 循环添加信息窗口问题

    百度地图API循环添加信息窗口,会出现所有消息只显示在第一个窗口的位置的问题.并且信息内容相同 解决方法1 转载自 https://blog.csdn.net/zz_mm/article/detail ...

  10. [ABC347C] Ideal Holidays题解

    [ABC347C] Ideal Holidays题解 原题传送门 原题传送门(洛谷) ​ 题意翻译: ​ 在 \(AtCoder\) 王国中,一个周有 \(A+B\) 天.其中在一周中, \([1,A ...