零基础小白(五)上篇-Pytest框架介绍及入门

2021-01-16  本文已影响0人  巴鶴

零基础小白 接口自动化测试 集锦: https://www.jianshu.com/nb/49125734

第1步: Pytest 介绍与安装

Pytest介绍

Pytest与unittest区别

unittes

Pytest

Pytest安装

pip install pytest

第2步:Pytest 基础使用

步骤1: 配置pytest.ini文件

[pytest]
addopts = -s

#执行目录
testpaths = testcase
#执行目录下的文件
python_files = test_*.py
#执行测试类
#python_classes = Test_*
#执行测试方法
python_functions = test_*

步骤2: Pytest 函数级别方法

• 运行于测试方法的始末
• 运行一次测试函数会运行一次 setup 和 teardown

# -*- coding: utf-8 -*-
# @Time : 2021/1/12 20:37
# @File : pytest_func.py
# @Author : Yvon_₯㎕ζ๓

"""
1、定义类
2、创建测试方法test开头
3、创建setup,teardown
4、运行查看结果
"""
import  pytest

#1、定义类
class TestFunc:

    #3、创建setup,teardown
    def setup(self):
        print("---setup---")

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

    #2、创建测试方法test开头
    def test_a(self):
        print("---史莱克七怪---")

    def test_b(self):
        print("--- 唐三 小舞 戴沐白 朱竹清 奥斯卡 宁荣荣 马红俊 ---")

#4、运行查看结果
if __name__ == "__main__":
    pytest.main(['-s','pytest_func.py'])
函数级别运行结果.jpg

步骤3: Pytest 类级别方法

• 运行于测试类的始末
• 一个测试内只运行一次 setup_class 和 teardown_class,不关心测试类内有多少个测试函数

# -*- coding: utf-8 -*-
# @Time : 2021/1/12 20:57
# @File : pytest_class.py
# @Author : Yvon_₯㎕ζ๓

"""
1、定义类
2、创建测试方法test开头
3、创建setup_class,teardown_class
4、运行查看结果
"""
import  pytest

#1、定义类
class TestFunc:

    #3、创建setup,teardown
    def setup_class(self):
        print("---setup_class---")

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

    #2、创建测试方法test开头
    def test_a(self):
        print("---史莱克七怪---")

    def test_b(self):
        print("---唐三 小舞 戴沐白 朱竹清 奥斯卡 宁荣荣 马红俊---")

#4、运行查看结果
if __name__ == "__main__":
    pytest.main(['-s','pytest_class.py'])
Pytest 类级别运行结果.jpg

第3步:Pytest 常用插件

pytest常用插件介绍

测试报告插件

失败重试插件

第4步:Pytest 参数化

传入单个参数

传多个参数

上一篇 下一篇

猜你喜欢

热点阅读