知识点1:
1、测试结果信息阅读
-
passed表示通过,有个简写.
-
failed表示失败,有个简写F
2、命令行参数
-
-h:帮助
-
-version:版本信息
3、测试用例命名规则:
-
测试函数必须以test开头
-
测试类必须以Test开头,但测试类不能有init方法
-
测试文件必须以test/Test开头,或者test/Test结尾
4、运行指定的测试用例:指定文件[::类名][::函数名]
-
用例搜索的规则默认是pytest执行当前目录及子目录(遍历)
-
指定类名运行:pytest 文件名::类名
-
指定函数名运行:pytest 文件名::类名::函数名
-
指定函数名运行:pytest 文件名::函数名
-
指定目录下的文件运行:pytest 目录名\文件名::函数名
-
pytest .\test_a02\test_a02_a.py::test_001 (test_a02_a.py文件下的test_001 类;终端)
5、pytest.main()执行用例
- 命令行参数args,列表类型,一个或多个str组成,逗号分隔
import pytest
def test_a01():
assert 1 == 1
if __name__ == '__main__': # 运行测试的话,需要在pycharm中设置python集成工具=>unittest
# pytest.main() # 等价于pytest命令
# pytest.main([__file__]) # 测试当前文件
# pytest.main(['--version'])
pytest.main['-sv', __file__] # 多参数调用
6、运行指定测试用例——k参数
-
匹配测试类或测试方法的名字部分
-
大小写敏感
-
逻辑运算
表达式 | 说明 |
---|---|
-k login | 测试类名或函数名中包含login的用例 |
-k not login | 不包含login的 |
-k login and success | 包含login且包含success的 |
-k login or success | 包含login或success的 |
-k login and not success | 包含login且不包含success的 |
import pytest
def test_login_success():
pass
def test_login_failed():
pass
def test_logout_success():
pass
def test_logout_failed():
pass
if __name__ == '__main__':
pytest.main(['-k success and not failed', '--collect-only', __file__]) # –collect-only 含义:展示给定配置下哪些测试用例会被运行
7、pytest.mark.skip/skipif
- 有条件的忽略skipif
import pytest, sys
@pytest.mark.skipif(1 == 1, reason='if 1==1 skip this') # 条件满足就忽略测试 reason:标注原因
def test_a01_1(reason='i wanna skip this case'):
pass
@pytest.mark.skipif(1 == 2, reason='if 1==2 skip this') # 条件不满足不会跳过
def test_a01_2():
pass
@pytest.mark.skipif(sys.platform == 'linux', reason='if linux skip this') # sys.platform 操作系统平台标识
def test_a01_3():
pass
if __name__ == '__main__':
pytest.main(['-sv', __file__])
- 输出结果s
import pytest
@pytest.mark.skip
def test_a01_1(reason='i wanna skip this case'):
pass
def test_a01_2