- 模块:pytest
安装:pip3 install pytest
导入模块:import pytest
- 基本使用方法:
2.1说明:
2.1.1函数名必须以test开头
2.1.2类名必须以Test开头,类中的方法需要以test开头
2.1.2.1类中不能有构造方法
2.1.3使用assert进行断言
2.1.4脚本名必须以test开头
2.2作用于函数
import pytest
def test_1():
assert 1 == 1
def atest_1():
print(123)
assert 1 == 1
def test_1():
assert 12 > 100
if __name__ == '__main__':
# pytest.main(['-s', __file__])
pytest.main(['-v', 'test_pytest/test_1_func.py'])
pytest.main(['-q', 'test_pytest/test_1_func.py'])
2.3. 作用于类
import pytest
class Test_Add:
# def __init__(self):
# pass
def test(self):
assert 12 > 100
def atest(self):
assert 12 > 100
if __name__ == '__main__':
pytest.main(['-v', __file__])
2.4. 用例执行
方式
脚本:pytest.mian([参数1,参数2,参数。。。])
命令行
pytest 参数 脚本
pytest 参数 脚本
python -m pytest 参数脚本
参数
-s,
-v,
-q,
-k,通过关键字匹配脚本、函数名、类名、方法名
-x,如果测试执行过程中有fail的用例,则测试立即停止
–maxfile=n,当fail用例达到指定的数量,则退出测试
-m,对用例进行标记,执行指定的用例
1.在项目根目录下新建文件pytest.ini
2.在pytest.ini
文件中添加标记
[pytest]
markers = a
b
c
smoke_case
c. 使用装饰器标记测试用例@pytest.mark.xxx
@pytest.mark.a
def test_1():
assert 1 == 1
@pytest.mark.b
def test_2():
print(123