hincky的主页 hincky的主页
  • 2023

    • nginx
    • prometheus
    • 小工具
    • 部署
  • 数据库

    • MySQL是怎么使用/运行的
    • Redis核心技术与实战
  • 极客时间

    • Web协议详解与抓包实战
    • SQL必知必会
    • MySQL45讲
个人日常
  • 分类
  • 标签
  • 归档
  • 随笔
GitHub (opens new window)

Hincky

当有趣的人,做想做的事
  • 2023

    • nginx
    • prometheus
    • 小工具
    • 部署
  • 数据库

    • MySQL是怎么使用/运行的
    • Redis核心技术与实战
  • 极客时间

    • Web协议详解与抓包实战
    • SQL必知必会
    • MySQL45讲
个人日常
  • 分类
  • 标签
  • 归档
  • 随笔
GitHub (opens new window)
  • 2022

  • 2023

    • 一月

    • 五月

      • 05-04
        • UI测试
        • 接口测试
          • demo
        • 工作计划拆解
          • 测试准备工作
      • 05-08
        • pytest
          • pytest基本的测试用例规则
          • pytest运行方式
        • pycharm创建测试项目
          • 准备
          • 新建包和模块
        • 装饰器
          • 前后置处理
          • fixture实现部分前后置
      • 05-10
        • APIPost自动化测试
          • 测试用例
          • 测试套件
          • 测试报告
      • 良主测试流程
        • 测试流程安排
          • 个人中心
          • 卖闲置
    • nginx

    • prometheus

    • tools

    • http状态码

    • 监控

    • Go

    • python

    • linux性能优化

    • PHP

    • elastic

    • chatgpt

    • 小工具

    • js

    • django

    • centos7.6配置

    • 部署

    • 部署资源整理

    • MySQL数据库

  • 工作记录
  • 2023
  • 五月
hincky
2023-05-08
目录

05-08

测试软件分类:

  • python:unittest,pytest
  • java:testing,junit

作用:

  1. 发现测试用例
  2. 执行测试用例
  • pytest:默认从上到下执行,可以通过装饰器来改变规则
  • unittest:默认按照asc||顺序执行
  1. 判断测试结果:断言
  2. 生成测试报告:pytest-html,allure报告

# pytest

  1. 用例管理框架(单元测试框架),可以和selenium,requests,appium结合实现自动化测试
  2. 实现用例跳过skip和reruns失败用例重跑
  3. 结合allure-pytest插件生成allure报告
  4. 和jenkins实现持续集成
  5. 丰富的插件:
  • pytest-html 生成html测试报告
  • pytest-xdist 多线程执行测试用例
  • pytest-ordering 改变测试用例的执行顺序
  • pytest-rerunfailures 失败用例重跑
  • allure-pytest 生成allure报告

# pytest基本的测试用例规则

  1. 模块名必须以test_开头
  2. 测试类必须以Test开头,并且不能带有init方法
  3. 测试用例必须以test_开头

命名规范

  1. 模块名:一般名称全小写,单词之间有下划线分隔,test_api.py
  2. 类名:大写开头,驼峰命名,TestApi
  3. 方法名:一般全小写,多个单词之间用下划线分隔

# pytest运行方式

  1. 主函数方式:就是在根目录下通过一个all.py发现并运行所有测试用例模块
  • 常用参数:
    • -v 输出更加详细的信息,比如文件和用例名等
    • -s 输出调试信息,打印信息等
    • -vs 上面两者合并
    • --reruns n:失败重跑n次
    • -X 出现一次失败就停止
    • --maxfail=n 出现n个失败就终止测试
    • --html=reports/report.html 生成html测试报告(一般生成到repots包下,根目录下新建一个reports包)
    • -n=m 利用m个线程去运行
    • -m=smoke 执行标记为smoke的用例
  • 不常用参数
    • -k 运行测试用例名称中包含指定的字符串的用例
  • 指定运行特定模块
if __name__ == '__main__':
    pytest.main('testcases/test_api.py')
1
2
  • 指定运行特定包
if __name__ == '__main__':
    pytest.main('testcases/')
1
2
  • 指定运行特定node
  1. 命令行方式
  • pytest
  1. 通过pytest.ini的配置文件运行,根目录下创建pytest.ini,对应修改默认配置

# pycharm创建测试项目

# 准备

  1. 新建测试项目pytestdemo
  2. 为项目新建一个专有的虚拟python环境,不影响系统的python环境
  • python -m venv D:\workspace\pytestdemo
  • .\Scripts\activate
  1. 在项目根目录下创建requirements.txt插件目录,内容如下
pytest-html
pytest-xdist
pytest-ordering
pytest-rerunfailures
allure-pytest
1
2
3
4
5
  1. 为项目安装插件,并使用list确认是否已经安装成功
  • pip install -r requirements.txt
  • pip list

# 新建包和模块

  1. 根目录下新建包,名为testcases
  2. testcases包下新建test_api.py模块
import pytest


class TestApi:
    @pytest.mark.run(order=2)  # 改变执行顺序
    @pytest.mark.smoke
    def test_hincky(self):
        print("this is hincky")

    @pytest.mark.run(order=1)  # 改变执行顺序
    @pytest.mark.productMange
    def test_xiaotong(self):
        print("this is xiaotong")
1
2
3
4
5
6
7
8
9
10
11
12
13
  1. 创建根目录all.py测试入口
import pytest

if __name__ == '__main__':
    # pytest.main() # 不指定模块的时候,默认执行根目录下所有的测试模块,可以'test_api.py'指定运行当前测试模块
    # pytest.main(['-vs','--reruns=2']) # 失败就重跑2次
    # pytest.main(['-vs','-x']) # 出现错误就停止
    # pytest.main(['-vs','-maxfail=1','--html=reports/report.html']) # 失败1次就停止,并在reports包下生成html测试报告
    # pytest.main(['-vs','-k','xiaotong']) #只运行xiaotong的测试用例,前提是命名要规范
    # pytest.main(['-vs','-k','xiaotong and hincky']) #只运行xiaotong和hincky的测试用例
    # pytest.main(['-vs','-k','xiaotong or hincky']) #只运行xiaotong或hincky的测试用例
    pytest.main()

1
2
3
4
5
6
7
8
9
10
11
12
  1. 创建根目录pytest.ini配置文件
[pytest]

;addopts = -vs --html reports/report.html # 指定命令行参数

; 指定命令行参数
addopts = -vs -m smoke

;指定测试用例的范围/具体模块文件
testpaths = testcases/

; 修改模式测试用例的匹配前缀
python_files = test_*.py
python_classes = Test*
python_functions = test_*

; 用例分组 需要去用例里面进行标记,即上面用例里面的 @pytest.mark.smoke   @pytest.mark.productMange
markers =
    smoke: 冒烟用例(即正例)
    productMange: 分模块管理,这里用商品模块举例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 装饰器

利用装饰器来实现:

  • 跳过执行
  • 改变顺序
  • 给用例加上特定标签
version = 3

@pytest.mark.skip(reason="粒度不需要")       # 无条件跳过,括号里面是自定义的跳过提示说明
@pytest.mark.skipif(version>2, reason="2版本以下的都不执行") # 有条件跳过,括号里面是条件
@pytest.mark.run(order=1)  # 改变执行顺序
@pytest.mark.productMange  # 给用例加上productMange的标签
def test_xiaotong(self):
    print("this is xiaotong")
1
2
3
4
5
6
7
8

# 前后置处理

用例级别

# 前置处理
def setup(self):
    print("执行每个测试用例之前,web自动化:打开浏览器,加载网页;接口自动化:日志开始")

# 后置处理
def teardown(self):
    print("在每个用例之后执行,销毁日志,关闭浏览器")
1
2
3
4
5
6
7

类级别

# 前置处理
def setup_class(self):
    print("执行每个类之前,创建日志对象,创建数据库连接")

# 后置处理
def teardown_class(self):
    print("在每个类之后执行,销毁日志对象,关闭数据库连接")
1
2
3
4
5
6
7

模块级别(就是.py文件,用得不多)

# 前置处理
def setup_module(self):
    print("执行每个模块之前,创建日志对象,创建数据库连接")

# 后置处理
def teardown_module(self):
    print("在每个模块之后执行,销毁日志对象,关闭数据库连接")
1
2
3
4
5
6
7

# fixture实现部分前后置

语法:

@pytest.fixture(scope="作用域".params="数据驱动".autouser="自动执行",ids="自定义参数名称",name="别名")
1

fixture里面的function级别前后置操作,并如何调用

# function级别
def execute_sql():  # 前置操作
    print("执行数据库的验证,查询数据库")
    yield           # 后置操作
    print("关闭数据库连接")

# function级别的调用
def test_hincky(self, execute_sql):
    print("this is hincky , fixture funciton level")
1
2
3
4
5
6
7
8
9

ZINC_FIRST_ADMIN_USER=jiuyu ZINC_FIRST_ADMIN_PASSWORD=jiuyu_zinc363 ./zincsearch

编辑 (opens new window)
05-04
05-10

← 05-04 05-10→

最近更新
01
集成chatgpt的工具
05-24
02
修改服务器ssh默认连接端口
05-22
03
阿里云免费证书
05-15
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Hincky | MIT License | 粤ICP备2022120427号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式