ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Pytest进阶 -- fixture/yield

2022-05-13 19:03:41  阅读:323  来源: 互联网

标签:case -- fixture print Pytest test login def


fixture

 

使用介绍:

@pytest.fixture()   #加fixture装饰器,可以让这个方法后面被调用
def login():
    print("\nlogin.....\n")


def test_search():
    print("search")

def test_order(login):    #只需要在括号内填入加了fixture装饰器的方法,就可以实现在这个测试用例前调用login方法
    print("ordering")

def test_cart():
    print("shopping cart..")

 

作用域:

取值 范围 说明
function 函数级 每一个函数或方法都会调用
class 类级别 每个测试类只运行一次
module 模块级 每一个.py文件调用一次
package 包级 每一个python包只调用一次(暂不支持)
session 会话级 每次会话只需要运行一次,会话内所有方法及类,模块都共享这个方法

module

import pytest


#fixture作用域 -- module
@pytest.fixture(scope= "module")   #避免以test开头
def login():
    print("\nlogin.....\n")


def test_search():
    print("search")

def test_order(login):    
    print("ordering")

def test_cart(login):
    print("shopping cart..")

class TestDemo:
    def test_case_1(self):
        print("test case 1")
    def test_case_2(self,login):
        print("test case 2")

输出:

test_demo.py::test_search search
PASSED
test_demo.py::test_order
login.....    #整个py文件只执行一次

ordering
PASSED
test_demo.py::test_cart shopping cart..
PASSED
test_demo.py::TestDemo::test_case_1 test case 1
PASSED
test_demo.py::TestDemo::test_case_2 test case 2
PASSED

 

class

类里面只运行一次

#fixture作用域
@pytest.fixture(scope= "class")   #避免以test开头
def login():
    print("\nlogin.....\n")


def test_search():
    print("search")

def test_order(login):    #只需要在括号内填入加了fixture装饰器的方法,就可以实现在这个测试用例前调用login方法
    print("ordering")

def test_cart(login):
    print("shopping cart..")

class TestDemo:
    def test_case_1(self,login):
        print("test case 1")
    def test_case_2(self,login):
        print("test case 2")

输出:

test_demo.py::test_search search
PASSED
test_demo.py::test_order
login.....#类外每次都会调用

ordering
PASSED
test_demo.py::test_cart
login.....#类外每次都会调用

shopping cart..
PASSED
test_demo.py::TestDemo::test_case_1
login.....#类内只调用一次

test case 1
PASSED
test_demo.py::TestDemo::test_case_2 test case 2
PASSED

 

 yield

 

#fixture作用域
"""
@pytest.fixture
def fixture_name():
    setup操作
    yield 返回值
    teardown操作
"""
@pytest.fixture()   #避免以test开头
def login():
    print("\nlogin.....\n")
    token = '123'
    yield token #返回token值
    print("\nlogout.....\n")

def test_search():
    print("search")

def test_order(login):    #只需要在括号内填入加了fixture装饰器的方法,就可以实现在这个测试用例前调用login方法
    print("ordering")
    print(f"token:{login}") #这时候使用fixture的方法来代表yield返回的值token

yield也可以返回多个数值

yield token,name

标签:case,--,fixture,print,Pytest,test,login,def
来源: https://www.cnblogs.com/manshuoli/p/16267796.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有