pytest的 模块 级别初始化清除数据的用法,此用法只会在整个代码文件的前后执行一次!!
setup_module & teardown_module
import pytest
def setup_module(): 模块执行前此代码就会执行
print("准备测试数据")
def teardown_module(): 模块代码执行完此代码才会执行
print("清理测试数据")
def test001():
print("001开始执行")
def test002():
print("002开始执行")
def test003():
print("003开始执行")
pytest的 函数 级别初始化清除数据的用法,此用法会在每个函数执行前后各执行一次!!!
setup_function & teardown_function
import pytest
def setup_function(): 每个函数前执行一次
print("准备测试数据")
def teardown_function(): 每个函数执行完在执行
print("清理测试数据")
def test001():
print("001开始执行")
def test002():
print("002开始执行")
def test003():
print("003开始执行")
pytest的 类 级别初始化清除数据的用法,此用法会在每个类执行前后各执行一次!!!
setup_class & teardown_class
class TestMobile:
def setup_class(self): 这个类开始执行前执行一次
print("准备测试数据")
def teardown_class(self): 这个类执行完之后执行一次
print("清理测试数据")
def test001(self):
print("001开始执行")
def test002(self):
print("002开始执行")
def test003(self):
print("003开始执行")
class TestMobile1:
def setup_class(self):
print("准备测试数据")
def teardown_class(self):
print("清理测试数据")
def test0010(self):
print("0010开始执行")
def test0020(self):
print("0020开始执行")
def test0030(self):
print("0030开始执行")
pytest的 方法 级别初始化清除数据的用法,此用法会在每个类里面的每个方法执行前后各执行一次!!!
setup_method & teardown_method
class TestMobile:
def setup_methos(self): 这个类里面每个方法执行前执行一次
print("准备测试数据")
def teardown_method(self): 这个类里面每个方法执行完执行一次
print("清理测试数据")
def test001(self):
print("001开始执行")
def test002(self):
print("002开始执行")
def test003(self):
print("003开始执行")
pytest 通过 @pytest.mark.skip 打标签的方式跳过测试用例,适用于类和方法
import pytest
@pytest.mark.skip 这个pytest装饰器会跳过此条测试用例test001
def test001():
print("001开始执行")
def test002():
print("002开始执行")
def test003():
print("003开始执行")
pytest 通过 @pytest.mark.skipif 打标签的方式选择跳不跳过此测试用例,适用于类和方法
注意:@pytest.mark.skipif() skipif括号里面的表达式必须带双引号包起来
import pytest
@pytest.mark.skipif("1 == 2") 条件为False执行test001用例,条件为True不执行test001用例
def test001():
print("001开始执行")
def test002():
print("002开始执行")
def test003():
print("003开始执行")
pytest 解决接口参数依赖,比如需要获取其他函数产生的token给下一个函数用
# 用户名密码登录
# 拿到登录的token,获取用户信息
class TestUser:
teken = ""
username = ""
def test_login(self):
username = "laobai"
passwork = "123456"
# res = requests.get("/login", json={"username": "username","passwork": "passwork"})
token = "嘿嘿嘿"
TestUser.teken = token 把token信息返回给类属性变量
TestUser.username = username 把token信息返回给类属性变量
def test_userinfo(self):
# token, username = self.test_login()
haders = {
"token": TestUser.teken 调用上面一个方法的属性变量
}
assert haders["token"] == TestUser.teken
assert TestUser.username == "laobai11"
pytest fixture夹具function方法&函数 级别初始化清除数据的用法 scope="function"
import pytest
# 默认scope是function
@pytest.fixture(scope="function") # fixture() 括号里面不传参数默认就是方法级别的处理
def func():
print("我是前置步骤")
def test001(func): # 执行这个测试用例就会先执行fixture()装饰器的方法
print("001开始执行")
def test002():
print("002开始执行")
def test003():
print("003开始执行")
pytest fixture夹具function方法&函数 级别初始化清除数据的用法,在fixture()括号里面传递autouse用法,默认是False表示必须手动把这个装饰器的方法加在相关的函数里面这个fixture夹具初始化清理数据才会生效,如果autouse=True表示这个代码模块里面的所有函数都会执行初始化清理数据!!
import pytest
@pytest.fixture(autouse=True) # autouse=True 表示这里面所有的函数都会执行这个初始化清理方法
def func():
print("我是前置步骤")
def test001():
print("001开始执行")
def test002():
print("002开始执行")
def test003():
print("003开始执行")
pytest fixture夹具 类级别的初始化清除方法 scope="class"
import pytest
# 默认scope是function
@pytest.fixture(scope="class", autouse=True) # scope="class" 表示下面的类在执行前会执行func
def func():
print("我是前置步骤")
class TestClassFixture:
def test001(self):
print("001开始执行")
def test002(self):
print("002开始执行")
def test003(self):
print("003开始执行")
pytest fixture夹具 文件级别初始化清除方法 scope="module"
import pytest
@pytest.fixture(scope="module", autouse=True) # scope="module" 表示模块级别的初始化清除方法
def func():
print("我是前置步骤")
class TestClassFixture: # 只有这个类会执行fixture夹具的方法
def test001(self):
print("001开始执行")
def test002(self):
print("002开始执行")
def test003(self):
print("003开始执行")
def test004():
print("004开始执行")
pytest fixture夹具 多文件初始化清除的方法,需要在指定文件夹添加conftest.py的文件,conftest.py是固定写法 scope="session"
import pytest
@pytest.fixture(scope="session", autouse=True) # scope="session"表示这个初始化清除是多文件使用
def test_session():
print("我是session级的fixture")
pytest fixture夹具 使用conftest.py文件来管理fixture,这样就不要在每个代码文件里面定义初始化清除方法了,只需要在conftest.py里面定义好,在其他文件,方法或者类调用即可
注意:conftest.py里面定义的初始化清除方法,只会在文件夹的同级,或者下级文件夹有效
pytest fixture夹具+conftest.py实现return返回数据,这样我们在后续需要重复使用的数据就可以放在conftest.py文件当中,在其他文件直接调用即可
pytest fixture夹具 使用yield关键字做数据后置处理,用在比如我们在做自动化测试的时候,首先需要创建数据,在这条自动化测试执行完之后需要清理这条数据,就需要用到yield关键字,,也可以结合conftest.py文件组合使用
pytest mark.parametrize 实现单参数单次循环
import pytest
# 单参数单次循环
@pytest.mark.parametrize("name", ["老陈"]) # 这里的"name"为变量,["老陈"]为测试数据
def test_parametrize(name):
print("我是" + name)
pytest mark.parametrize 实现单参数多次循环的例子。运行时,将数组里的值分别赋值给变量,每赋值一次就运行一次
import pytest
# 单参数多次循环的例子
# 运行时,将数组里的值分别赋值给变量,每赋值一次就运行一次
@pytest.mark.parametrize("name", ["老陈", "老周", "老王"]) # 这里的"name"为变量,["老陈", "老周", "老王"]为测试数据
def test_parametrize(name): # 执行这条用例就会依次执行数据 老陈 老周 老王的测试用例
print("我是" + name)
pytest mark.parametrize 实现多参数化多次循环测试数据
注意:实现多参数的时候必须把数组里面的数据用元组或者列表包起来,有多少个变量,每个数组里面的数据应该对应变量的数量
import pytest
@pytest.mark.parametrize("name,word", [["安其拉", "火烧屁屁咯"], ["黄忠", "周日被我射熄火了"], ["鲁班", "上上下下左左右右"]]) # 分别会把数组里面的三条数据分别传给"name,word"
def test_parametrize_02(name, word):
print(f"{name}的台词是{word}")
pytest mark.parametrize 实现参数化的时候,参数值为字典的时候的用法
import pytest
# 参数值为字典
@pytest.mark.parametrize("hero", [{"name": "安其拉"}, {"name": "黄忠"}, {"name": "小乔"}])
def test_parametrize(hero):
print("我叫" + hero["name"])
yaml 语法格式示例
hero: # hero为key 下面的数据为value 字典的值是一个字典
name: 安其拉
word: 火焰是我最喜欢的玩具
HP: 445.5
heros_name: # heros_name为key value为下面的list 字典的值是list
- 安其拉
- 黄忠
- 小乔
heros: # heros为key value为下面的list包字典 字典的值是list包字典
- name: 黄忠
word: 周日被我射熄火了
HP: 440
heros_name_list: # heros_name_list为key value为下面的list嵌套 字典的值是list在包list
- - 安其拉
- 黄忠
- 小乔