pytest基础知识一

pytest基础知识一,第1张

pytest基础知识一
  • pytest的介绍
    • pytest的作用
    • pytest的安装与版本查看
  • pytest的基本应用
    • 编写第一个简单的pytest应用
    • pytest文件和函数命令规则
    • pytest文件的三种运行方式
      • 命令行运行
      • PyCharm界面运行
      • pytest.main()运行pytesr文件
    • pytest常用的命令行运行参数
    • 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__add 或 add_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")

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/800071.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-06
下一篇2022-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存