直播

###课程贴地址

https://ceshiren.com/t/topic/9916

fixture

调用方式

  • 测试用例中传入 fixture 方法名
  • @pytest.mark.usefixtures("login")
  • 自动调用:@pytest.fixture(autouse=True)

作用域

  • 控制方法:@pytest.fixture(scope='')
  • scope 的取值
    • function
    • class
    • module
    • session

fixture 方法返回值的获取

  • 在测试用例中使用 fixture 方法名就可以获取到 yield 后面的返回值

作用:

定义传入测试中的数据集

配置测试前系统的初始状态

为批量测试提供数据源等

场景一:自动化中应用

测试用例执行时,有的用例需要登陆才能执行,有些用例不需要登录

setup  和teardown无法满足

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


@pytest.fixture()
def login():
    print("登录操作")

#测试用例1,需要提前登录
#在测试用例中传入fix方法名
def test_case1(login):
    print("测试用例1")


def test_case2():
    print("测试用例2")

#测试用例3,需要提前登录
def test_case3(login):
    print("测试用例3")

#测试用例4,需要提前登录
#用装饰器的方法,将函数名用字符串方式传入
@pytest.mark.usefixtures('login')
def test_case4():
    print("测试用例4")

 

 注:

获取fixture 方法的返回值,直接调用fixture方法名

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


@pytest.fixture()
def login():
    print("登录操作")
    print('获取 token')
    username = "tom"
    password = "123456"
    token = "tokerjdjdkkdiid"
    #yield关键字可以激活fixture的teardown功能
    #yeild相当于ruturn,返回数据可以直接跟在yield后面
    yield[username,password,token]
    print("注销操作")


# 测试用例1需要登录操作
def test_case1(login):
    print(f"用户信息返回:{login}")
    print("这是测试用例1")

 

 如果所用用例都需要调用“登录模块

则需要:autouse

@pytest.fixture(autouse= True)
#!/usr/bin/env python
# -*-coding:utf-8 -*-
# -*-hanjingjing-*-
import pytest


@pytest.fixture(autouse= True)
def login():
    print("登录操作")
    print('获取 token')
    username = "tom"
    password = "123456"
    token = "tokerjdjdkkdiid"
    #yield关键字可以激活fixture的teardown功能
    #yeild相当于ruturn,返回数据可以直接跟在yield后面
    yield[username,password,token]
    print("注销操作")


# 测试用例1需要登录操作
def test_case1(login):
    print(f"用户信息返回:{login}")
    print("这是测试用例1")


def test_case2():
    print("这是测试用例2")


# 测试用例3需要登录操作
def test_case3(login):
    print("这是测试用例3")

#c测试用例4,用装饰器的方法
#方法名作为字符串的方式的传入
@pytest.mark.usefixtures("login")
def test_case4():
    print("这是测试用例41")

1)function

import pytest

@pytest.fixture(scope='function')
def connectDB():
    print("连接数据库操作")
    yield
    print("断开数据库操作")

class TestDemo:

    def test_a(self,connectDB):
        print("测试用例a")

    def test_b(self,connectDB):
        print("测试用例b")


class TestDemo1:

    def test_c(self,connectDB):
        print("测试用例c")

    def test_d(self,connectDB):
        print("测试用例d")

输出结果

两个函数调用两次

 2)class

一个类调一次

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

@pytest.fixture(scope='class')
def connectDB():
    print("连接数据库操作")
    yield
    print("断开数据库操作")

class TestDemo:

    def test_a(self,connectDB):
        print("测试用例a")

    def test_b(self,connectDB):
        print("测试用例b")

    def test_a(self,connectDB):
        print("测试用例a")

    def test_e(self,connectDB):
        print("测试用例e")

class TestDemo1:

    def test_c(self,connectDB):
        print("测试用例c")

    def test_d(self,connectDB):
        print("测试用例d")

输出结果

3)module

整个文件调用一下

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

@pytest.fixture(scope='module')
def connectDB():
    print("连接数据库操作")
    yield
    print("断开数据库操作")

class TestDemo:

    def test_a(self,connectDB):
        print("测试用例a")

    def test_b(self,connectDB):
        print("测试用例b")

    def test_a(self,connectDB):
        print("测试用例a")

    def test_e(self,connectDB):
        print("测试用例e")

class TestDemo1:

    def test_c(self,connectDB):
        print("测试用例c")

    def test_d(self,connectDB):
        print("测试用例d")

输出结果

原文地址:https://www.cnblogs.com/hantongxue/p/14364529.html