刚刚做完一个项目,由于这是一个方案项目,而不是产品,所以各种准备很不充分,很多公司的能力不能复用,整个团队又都是新员工,而且有部分实习生,匆忙上马,今天对我的自动化框架做一个回溯

自动化测试框架的选择上,我选择pytest框架,下面是我的示例文件,不是我真正的自动化用例,主要为了给刚入门的小伙伴指引

一、测试项目目录设计

lib目录:存放我的公共的方法

log目录:存放我的测试案例的日志路径

report目录:存放的pytest的执行报告

test_case目录:存放真正要执行的案例

testfile目录:存放我的测试文件

conftest.py文件:pytest的文件,具体可以看我的前一篇博客:https://www.cnblogs.com/bainianminguo/p/14338222.html

python.ini文件:pytest的配置文件,具体可以看我的前一篇博客:https://www.cnblogs.com/bainianminguo/p/13773717.html

run_case.py文件:是执行自动化案例的入口文件

二、pytest的案例如何设计

1、入口函数:run_case.py

# -*- coding: utf-8 -*-
import pytest import os if __name__ == '__main__':
pytest.main(["-v","-s","--html=./report/report.html" ])

  

2、pytest的配置文件:pytest.ini

[pytest]
;addopts=-s --html=report.html --reruns 3 --reruns-delay 2
;--html=./report/report.html
addopts=-s
testpaths = test_case
python_files = test_*.py
python_classes = Test_*
python_functions = test_*
markers =
level1
level2
level3
bvt

  

3、全局共享配置文件:conftest.py文件

# -*- coding:utf-8 -*-
import pytest
from lib import basefunc @pytest.fixture(scope="function",autouse=True)
def setup_function():
print("执行conftest文件")
basefunc.delfile()
yield
print("执行conftest文件")
basefunc.delfile()

  

我的conftest文件中的scope=“function”,autouse=True,所以是每个测试函数都会执行这个函数

yield前面的代码是函数执行前执行的,yield后面的代码是是函数执行后需要执行的代码

4、lib目录下,存储我的公共的方法,包括一些日志模块等

logobj.py

# Auther Bob
# --*--coding:utf-8--*--
import logging
import os # z注册一个全局的日志对象
class GetLogObj(object):
basepath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),"log")
def __init__(self,filename):
self.path = os.path.join(GetLogObj.basepath,filename+".txt") def log(self):
log_obj = logging.getLogger("administrator")
log_obj.setLevel(logging.DEBUG) # 注册一个打印到文件的日志对象
fh = logging.FileHandler(self.path)
fh.setLevel(logging.DEBUG) # 设定日志打印的格式
log_format = logging.Formatter("%(name)s %(message)s %(levelno)s %(thread)d %(process)d %(asctime)s",
datefmt='%m/%d/%Y:%H:%M:%S %p') # 在打印到文件的日志对象中应用日志格式
fh.setFormatter(log_format) # 将打印到屏幕和日志的对象注册到全局的日志对象中
log_obj.addHandler(fh) return log_obj

  

basefunc.py

# -*- coding:utf-8 -*-
import os
import time def addfile(filename):
file = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'testfile',filename + "_" + str(time.time()))
with open(file,mode="a") as f:
for i in range(1,200):
f.write(str(i))
f.write("\n") def getfile():
file = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'testfile')
filelist = []
for file in os.listdir(file):
filelist.append(file)
if "__init__.py" in filelist:
filelist.remove("__init__.py")
return filelist def delfile():
basefile = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'testfile')
for file in os.listdir(basefile):
abspath = os.path.join(basefile,file)
os.remove(abspath)

 

5、test_case目录:这里是我的核心的测试代码

 

6、pytest的知识点1:给案例打标签

在真正的实战中,这里主要是区分案例执行的优先级

7、pytest知识点2:断言

在真正的实战中,断言是必不可缺少的,没有断言的测试案例不是一个真正的测试案例,是没有灵魂的

7、pytest知识点3:函数的前置条件和后置条件

    def setup(self):
print("测试文件1:添加资产::函数级别::前置条件") def teardown(self):
print("测试文件1:添加资产::函数级别::后置条件")

  

这个只对当前的测试文件生效,setup是执行测试函数前执行,teardown在执行测试函数后执行

8、pytest知识点4:类的前置条件和后置条件

    def setup_class(self):
print("测试文件1:添加资产::类::前置条件") def teardown_class(self):
print("测试文件1:添加资产::类::后置条件")

  

这个只对当前的测试文件生效,setup_class是执行这个类前执行,tear_down是执行类后执行

9、pytest知识点5:序列化参数

    @pytest.mark.level2
@pytest.mark.parametrize("filename", ["cui1", "cui2", "cui3", "cui4"])
def test_add_asset_002(self,filename):
time.sleep(2)
flag = False
basefunc.addfile(filename)
for i in basefunc.getfile():
if filename in i:
flag = True
l_obj.log().info("给{filename}文件中添加文件成功".format(filename = filename))
if not flag:
l_obj.log().error("给{filename}文件中添加文件失败".format(filename = filename)) filelist = basefunc.getfile()
assert len(filelist) == 1

  

这里通过@pytest.mark.parametrize去传参数给测试函数,这里传了4个参数,相当于是4个测试案例

三、jenkins结合pytest框架执行测试案例

1、设置执行周期

2、设置执行任务入口

3、查看jenkins配置生效

四、jenkins的邮件配置

1、需要安装一个插件:Email Extension Plugin

2、jenkins全局配置

这里要配置Jenkins的发件人的邮箱地址

这里的密码可不是登陆的密码,而是是在这里,大家千万要注意

3、项目内配置,大家主要圈红的地方

五、测试

1、点击build now

2、查看控制台输出日志

3、检查已经收到具体的邮件

pytest测试框架+jenkins结合pytest+jenkins邮件通知配置的更多相关文章

  1. 『德不孤』Pytest框架 — 1、Pytest测试框架介绍

    目录 1.什么是单元测试框架 2.单元测试框架主要做什么 3.单元测试框架和自动化测试框架有什么关系 4.Pytest测试框架说明 5.Pytest框架和Unittest框架区别 (1)Unittes ...

  2. pytest测试框架 -- 简介

    一.pytest测试框架简介: (1)pytest是python的第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效. (2)pytest框架可以兼容unittest用 ...

  3. Pytest测试框架(五):pytest + allure生成测试报告

    Allure 是一款轻量级.支持多语言的开源自动化测试报告生成框架,由Java语言开发,可以集成到 Jenkins. pytest 测试框架支持Allure 报告生成. pytest也可以生成juni ...

  4. Pytest测试框架(一):pytest安装及用例执行

    PyTest是基于Python的开源测试框架,语法简单易用,有大量的插件,功能非常多.自动检测测试用例,支持参数化,跳过特定用例,失败重试等功能. 安装 pip install -U pytest  ...

  5. Pytest测试框架(二):pytest 的setup/teardown方法

    PyTest支持xUnit style 结构, setup() 和 teardown() 方法用于初始化和清理测试环境,可以保证测试用例的独立性.pytest的setup/teardown方法包括:模 ...

  6. Pytest测试框架(三):pytest fixture 用法

    xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...

  7. Jenkins系列之四——设置邮件通知

    Jenkins持续集成,当我们自动打包部署完,我们可以发送一封邮件给相关的负责人.现介绍一下如何在Jenkins中配置实现邮件通知. 在Jenkins中配置实现邮件通知,Jenkins提供了两种方式的 ...

  8. 高可用服务之Keepalived邮件通知配置

    上一篇博客我们了解了keepalived的架构以及安装.VIP的配置和高可用相关配置,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13634755.html: ...

  9. paip.数据库发邮件通知配置

    paip.数据库发邮件通知配置 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.net/attilax ...

随机推荐

  1. C#自定义控件的应用(数据绑定,属性等)

    刚刚开始程序设计的码农生涯,也许一些开发工具上的控件可以满足我们的需求,但是随之时间的迁移,我们对控件的呈现形式需求越来越多样化,这个时候就需要我们来自定义控件,我是一个刚刚入职没多久的菜鸟,接触软件 ...

  2. linux seccomp使用和原理

    linux seccomp使用和原理 概要 linux的沙箱机制,可以限制进程对系统调用的访问,从系统调用号,到系统调用的参数,都可以检查和限制 有两种模式 SECCOMP_MODE_STRICT, ...

  3. [论文阅读笔记] node2vec Scalable Feature Learning for Networks

    [论文阅读笔记] node2vec:Scalable Feature Learning for Networks 本文结构 解决问题 主要贡献 算法原理 参考文献 (1) 解决问题 由于DeepWal ...

  4. MAC与ARP缓存中毒介绍

    ARP 协议 用于地址解析,请求MAC地址. arp -a 或者 -n 查看ARP缓存表 ls(ARP) 查看scapy里的协议字段 ARP缓存中毒原理 ARP收到ARP请求报文,会将发送方的mac地 ...

  5. Date、SimpleDateFormat以及Calendar

    Date类 毫秒值 java.util.Date:表示日期和时间的类 类Date表示特定的瞬间,精确到毫秒 日期转毫秒,号秒转日期 中国属于东八区,会把事件增加8个小时 1天 = 4 * 60 * 6 ...

  6. win7安装oracle11g和oracle client和pl/sql

    一.安装oracle11g 1.下载Oracle 11g R2 for Windows的版本 下载地址:hhttps://www.oracle.com/technetwork/database/ent ...

  7. Linux find 命令的初步实现(C++)

    Implement a myfind command following the find command in UNIX operating system. The myfind command s ...

  8. 深入理解nodejs中的异步编程

    目录 简介 同步异步和阻塞非阻塞 javascript中的回调 回调函数的错误处理 回调地狱 ES6中的Promise 什么是Promise Promise的特点 Promise的优点 Promise ...

  9. Hbase 手动执行MajorCompation

    说明: Major Compaction 的作用: 1.将一个Region下的所有StoreFile合并成一个StoreFile文件 2.对于删除.过期.多余版本的数据进行清除 由于MajorComp ...

  10. WIN7系统没有USB驱动和以太网驱动如何操作

    |  欢迎关注个人公众号  zclinux_note  第一时间获取关于linux使用的技巧.探索Linux的奥秘   | 今天在单位安装了一台win7纯净版,但是安装完成后发现usb没有反应,插上网 ...