暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

pytest之fixture

小二测试成长 2021-08-30
639

一、fixture优势
1、命名方式灵活
2、conftest.py配置里可以实现数据共享,不需要import就能自动找到一些配置
3、scope=”module“可以实现多个.py跨文件共享前置
4、scope="session"可以实现多个.py跨文件使用一个session来完成多个用例
二、fixture介绍
方法:fixture(scope="function", params=None, autouse=False, ids=None, name=None)
"使用装饰器标记fixture的功能
     可以使用此装饰器(带或不带参数)来定义fixture功能。fixture功能的名称可以在以后使用
     引用它会在运行测试之前调用它:test模块或类可以使用pytest.mark.usefixtures(fixturename标记。
     测试功能可以直接使用fixture名称作为输入参数,在这种情况下,夹具实例从fixture返回功能将被注入。
 
scope:被标记方法的作用域
function" (default):作用于每个测试方法,每个test都运行一次
"class":作用于整个类,每个class的所有test只运行一次
"module":作用于整个模块,每个module的所有test只运行一次
"session:作用于整个session(慎用),每个session只运行一次
params:(list类型)提供参数数据,供调用标记方法的函数使用
autouse:是否自动运行,默认为False不运行,设置为True自动运行
三、简单代码示例
示例一:通过参数引用fixture
import pytest

@pytest.fixture()
def login():
    print("输入账号密码")

def test_one(login):
    print("登陆后,执行测试1")

def test_two():
    print("不用登录,执行测试")

def test_three(login):
    print("不用登录执行测试2")

if __name__ == '__main__':
    pytest.main(['-s', 'test_fixture.py'])
输出结果:
示例二:可以把fixture函数写到conftest.py中
conftest.py
import pytest

@pytest.fixture()
def login():
    print("\n输入账号密码")  #\n是为了输出时候好看一点
test_fixture.py
import pytest
def test_one(login):
    print("\n登陆后,执行测试1")

def test_two():
    print("\n不用登录,执行测试")

def test_three(login):
    print("\n不用登录执行测试2")

if __name__ == '__main__':
    pytest.main(['-s', 'test_fixture.py'])
运行结果与示例一一致
示例三:
使用scope="module"设置为整个模块运行一次,默认为scope="function"
autouse=True设置为自动运行,默认为autouse=false
yield相当于一个分割线,yield前面的是最先运行相当于setup函数、yield后面的最后运行,相当于teardown函数
如果把scope修改成默认、则每个方法都会运行一次fixture
import pytest
@pytest.fixture(scope="module", autouse=True)
def open_browser():
    print("\n打开浏览器,进入测试环境")  #相当于setup
    yield
    
    print('最后关闭浏览器') #相当于teardown
def test_one(login):
    print("\n登陆后,执行测试1")

def test_two():
    print("\n不用登录,执行测试")

def test_three(login):
    print("\n不用登录执行测试2")

if __name__ == '__main__':
    pytest.main(['-s', 'test_fixture.py'])
输出结果:
示例四:可以作为返回值并且在一个函数中多次调用
import pytest
@pytest.fixture()
def make_customer_record():
    def _make_customer_record(name):
        return {
            "name":name,
            "orders":[]
        }
    return _make_customer_record


def test_customer_records(make_customer_record):
    customer_1 = make_customer_record("lisa")
    customer_2 = make_customer_record("mike")
    customer_3 = make_customer_record("anna")
    print(customer_1,customer_2,customer_3)


执行结果:[100%]{'name': 'lisa', 'orders': []} {'name': 'mike', 'orders': []} {'name': 'anna', 'orders': []}
示例五:pytest还可以嵌套使用
import pytest
# autouse=True 不用要测试方法中传参调用,默认全使用,如果想某个模块不使用就要显示声明
@pytest.fixture(autouse=True)
def login(open_browser):
    print("\n进入登录页面")

@pytest.fixture()
def open_browser():
    print("\n打开浏览器")

def test_case1():
    print("\ncase1:登陆后执行")

def test_case2():
    print("\ncase2:不登陆执行")

def test_case3():
    print("\ncase3:登录后执行")

if __name__ == '__main__':
    pytest.main(["-s","test_fixture.py"])
执行结果:




微信号:wsj742769775

-扫码关注我-



文章转载自小二测试成长,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论