Pytest自动化测试框架-前后置操作详细

前言

1、Pytest 的前置与后置处理

Pytest贴心的提供了类似setup、teardown的方法,并且还超过四个,一共有十种

模块级别:setup_module、teardown_module;
函数级别:setup_function、teardown_function,不在类中的方法;
类级别:setup_class、teardown_class;
方法级别:setup_method、teardown_method;
方法细化级别:setup、teardown;

2、setup和teardown的详细使用

代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest

def setup_module():
    print("=====整个.py模块开始前只执行一次:登录=====")

def teardown_module():
    print("=====整个.py模块结束后只执行一次:数据清理=====")

def setup_function():
    print("===每个函数级别用例开始前都执行setup_function===")

def teardown_function():
    print("===每个函数级别用例结束后都执行teardown_function====")

def test_one():
    print("1")

def test_two():
    print("2")

class TestCase():
    def setup_class(self):
        print("====整个测试类开始前只执行一次setup_class====")

    def teardown_class(self):
        print("====整个测试类结束后只执行一次teardown_class====")

    def setup_method(self):
        print("==测试类里面每个用例执行前都会执行setup_method==")

    def teardown_method(self):
        print("==测试类里面每个用例结束后都会执行teardown_method==")

    def setup(self):
        print("=测试类里面每个用例执行前都会执行setup=")

    def teardown(self):
        print("=测试类里面每个用例结束后都会执行teardown=")

    def test_three(self):
        print("3")

    def test_four(self):
        print("4")


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

运行结果:

C:\Users\chenyongzhi11\.virtualenvs\api-test-project-FfYYNBU1\Scripts\python.exe D:/apk_api/api-test-project/debug/debug_pytest.py 
=====整个.py模块开始前只执行一次:登录=====
===每个函数级别用例开始前都执行setup_function===
1
.===每个函数级别用例结束后都执行teardown_function====
===每个函数级别用例开始前都执行setup_function===
2
.===每个函数级别用例结束后都执行teardown_function====
====整个测试类开始前只执行一次setup_class====
==测试类里面每个用例执行前都会执行setup_method==
3
.==测试类里面每个用例结束后都会执行teardown_method==
==测试类里面每个用例执行前都会执行setup_method==
4
.==测试类里面每个用例结束后都会执行teardown_method==
====整个测试类结束后只执行一次teardown_class====
=====整个.py模块结束后只执行一次:数据清理=====

4 passed in 0.33s

进程已结束,退出代码0

模块级别:模块级别的初始化、清除分别再整个模块的测试用例执行钱后执行,并且只执行一次

类级别:类级别的初始化、清除分别再整个类的测试用例执行前后执行,并且只执行一次

方法级别:方法级别的初始化、清除分别在类的每个测试方法执行前后执行,并且每个用例执行一次

注意:上述都是针对整个脚本全局生效

3、Fixture的详细使用

1)fixture的优势
名命灵活,不局限与setup\teardown 的名命

conftest.py 配置可以实现数据共享,不需要import 就能自动找到fixture

scope=“module” 实现多py文件共享前置

scope=“session” 实现多个py文件使用一个session完成多用例

2)fixture参数列表

@pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None)
def test():
    print("fixture初始化的参数列表")

参数列表

scope:可以理解成fixture的作用域,默认:function,还有class、module、package、session四个【常用】

autouse:默认:False,需要用例手动调用该fixture;如果是True,所有作用域内的测试用例都会自动调用该fixture

name:默认:装饰器的名称,同一模块的fixture相互调用建议写个不同的name

注意:session的作用域:是整个测试会话,即开始执行pytest到结束测试

3)测试用例调用fixture的方式

将fixture名称作为测试用例函数的输入参数

方式1:

测试用例加上装饰器:@pytest.mark.usefixtures(fixture_name)
fixture设置autouse=True

将fixture名称作为测试用例函数的输入参数

import pytest


# 调用方式一
@pytest.fixture
def login():
    print("输入账号,密码先登录")


def test_s1(login):
    print("用例 1:登录之后其它动作 111")


def test_s2():  # 不传 login
    print("用例 2:不需要登录,操作 222")


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

结果:

============================= test session starts =============================
platform win32 -- Python 3.9.5, pytest-7.3.1, pluggy-1.0.0
rootdir: D:\apk_api\api-test-project\debug
plugins: Faker-18.4.0
collected 2 items

debug_pytest.py 输入账号,密码先登录
用例 1:登录之后其它动作 111
.用例 2:不需要登录,操作 222
.

============================== 2 passed in 0.14s ==============================

进程已结束,退出代码0

方式2:

测试用例加上装饰器:@pytest.mark.usefixtures(fixture_name)

import pytest


# 调用方式一
@pytest.fixture
def login():
    print("输入账号,密码先登录")


# 调用方式二
@pytest.fixture
def login2():
    print("please输入账号,密码先登录")


@pytest.mark.usefixtures("login2", "login")
def test_s11():
    print("用例 11:登录之后其它动作 111")

注意:
在类声明上面加 @pytest.mark.usefixtures() ,代表这个类里面所有测试用例都会调用该fixture

可以叠加多个 @pytest.mark.usefixtures() ,先执行的放底层,后执行的放上层

可以传多个fixture参数,先执行的放前面,后执行的放后面

如果fixture有返回值,用 @pytest.mark.usefixtures() 是无法获取到返回值的,必须用传参的方式(方式一)

方式3:

fixture设置autouse=True

import pytest


# 调用方式三

@pytest.fixture(autouse=True)
def login3():
    print("====所有作用域内的测试用例都会自动调用该fixture===")


def test_s1(login3):
    print("用例 1:登录之后其它动作 111")


def test_s2():  # 不传 login
    print("用例 2:不需要登录,操作 222")


# 不是test开头,加了装饰器也不会执行fixture
@pytest.mark.usefixtures("login3")
def loginss():
    print(123)

4、fixture的实例化顺序

较高 scope 范围的fixture(session)在较低 scope 范围的fixture( function 、 class )之前实例化【session > package > module > class > function】

具有相同作用域的fixture遵循测试函数中声明的顺序,并遵循fixture之间的依赖关系【在fixture_A里面依赖的fixture_B优先实例化,然后到fixture_A实例化】

自动使用(autouse=True)的fixture将在显式使用(传参或装饰器)的fixture之前实例化

import pytest

order = []


@pytest.fixture(scope="session")
def s1():
    order.append("s1")


@pytest.fixture(scope="module")
def m1():
    order.append("m1")


@pytest.fixture
def f1(f3, a1):
    # 先实例化f3, 再实例化a1, 最后实例化f1
    order.append("f1")
    assert f3 == 123


@pytest.fixture
def f3():
    order.append("f3")
    a = 123
    yield a


@pytest.fixture
def a1():
    order.append("a1")


@pytest.fixture
def f2():
    order.append("f2")


def test_order(f1, m1, f2, s1):
    # m1、s1在f1后,但因为scope范围大,所以会优先实例化
    assert order == ["s1", "m1", "f3", "a1", "f1", "f2"]

执行结果:断言成功

关于fixture的注意点

添加了 @pytest.fixture ,如果fixture还想依赖其他fixture,需要用函数传参的方式,不能用 @pytest.mark.usefixtures() 的方式,否则会不生效

@pytest.fixture(scope="session")
def open():
    print("===打开浏览器===")

@pytest.fixture
# @pytest.mark.usefixtures("open") 不可取!!!不生效!!!
def login(open):
    # 方法级别前置操作setup
    print(f"输入账号,密码先登录{open}")

5、fixture的后置teardown

用fixture实现teardown并不是一个独立的函数,而是用 yield 关键字来开启teardown操作(yield 之前是前置,之后是后置)

import pytest


@pytest.fixture(scope="session")
def open():
    # 会话前置操作setup
    print("===打开浏览器===")
    test = "测试变量是否返回"
    yield test
    # 会话后置操作teardown
    print("==关闭浏览器==")


@pytest.fixture
def login(open):
    # 方法级别前置操作setup
    print(f"输入账号,密码先登录---{open}")
    name = "==我是账号=="
    pwd = "==我是密码=="
    age = "==我是年龄=="
    # 返回变量
    yield name, pwd, age
    # 方法级别后置操作teardown
    print("登录成功")


def test_s2(login):
    print("==用例2==")
    print(login)

yield注意事项:

如果yield前面的代码,即setup部分已经抛出异常了,则不会执行yield后面的teardown内容

如果测试用例抛出异常,yield后面的teardown内容还是会正常执行

6、addfinalizer

在用法上,addfinalizer跟yield是不同的,需要你去注册作为终结器使用的函数。

import pytest


@pytest.fixture()
def demo_fixture(request):
    print("\n这个fixture在每个case前执行一次")

    def demo_finalizer():
        print("\n在每个case完成后执行的teardown")

    # 注册demo_finalizer为终结函数
    request.addfinalizer(demo_finalizer)


def test_01(demo_fixture):
    print("\n===执行了case: test_01===")


def test_02(demo_fixture):
    print("\n===执行了case: test_02===")


def test_03(demo_fixture):
    print("\n===执行了case: test_03===")

7、yield与addfinalizer的区别

1)addfinalizer可以注册多个终结函数。

import pytest


@pytest.fixture()
def demo_fixture(request):
    print("\n这个fixture在每个case前执行一次")

    def demo_finalizer():
        print("\n在每个case完成后执行的teardown")

    def demo_finalizer2():
        print("\n在每个case完成后执行的teardown2")

    def demo_finalizer3():
        print("\n在每个case完成后执行的teardown3")

    # 注册demo_finalizer为终结函数
    request.addfinalizer(demo_finalizer)
    request.addfinalizer(demo_finalizer2)
    request.addfinalizer(demo_finalizer3)


def test_01(demo_fixture):
    print("\n===执行了case: test_01===")


def test_02(demo_fixture):
    print("\n===执行了case: test_02===")


def test_03(demo_fixture):
    print("\n===执行了case: test_03===")

2)当setUp的代码执行错误,addfinalizer依旧会执行

conftest.py的详细讲解

可以理解成一个专门存放fixture的配置文件,如果多个测试用例文件(test_*.py)的所有用例都需要用登录功能来作为前置操作,那就不能把登录功能写到某个用例文件中去了,conftest.py的出现,就是为了解决上述问题,单独管理一些全局的fixture。

conftest.py配置fixture注意事项:

pytest会默认读取conftest.py里面的所有fixture;
conftest.py 文件名称是固定的,不能改动;
conftest.py只对同一个package下的所有测试用例生效;
不同目录可以有自己的conftest.py,一个项目中可以有多个conftest.py;
测试用例文件中不需要手动import conftest.py,pytest会自动查找;

conftest.py使用举例
conftest.py文件(scope=“session”)

import pytest


@pytest.fixture(scope="session")
def login():
    print("输入账号密码")
    yield
    print("清理数据完成")

case文件:

import pytest


class TestLogin1():
    def test_1(self, login):
        print("用例1")

    def test_2(self):
        print("用例2")

    def test_3(self, login):
        print("用例3")


if __name__ == '__main__':
    pytest.main()

输出:

输入账号密码
PASSED                                  [ 33%]用例1
PASSED                                  [ 66%]用例2
PASSED                                  [100%]用例3
清理数据完成

可以看出,conftest.py内的fixture方法的作用范围是session,调用时,整个.py文件只会调用一次

conftest.py注意事项:

conftest.py的作用域与Python变量作用域相同;
内层包内conftest.py不允许被其它包的测试类或方法使用,相当于本地变量;
外层conftest.py可被内层测试类和方法使用,相当于全局变量;

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

 

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取   

 

  • 15
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值