目录:
1.pytest参数化用例
参数化
参数化应用场景
def test_param_login_ok():
# 登录成功
username = "right"
password = "right"
login(username,password)
def test_param_login_fail():
# 登录失败
username = "wrong"
password = "wrong"
login(username,password)
参数化实现方案
@pytest.mark.parametrize
@pytest.mark.parametrize("username,password", [["right", "right"], ["wrong", "wrong"]])
def test_param(username, password):
login(username, password)
Mark:参数化测试函数使用
参数化:单参数情况
search_list = ['appium', 'selenium', 'pytest']
@pytest.mark.parametrize('name', search_list)
def test_search(name):
assert name in search_list
示例:
import pytest
search_list = ['appium', 'selenium', 'pytest']
@pytest.mark.parametrize("search_key", ['appium', 'selenium', 'pytest', "", 'requests', 'abc'])
def test_search_param(search_key):
assert search_key in search_list
参数化:多参数情况
import pytest
# 数据放在元组中
@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+5", 7), ("7+5", 12)])
def test_mark_more(test_input, expected):
assert eval(test_input) == expected
# 数据放在列表中
@pytest.mark.parametrize("test_input,expected", [["3+5", 8], ["2+5", 7], ["7+5", 12]])
def test_mark_more(test_input, expected):
assert eval(test_input) == expected
参数化:用例重命名-添加 ids 参数
import pytest
@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+5", 7), ("7+5", 12)],
ids=['add_3+5=8', 'add_2+5=7', 'add_3+5=12'])
def test_mark_more(test_input, expected):
assert eval(test_input) == expected
参数化:用例重命名-添加 ids 参数(中文)
# 创建conftest.py 文件 ,将下面内容添加进去,运行脚本
def pytest_collection_modifyitems(items):
"""
测试用例收集完成时,将收集到的用例名name和用例标识nodeid的中文信息显示在控制台上
"""
for i in items:
i.name=i.name.encode("utf-8").decode("unicode_escape")
i._nodeid=i.nodeid.encode("utf-8").decode("unicode_escape")
@pytest.mark.parametrize("test_input,expected",[
("3+5",8),("2+5",7),("7+5",12)
],ids=["3和5相加","2和5相加","7和5相加"])
def test_mark_more(test_input,expected):
assert eval(test_input) == expected
参数化:笛卡尔积
import pytest
@pytest.mark.parametrize("b", ["a", "b", "c"])
@pytest.mark.parametrize("a", [1, 2, 3])
def test_param1(a, b):
print(f"笛卡积形式的参数化中 a={a} , b={b}")
2.pytest标记测试用例
Mark:标记测试用例
pytest -s test_mark_zi_09.py -m=webtest
pytest -s test_mark_zi_09.py -m apptest
pytest -s test_mark_zi_09.py -m "not ios"
import pytest
def double(a):
return a * 2
@pytest.mark.int
def test_double_int():
print('test double int')
assert 2 == double(1)
@pytest.mark.minus
def test_double_minus():
print('test double minus')
assert -2 == double(-1)
@pytest.mark.float
def test_double_float():
assert 0.2 == double(0.1)
@pytest.mark.float
def test_double2_float():
assert -10.2 == double(-0.1)
@pytest.mark.zero
def test_double_0():
assert 10 == double(0)
@pytest.mark.bignum
def test_double_bignum():
assert 200 == double(100)
@pytest.mark.str
def test_double_str():
assert 'aa' == double('a')
@pytest.mark.str
def test_double_str():
assert 'a$a$' == double('a$')
在终端运行命令:
pytest test_test1.py -v -s -m "str"
上述代码会出现异常,pytest识别不到这些标签,解决办法如下:
在项目根目录新建一个pytest.ini文件
重新运行:
3.pytest设置跳过、预期失败用例
Mark:跳过(Skip)及预期失败(xFail)
Skip 使用场景
@pytest.mark.skip
@pytest.mark.skipif
pytest.skip(reason)
import sys
import pytest
@pytest.mark.skip
def test_aaa():
print("没有任何输出")
assert True
@pytest.mark.skip(reason="代码没有实现")
def test_bbb():
assert False
@pytest.mark.skipif(sys.platform == 'darwin', reason="does now run on mac")
def test_case1():
assert True
@pytest.mark.skipif(sys.platform == 'win', reason="dose not run on windows")
def test_case2():
assert True
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")
def test_case3():
assert True
def check_login():
# return True
return False
def test_ccc():
if not check_login():
pytest.skip("")
print("end")
xfail 使用场景
@pytest.mark.xfail
xfail 是 pytest 中的一个标记,用于标记测试用例,表示该测试用例在某些条件下不会执行,而是被标记为 pass。通常用于在开发阶段快速验证代码的正确性,而在发布版本中禁用这些测试用例,以避免潜在的问题。使用 xfail 标记的测试用例在运行时会被 pytest 忽略,但它们仍然会被计入测试覆盖率。这使得您可以快速查看哪些测试用例未通过,而无需查看未通过的原因。
import pytest
@pytest.mark.xfail
def test_aaa():
print('test_xfail 方法执行')
# assert 1 == 2
assert 2 == 2
xfail = pytest.mark.xfail
@xfail(reason="有bug")
def test_hello():
assert 0
def test_xfail():
print('****开始测试****')
pytest.xfail(reason='该功能尚未完成')
print('测试过程')
assert 1 == 1
4.pytest运行用例
运行多条用例
运行多条用例方式
pytest [包名]
pytest 文件名.py
pytest 文件名.py::类名
pytest 文件名.py::类名::方法名
运行结果分析
运行结果分析是测试执行过程中非常重要的一个环节,可以帮助开发人员快速定位和解决问题。常用的结果包括:
除了常用的结果之外,还有一些特殊的结果,例如:
5.pytest测试用例调度与运行
命令行参数-使用缓存状态
--lf(--last-failed)
选项会重新运行上一次失败的测试用例。它会跳过所有未失败的测试用例,只运行上一次失败的测试用例。--ff(--failed-first)
选项会先运行上一次失败的测试用例,然后再运行其余的测试用例。它会跳过所有未失败的测试用例,先运行上一次失败的测试用例,然后再运行其余的测试用例。6.pytest命令行常用参数
--help
-x 用例一旦失败(fail/error),就立刻停止执行
--maxfail=num 用例达到
-m 标记用例
-k 执行包含某个关键字的测试用例
-v 打印详细日志
-s 打印输出日志(一般-vs一块儿使用)
--collect-only(测试平台,pytest 自动导入功能 )
7.python执行pytest
Python 代码执行 pytest
Python 代码执行 pytest - main 函数
if __name__ == '__main__':
# 1、运行当前目录下所有符合规则的用例,包括子目录(test_*.py 和 *_test.py)
pytest.main()
# 2、运行test_mark1.py::test_dkej模块中的某一条用例
pytest.main(['test_mark1.py::test_dkej','-vs'])
# 3、运行某个 标签
pytest.main(['test_mark1.py','-vs','-m','dkej'])
运行方式(在终端输入命令~)
`python test_*.py `
8.pytest异常处理
常用的异常处理方法
异常处理方法 try …except
try:
可能产生异常的代码块
except [ (Error1, Error2, ... ) [as e] ]:
处理异常的代码块1
except [ (Error3, Error4, ... ) [as e] ]:
处理异常的代码块2
except [Exception]:
处理其它异常
try:
a = int(input('输入被除数:'))
b = int(input('输入除数:'))
c = a / b
print('您输入的俩个数相除的结果是:', c)
except(ValueError, ArithmeticError):
print('程序发生数字格式异常,算术异常之一')
except:
print('未知异常')
print('程序继续运行')
异常处理方法 pytest.raise()
pytest.raise() 用法
def test_raise():
with pytest.raises(ValueError, match='must be 0 or None'):
raise ValueError("value must be 0 or None")
def test_raise1():
with pytest.raises(ValueError) as exc_info:
raise ValueError("value must be 42")
assert exc_info.type is ValueError
assert exc_info.value.args[0] == "value must be 42"