若是你还想从头学起Pytest,能够看看这个系列的文章哦!html
https://www.cnblogs.com/poloyy/category/1690628.htmlpython
@pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None) def test(): print("fixture初始化的参数列表")
session的做用域:是整个测试会话,即开始执行pytest到结束测试浏览器
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = __Time__ = 2020-04-06 15:50 __Author__ = 小菠萝测试笔记 __Blog__ = https://www.cnblogs.com/poloyy/ """ import pytest # 调用方式一 @pytest.fixture def login(): print("输入帐号,密码先登陆") def test_s1(login): print("用例 1:登陆以后其它动做 111") def test_s2(): # 不传 login print("用例 2:不须要登陆,操做 222") # 调用方式二 @pytest.fixture def login2(): print("please输入帐号,密码先登陆") @pytest.mark.usefixtures("login2", "login") def test_s11(): print("用例 11:登陆以后其它动做 111") # 调用方式三 @pytest.fixture(autouse=True) def login3(): print("====auto===") # 不是test开头,加了装饰器也不会执行fixture @pytest.mark.usefixtures("login2") def loginss(): print(123)
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = __Time__ = 2020-04-06 16:14 __Author__ = 小菠萝测试笔记 __Blog__ = https://www.cnblogs.com/poloyy/ """ 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): # m一、s1在f1后,但由于scope范围大,因此会优先实例化 assert order == ["s1", "m1", "f3", "a1", "f1", "f2"]
断言成功session
添加了 @pytest.fixture ,若是fixture还想依赖其余fixture,须要用函数传参的方式,不能用 @pytest.mark.usefixtures() 的方式,不然会不生效app
@pytest.fixture(scope="session") def open(): print("===打开浏览器===") @pytest.fixture # @pytest.mark.usefixtures("open") 不可取!!!不生效!!! def login(open): # 方法级别前置操做setup print(f"输入帐号,密码先登陆{open}")
前面讲的,其实都是setup的操做,那么如今就来说下teardown是怎么实现的ide
用fixture实现teardown并非一个独立的函数,而是用 yield 关键字来开启teardown操做函数
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = __Time__ = 2020-04-06 15:50 __Author__ = 小菠萝测试笔记 __Blog__ = https://www.cnblogs.com/poloyy/ """ 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_s1(login): print("==用例1==") # 返回的是一个元组 print(login) # 分别赋值给不一样变量 name, pwd, age = login print(name, pwd, age) assert "帐号" in name assert "密码" in pwd assert "年龄" in age def test_s2(login): print("==用例2==") print(login)
# 官方例子 @pytest.fixture(scope="module") def smtp_connection(): with smtplib.SMTP("smtp.gmail.com", 587, timeout=5) as smtp_connection: yield smtp_connection # provide the fixture value
该 smtp_connection 链接将测试完成执行后已经关闭,由于 smtp_connection 对象自动关闭时, with 语句结束。测试
@pytest.fixture(scope="module") def test_addfinalizer(request): # 前置操做setup print("==再次打开浏览器==") test = "test_addfinalizer" def fin(): # 后置操做teardown print("==再次关闭浏览器==") request.addfinalizer(fin) # 返回前置操做的变量 return test def test_anthor(test_addfinalizer): print("==最新用例==", test_addfinalizer)