专栏文章 Pytest 之 fixture

程序员一凡 · 2021年01月18日 · 1770 次阅读

Pytest 之 fixture

  • unittest 和 nose 都支持 fixture 的,但是 fixture 在 pytest 里使用更灵活。也算是 pytest 的一个闪光点吧
  • 可以理解为一个跟 setup 和 teardown 这种前后置类似的东西。但是比它们要强大、灵活很多

fixtur 当做参数传入

# -*- coding: utf-8 -*-

import pytest

@pytest.fixture()
def login():
    print('登录系统')

# 直接使用函数名做为参数传入
def test_01(login):
    print('测试用例一')

def test_02():
    print('测试用例2')

def test03():
    print('测试用例3')

运行结果

  • 只有 tes_01 调用了 login
  • 遗留问题来了,如果我这里有 10 个方法或更多?是不是都需调用 login 方法?继续看下面的 fixture 参数
testcase.py::test_01 登录系统
测试用例一
PASSED
testcase.py::test_02 测试用例2
PASSED
testcase.py::test03 测试用例3
PASSED

fixture 语法

# scope有4个作用范围:function(不填则默认)、class、module、session
fixture(scope='function', params=None, autouse=False, ids=None, name=None)

参数说明

  • scope:即作用域,function"(默认),"class","module","session"四个
  • params:可选参数列表,它将导致多个参数调用 fixture 函数和所有测试使用它。
  • autouse:默认:False,需要用例手动调用该 fixture;如果是 True,所有作用域内的测试用例都会自动调用该 fixture
  • ids:params 测试 ID 的一部分。如果没有将从 params 自动生成.
  • name:默认:装饰器的名称,同一模块的 fixture 相互调用建议写个不同的 name。
  • session 的作用域:是整个测试会话,即开始执行 pytest 到结束测试 scope 参数作用范围控制 fixture 的作用范围:session>module>class>function

autouse

  • 参数置默认为 False,则需要手动去调用装饰器
# -*- coding: utf-8 -*-

import pytest

# 当前就算定义了装饰器,也不会调用Login
@pytest.fixture()
def login():
    print("打开浏览器")

def test1():
    print("test1里的用例")

def test2():
    print("test2里的用例")
调用方式 1
# -*- coding: utf-8 -*-

import pytest

@pytest.fixture()
def login():
    print("打开浏览器")

# 直接传入函数名
def test1(login):
    print("test1里的用例")

def test2(login):
    print("test2里的用例")
调用方式 2
# -*- coding: utf-8 -*-

import pytest

# autouse设为True,就能自动调用login的装饰器
@pytest.fixture(autouse=True)
def login():
    print("打开浏览器")

# 直接传入函数名
def test1():
    print("test1里的用例")

def test2():
    print("test2里的用例")

function

  • function:作用域为函数
  • 所有的方法都调用了 login
# -*- coding: utf-8 -*-

import pytest

@pytest.fixture(scope='function', autouse=True)
def login():
    print('登录系统')

def test_01():
    print('测试用例一')

def test_02():
    print('测试用例2')

def test03():
    print('测试用例3')

运行结果

  • 符合用例名设计的都会调用装饰器
  • login 不符合所以不会调用
testcase.py::test_01 登录系统
测试用例一
PASSED
testcase.py::test_02 登录系统
测试用例2
PASSED
testcase.py::test03 登录系统
测试用例3
PASSED

class

  • class:作用域为类
  • 所以 TestCase1 和 TestCase2 这两个类都会执行 login
# -*- coding: utf-8 -*-

# @Time : 2021/1/14 21:05
# @Author : 公众号(程序员一凡)

import pytest

@pytest.fixture(scope='class', autouse=True)
def login():
    print('登录系统')


def test_01():
    print('这个是类外面的用例')

class TestCase1:
    def test_02(self):
        print('测试用例2')
    def test03(self):
        print('测试用例3')

class TestCase2:
    def test_04(self):
        print('测试用例4')
    def test05(self):
        print('测试用例5')

运行结果

  • 类里面的方法只会调用一次

  • pytest 机制,因为方法是以 test 开头,所以也会调用

testcase.py::test_01 登录系统
这个是类外面的用例
PASSED
testcase.py::TestCase1::test_02 登录系统
测试用例2
PASSED
testcase.py::TestCase1::test03 测试用例3
PASSED
testcase.py::TestCase2::test_04 登录系统
测试用例4
PASSED
testcase.py::TestCase2::test05 测试用例5
PASSED

module

  • module:在当前.py 脚本里面所有用例开始前只执行一次
  • 只要符合用例的设计要求,不管是类里和外边的都会调用
# -*- coding: utf-8 -*-

import pytest

@pytest.fixture(scope='class', autouse=True)
def open():
    print("打开浏览器,并且打开百度首页")

def test_s1():
    print("用例1:搜索python-1")

class TestCase():
    def test_s2(self):
        print("用例2:搜索python-2")

    def test_s3(self):
        print("用例3:搜索python-3")

运行结果

  • 当前文件里的用例都调用了装饰器
  • 如果类名不是为 Test 开头你试试看是否还会调用装饰器?
testcase.py::test_s1 打开浏览器并且打开百度首页
用例1搜索python-1
PASSED
testcase.py::TestCase::test_s2 打开浏览器并且打开百度首页
用例2搜索python-2
PASSED
testcase.py::TestCase::test_s3 用例3搜索python-3
PASSED

session

  • fixture 为 session 级别是可以跨.py 模块调用的
  • 当我们有多个.py 文件的用例时候,如果多个用例只需调用一次 fixture,那就可以设置为 scope="session",并写到 conftest.py 文件里
  • conftest.py 文件名称是固定的,pytest 会自动识别该文件。放到工程的根目录下,就可以全局调用了
  • 如果放到某个 package 包下,那就只在该 package 内有效
# -*- coding: utf-8 -*-

# conftest文件内容
import pytest

@pytest.fixture(scope="session", autouse=True)
def login():
    print("调用conftest文件的里的方法")

两个用例文件

# -*- coding: utf-8 -*-
# testcase1.py 

import pytest

def test1():
    print("test1里的用例")

def test2():
    print("test2里的用例")

# -*- coding: utf-8 -*-
# testcase1.py  
import pytest
def test3():
    print("test3里的用例")

def test4():
    print("test4里的用例")

运行结果

  • 两个文件只有 testcase 文件的用例调了 conftest 里的方法
testcase.py::test1 调用conftest文件的里的方法
test1里的用例
PASSED
testcase.py::test2 test2里的用例
PASSED
testcase1.py::test3 test3里的用例
PASSED
testcase1.py::test4 test4里的用例
PASSED

pytest-allure 生成测试报告

03

暫無回覆。
需要 登录 後方可回應,如果你還沒有帳號按這裡 注册