一、跳过测试函数
1、方法名:
跳过测试函数
参数:
condition:跳过的条件,必传参数
reason:标注原因,必传参数
@pytest.mark.skipif(condition,reason)
注意:
condition是bool类型的,如果为True表示跳过
代码:
import pytest
class Test_login():
ANDROID_VERSION = 5.0
@pytest.mark.skipif(ANDROID_VERSION < 6.0, reason = "None")
def test_A(self):
print("this is A")
assert 1
def test_B(self):
print("this is B")
assert 1
二、预期失败
1、方法名
2、参数
condition:跳过的条件,必传参数
reason:标注原因,必传参数
@pytest.mark.xfail(condition = None,reason = None,raises = None,run = True, strict = False)
3、注意:
使用装饰器形式使用
4、结论:
1、报告为红色的提示,预期和实际结果不符
2、预期和实际结果相符的情况下,如果是通过,那么是绿色,如果是失败,那么是橙色
3、预期失败前面会有X,如果通过了,那么就是Xpass
预期失败前面会有X,如果失败了,那么就是Xfail
预期失败前面不会有X,如果通过了,那么就是pass
预期失败前面不会有X,如果失败了,那么就是fail
5、用在哪?
1、用在反向测试的时候演示
代码:
import pytest
class Test_login():
# 预言的四种情况:
# 1、实际成功,预言成功
# 2、实际成功,预言失败
# 3、实际失败,预言成功
# 4、实际失败,预言失败
# 实际成功,预言成功
@pytest.mark.Xfail(condition = False, reason = "None")
def test_1(self):
print("this is 1")
assert 1
@pytest.mark.xfail(condition=False, reason="None")
def test_2(self):
print("this is 2")
assert 0
@pytest.mark.xfail(condition=True, reason="None")
def test_3(self):
print("this is 3")
assert 1
@pytest.mark.xfail(condition=True, reason="None")
def test_4(self):
print("this is 4")
assert 0
三、数据参数化
方法名:
@pytest.mark.parameterize(argnames,argvalues,indirect=False,ids=None,scope=None)
参数:
argnames:参数名
argvalues:参数值
一个参数的使用方式:
1、argnames为字符串类型,根据需求决定何时的参数名
2、argvalues为列表类型,根据需求决定列表元素中的内容
3、在测试脚本中,参数,名字与argnames保持一致
4、在测试脚本中正常使用
注意:
argvalues列表有多少个内容,这个脚本就会运行几次
1、什么时候用?
脚本流程是相同的,数据不同的时候,可以使用
2、怎么用?
总的来说,都是使用装饰器的形式
单个参数
@pytest.mark.parametrize("参数名",["参数值1","参数值2","参数值3"])
多个参数
@pytest.mark.parametrize(("参数名1","参数名2"),[("参数名1的值1","参数名2的值1"),("参数名1的值2","参数名2的值2"),("参数名1的值3","参数名2的值3")])
TestCase包下test_login.py文件执行代码:
# 导包
from appium import webdriver
import pytest
import time
# 创建测试类
class TestLogin:
# 创建setup和teardown
def setup(self):
desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.56.101:5555'
desired_caps['appPackage'] = 'com.android.contacts'
desired_caps['appActivity'] = '.activities.PeopleActivity'
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 创建测试函数,数据参数化
@pytest.mark.parametrize(("name","phone"),[("wujinchao","18652510691"),("zhangyan","15962634944"),("mafei","15645702354")])
def test_login(self,name,phone):
# 隐式等待5秒
self.driver.implicitly_wait(5)
self.driver.find_element_by_xpath("//*[@content-desc = '添加新联系人']").click()
self.driver.find_element_by_xpath("//*[@text = '本地保存']").click()
self.driver.find_element_by_xpath("//*[@text = '姓名']").send_keys(name)
self.driver.find_element_by_xpath("//*[@text = '电话']").send_keys(phone)
self.driver.find_element_by_xpath("//*[@content-desc = '向上导航']").click()
def teardown(self):
self.driver.quit()
配置参数pytest.ini:
[pytest]
addopts = -s
testpaths = ./TestCase
python_files = test_*.py
python_classes = Test*
python_functions = test_*
pycharm下Terminal终端输入pytest执行
执行结果:
TestCase\test_login.py ...
3 passed in 92.69s (0:01:32)
pytest-fixture
应用场景
fixture修饰器来标记固定的工厂函数,再其他函数,类调用它时会被激活并优先执行,通常会被用于完成预置处理和重复操作
使用方式:
1、标记工厂函数
--装饰器,@pytest.fixture()
2、使用
--参数
---直接在脚本的参数列表中,添加fixture的名字即可
--函数(装饰器)
---在脚本之上写一个装饰器@pytest.mark.usefixtures("before")
3、fixture的参数
--自动运行
---@pytest.fixture(autouse=True)
4、fixture作用域
--@pytest.fixture(autouse = True,scope = "class")
--优先级
---function级别优先级:fixture>setup
class级别的优先级:setup_class > fixture
5、参数化
--@pytest.fixture(params = ["1","2","3"])
--列表中有多少个元素,脚本就会执行多少次。
--如果想要获取,列表中的内容
---需要在fixture中添加一个request参数
---通过request.param进行获取
6、返回值
--在使用参数引用的时候,这个参数就是fixture函数的返回值
--如果想要使用之前提到的参数化中的内容,那么直接当成返回值即可
案例:
import pytest
# 函数类级别
# @pytest.fixture(autouse=True,scope="class")
# def before():
# print("before")
class TestLogin1():
"""
pytest中的fixture装置函数
1、使用方式
2、参数
3、作用域
4、参数化
5、返回值
"""
# autouse = True:默认使用fixture函数
# False:需要手动调用fixture函数
# scope = "function":作用域为函数级别
# "class"作用域为类级别
# "module"作用域为.py文件
# "session"多个文件调用一次,可以跨.py文件调用
"""
params = ["1", "2", "3"]:列表中有多少个元素,脚本就会执行多少次。
--如果想要获取,列表中的内容
---需要在fixture中添加一个request参数
---通过request.param进行获取
"""
# @pytest.fixture(autouse=True,scope="function",params=["1","2","3"])
# def before(self,request):
# print(request.param)
@pytest.fixture()
def before(self):
return [1,2]
# 使用方式一
# def test_Login1(self,before):
# print("testLogin1")
# 使用方式二
# @pytest.mark.usefixtures("before")
# def test_Login2(self):
# print("testLogin2")
def test_Login1(self,before):
print("testLogin1")
print(before[0])
def test_Login2(self,before):
print("testLogin2")
print(before[1])
# class TestLogin2():
# def test_Login1(self):
# print("testLogin1")
#
#
# def test_Login2(self):
# print("testLogin2")
作为一个软件测试的过来人,我想尽自己最大的努力,帮助每一个伙伴都能顺利找到工作。所以我整理了下面这份资源,现在免费分享给大家,有需要的小伙伴可以关注【公众号:开心螺蛳粉】自提!
软件测试面试文档
我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
行动吧,在路上总比一直观望的要好,未来的你肯定会感谢现在拼搏的自己!如果想学习提升找不到资料,没人答疑解惑时,请及时加入群:1150305204,里面有各种测试开发资料和技术可以一起交流哦。