软测05/12&13|记录一下学习过程|pytest

2022/05/12&13学习内容
整理时间:2022/05/14
参考资料:https://www.bilibili.com/video/BV1NM4y1K73T?p=1&spm_id_from=333.851.header_right.history_list.click

进入新阶段-pytest

pytest介绍与安装

1.pytest测试工具:测试函数、类、方法能否正常运行完成
2.pytest特点
3.pytest安装

pytest基本使用

1.运行方式

pytest配置文件

test_py1.py
1.pytest默认规则
2.配置pytest.ini文件


def test_a():
    print("test_a")
    return 1 * 0

def test_b():
    print("test_b")
    return 1 / 0

def demo_a():
    print("demo_a")
    return 1 * 0

def demo_b():
    print("demo_b")
    return 1 / 0


[pytest]

addopts = -s

testpaths = ./

python_files = test_* *test.py

python_classes = Test_*

python_functions = test_*

pytest断言

test_py3.py
1.作用:
测试执行结果是否符合预期


import pytest

def test_a():
    print("test_a")
    assert 1 == 0

def test_b():
    print("test_b")
    assert "h" in "hello"

if __name__ == '__main__':
    pytest.main(["-s", "test_py3.py"])

pytest标记功能mark

test_py4.py
1.标记跳过
2.标记失败
:预期会异常的测试


import pytest

def test_a():
    print("test_a+++++++++++")
    return 1 + 10

@pytest.mark.skip(reason="想跳过")
def test_b():
    print("test_b+++++")
    return 1 / 0

@pytest.mark.xfail(raises=ZeroDivisionError)
def test_c():
    print("test_c+++++")
    return 1 / 0

if __name__ == '__main__':
    pytest.main(["-s", "test_py4.py"])

pytest参数化

test_py5.py
1.添加collections
2.runner
3.参数分析
4.运行结果


import pytest

@pytest.mark.parametrize(["a", "b"], [(1, 2), (90, 100), (10, 200)])
def test_a(a, b):
    print("test_a+++++++++++")
    assert a + b > 100

if __name__ == '__main__':
    pytest.main(["-s", "test_py5.py"])

pytest夹具fixture

1.夹具fixture作用
2.python示例–test_py6.py


import pytest

def setup_module(args):
    print("setup_moudle", args)

def teardown_module(args):
    print("teardown_module", args)

def setup_function(args):
    print("setup_function", args)

def teardown_function(args):
    print("teardown_function", args)

def test_func_a():
    print('-----', "test_func_a")

def test_func_b():
    print('-----', "test_func_b")

class Test_A():
    TAG = "a"

    def setup_class(self):
        print("     ", self.TAG + "setup_class")

    def teardown_class(self):
        print("      ", self.TAG + "teardown_class")

    def setup_method(self, args):
        print("     ", self.TAG + "setup_method", args)

    def teardown_method(self, args):
        print("     ", self.TAG + "teardown_methods", args)

    def setup(self):
        print("     ", self.TAG + "setup")

    def teardown(self):
        print("     ", self.TAG + "teardown")

    def test_1(self):
        print('-----', self.TAG + 'test_1')

    def test_2(self):
        print('-----', self.TAG + 'test_2')

if __name__ == '__main__':
    pytest.main(["-s", "test_py6.py"])

pytest装饰器夹具

test_py7.py
1.自定义夹具
2.使用夹具
3.参数化使用夹具


import pytest

@pytest.fixture()
def before():
    print("before")

@pytest.fixture()
def login():
    print("login")
    return "user"

@pytest.mark.usefixtures("before")
def test_1():
    print('test_1()')

def test_2(login):
    print('test_2()', login)

@pytest.fixture(params=[1 ,2, 3])
def init_data(request):
    print("request参数是", request.param)
    return request.param

def test_Data(init_data):
    assert init_data>2

if __name__ == '__main__':
    pytest.main(["-s", "test_py7.py"])

pytest插件使用

1.安装插件
2.生成测试报告
3.指定执行顺序
4.失败重试

Original: https://blog.csdn.net/weixin_49079904/article/details/124764152
Author: Joy_ou悠
Title: 软测05/12&13|记录一下学习过程|pytest

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/776359/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • day08 pickle实力

    day08 pickle实力 原创 wx5e6caa8b9792d2022-08-01 17:06:03博主文章分类:Python自动化开发 ©著作权 文章标签 git 文章分类 …

    Python 2023年5月24日
    075
  • Flask生产环境部署指南 | 复杂度– 稳定性++ 性能++

    自上次用flask搭了个接口跑yolo以后,一直都很ok 直到有一天请求数突然多了起来… 只要有1个以上线程请求就崩掉 查了一下原来app.run()是用于开发环境测试…

    Python 2023年8月13日
    049
  • Pandas reindex重置索引

    重置索引(reindex)可以更改原 DataFrame 的行标签或列标签,并使更改后的行、列标签与 DataFrame 中的数据逐一匹配。通过重置索引操作,您可以完成对现有数据的…

    Python 2023年8月16日
    062
  • Python numpy中random函数的使用

    np.random:随机数的生成 *np.random.random() import numpy as np c = np.random.random() #&#x751…

    Python 2023年8月24日
    047
  • scrapy 的入门使用超级详细

    记录一下scrapy的安装和实践操作的流程 1.安装 pip install Scrapy 2.创建scrapy项目 project是蜘蛛名字 scrapy startprojec…

    Python 2023年10月1日
    043
  • pytest框架(三)

    1、简单代码示例 pytest捕获异常with pytest.raise(异常类型):cal.div(1,0) #测试类 class Calculator: def add(sel…

    Python 2023年9月10日
    051
  • 天呐,我居然可以隔空作画了

    摘要:本案例将使用YOLOX、SCNet两个模型,实现一个简单的隔空作画趣味应用 本文分享自华为云社区《ModelBox开发案例 – 隔空作画》,作者:吴小鱼。 本案例…

    Python 2023年10月23日
    050
  • 豆瓣电影Top250数据分析

    此项目是b站上一视频的学习成果,此项目主要技术就是用了python爬虫搜集数据然后用Flask框架、Echarts、WordCloud等技术实现数据可视化 视频地址:Python爬…

    Python 2023年8月10日
    054
  • Django开发笔记

    Django开发笔记 Django学习 * 1. Django安装 – + path()函数 2. 创建项目 – 2.1 终端命令创建 2.2 pychar…

    Python 2023年8月4日
    058
  • 获取rdp保存的凭证

    获取用户保存的rdp凭证 当获取到一台windows服务器,可以尝试获取本地远程连接的信息,如果用户在登入rdp时勾选了 允许为我保存凭证的选项,则在该用户本地会生成一个凭证文件,…

    Python 2023年10月8日
    056
  • pandas的Series和DataFrame

    文章目录 pandas的核心类 Series(数据系列)带标签的数组 * 一、创建Series对象 二、Series索引和切片 三、Series的基本用法 – 1.处理…

    Python 2023年8月18日
    057
  • python+requests+pytest 接口自动化框架(七)

    一、接口自动化框架封装之热加载 热加载:就是在代码执行过程当中动态的调用Python中的方法达到或得动态参数的目的。 1.wx_fun.py(自定义)文件中定义方法,注意返回值需要…

    Python 2023年9月9日
    0107
  • 联邦学习:多任务思想与聚类联邦学习

    1 导引 计算机科学一大定律:许多看似过时的东西可能过一段时间又会以新的形式再次回归。 在联邦学习领域,许多传统机器学习已经讨论过的问题(甚至一些90年代和00年代的论文)都可以被…

    Python 2023年10月29日
    064
  • 白葡萄酒/红葡萄酒质量分析与预测(PCA+MLPClassifier)100%

    白葡萄酒质量数据集 数据来自于:https://scikit-learn.org/stable/modules/preprocessing.html 导包 import numpy…

    Python 2023年8月28日
    061
  • dataframe 设置空值_2019-12-19(三)对DataFrame空值数据记录进行的各种选择(1)

    ”’ 上期回顾: 学习对DataFrame数据记录进行各项选择前的初步和整体的了解。 本次: 我们将学习对DataFrame数据记录进行的各种选择! 因为2…

    Python 2023年8月8日
    074
  • Python 大作业 网易云歌单数据分析及可视化(参考多位博主文章)

    啊哦~你想找的内容离你而去了哦 内容不存在,可能为如下原因导致: ① 内容还在审核中 ② 内容以前存在,但是由于不符合新 的规定而被删除 ③ 内容地址错误 ④ 作者删除了内容。 可…

    Python 2023年8月3日
    063
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球