pytest基础知识一

pytest的介绍

pytest官网:http://www.pytest.org/

pytest是Python的一款单元测试框架,在学习自动化测试过程中,我们最开学习的都是线性脚本,但是当学到一定阶段以及业务复杂度和数据量上来后,我们必须需求一种全新的框架思维来管理和规范我们的测试脚本,从而实现高类聚低耦合的理念。

pytest的作用

  • 单测框架,比 unittest 测试框架更灵活
  • 入门难度低 第三方库丰富性
  • 通用性
  • 与 allure 生成的报告非常的美观
  • 定制性强

pytest的安装与版本查看

安装:		pip install -U	pytest
版本查看:    pytest --version

pytest的基本应用

编写第一个简单的pytest应用

# 被测试函数
def add(a, b):
    return a + b


# 测试脚本
def test_add():
    assert 3 == add(1, 2)

运行结果:

platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-0.13.1
rootdir: E:\Home_Work\Home_Work2\pytest01
plugins: allure-pytest-2.9.43, Faker-8.10.1, cov-2.12.1, forked-1.3.0, html-2.1.1, metadata-1.11.0, rerunfailures-10.1, testconfig-0.2
.0, xdist-2.3.0
collected 1 item                                                                                                                     

test\test_add.py .                                                                                                             [100%]

========================================================= 1 passed in 0.94s =========================================================

E:\Home_Work\Home_Work2\pytest01>

pytest文件和函数命令规则

pytest文件必须以 test开头或者 _ _ test.py 结尾,如 test__addadd_test.py结尾,否则在pytest解释器运行时,文件不能够被收集到。
在这里插入图片描述
pytest文件中测试类命名时,必须用Test开头
pytest文件中方法与函数命名必须要用 test__ 开头,函数没有用__test结尾的说法

def div(a, b):
    return a / b


class TestDiv:
    def test_div(self):
        assert 1 == div(1, 1)

pytest文件的三种运行方式

命令行运行

进入需要执行的Python文件目录下,打开命令行,输入pytest 文件名 如:pytest test_add.py
在这里插入图片描述

PyCharm界面运行

window:
第一步先设置默认运行的框架
在这里插入图片描述
第二步:点击倒三角,执行脚本
在这里插入图片描述

pytest.main()运行pytesr文件

1.删除之前运行过的IDE缓存
在这里插入图片描述
2.使用pytest.mian()运行pytest文件
要将默认框架切回unittest,用Python去运行才会生效
在这里插入图片描述

# 测试脚本
class TestAdd:
    def test_add(self):
        assert 3 == add(1, 2)

    def test_add2(self):
        assert 5 == add(1, 4)


if __name__ == '__main__':
    pytest.main(["TestAdd::test_add2", "-vs"])

在这里插入图片描述

pytest常用的命令行运行参数

在实际生产中,测试用例都会在Jenkins或服务器上运行,那么这时候运行方式都是采用命令行去运行
例如在Jenkins运行接口测试用例
在这里插入图片描述

  1. pytest -k “add” 执行所有测试用例名中含有“add”的用例
  2. pytest - s 打印测试用例中print语句
  3. pytest -v 增加打印细节
  4. pytest - x 一旦发现失败用例,停止运行
  5. pytest -maxfail=2 当测试遇到两条失败,立刻停止运行
  6. pytest -m “标签名” 给测试用例加标签

pytest的框架结构

模块级: setup_module/teardown_module 模块始末,全局的(优先最高)
函数级: setup_function/teardown_function 只对函数用例生效(不在类中)
类级:   setup_class/teardown_class 只在类中前后运行一次(在类中)
方法级: setup_method/teardown_methond 开始于方法始末(在类中)
方法级: setup/teardown 运行在调用方法的前后
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
模块级(setup_module/teardown_module)模块始末,全局的(优先最高)
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
方法级(setup_method/teardown_methond)开始于方法始末(在类中)
类里面的(setup/teardown)运行在调用方法的前后
'''

# 模块级别
def setup_module():
    print("资源准备:setup module")


def teardown_module():
    print("资源准备:teardown module")


def test_case1():
    print("case1")


def setup_function():
    print("资源准备:setup function")


def teardown_function():
    print("资源消毁:teardown function")


class TestDemo:

    #  执行类 前后分别执行setup_class  teardown_class
    def setup_class(self):
        print("TestDemo setup_class")

    def teardown_class(self):
        print("TestDemo teardown_class")

    # 每个类里面的方法前后分别执行 setup, teardown
    def setup(self):
        print("TestDemo setup")

    def teardown(self):
        print("TestDemo teardown")

    def test_demo1(self):
        print("test demo1")

    def test_demo2(self):
        print("test demo2")


class TestDemo1:

    #  执行类 前后分别执行setup_class  teardown_class
    def setup_class(self):
        print("TestDemo setup_class")

    def teardown_class(self):
        print("TestDemo teardown_class")

    # 每个类里面的方法前后分别执行 setup, teardown
    def setup(self):
        print("TestDemo setup")

    def teardown(self):
        print("TestDemo teardown")

    def test_demo1(self):
        print("test demo1")

    def test_demo2(self):
        print("test demo2")
  • 9
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值