安装pytest插件
pip install pytest
配置 Pytest 为默认测试运行器
-
打开 PyCharm,点击顶部菜单栏的
File
>Settings
(在 Mac 上是PyCharm
>Preferences
)。 -
在设置对话框中,依次选择
Tools
>Python Integrated Tools
。 -
在
Testing
部分,将Default test runner
选项更改为pytest
。 -
点击
OK
保存设置
运行测试
pytest
setup和teardown
在 Pytest 中,setup
和 teardown
是用于在测试前后执行初始化和清理工作的机制。这些操作通常用于设置测试环境、初始化资源、清理资源等。
模块级:setup_module/teardown_module 开始于模块始末,python里一个python文件就是一个模块,也就是一个python文件里的所有测试用例,在模块开始前和结束后运行。
import requests
def setup_module(module):
print("Setup module 测试开始前")
def teardown_module(module):
print("Teardown module 测试结束后")
def test_001():
res = requests.get('http://www.baidu.com')
assert res.status_code == 200
def test_002():
datas = {
"shouji": "15618919442",
"appkey": "0c818521d38759e1"
}
res = requests.post(url="http://sellshop.5istudy.online/sell/shouji/query", data=datas)
assert res.status_code == 200
result = res.json()['result']
print(result)
assert result['shouji'] == '15618919442'
assert result['province'] == '上海'
assert result['city'] == '上海'
assert result['company'] == '中国联通'
assert result['areacode'] == '0571'
函数级:setup_function/teardown_function 对每条函数用例生效
import requests
def setup_function(function):
print("Setup function 测试开始前")
def teardown_function(function):
print("Teardown function 测试结束后")
def test_001():
res = requests.get('http://www.baidu.com')
assert res.status_code == 200
def test_002():
datas = {
"shouji": "15618919442",
"appkey": "0c818521d38759e1"
}
res = requests.post(url="http://sellshop.5istudy.online/sell/shouji/query", data=datas)
assert res.status_code == 200
result = res.json()['result']
print(result)
assert result['shouji'] == '15618919442'
assert result['province'] == '上海'
assert result['city'] == '上海'
assert result['company'] == '中国联通'
assert result['areacode'] == '0571'
类级:setup_class/teardown_class 在类中定义,是类方法,只在类中实例方法的前后运行
import requests
class TestClass():
@classmethod
def setup_class(cls):
print("Setup class 测试开始前")
@classmethod
def teardown_class(cls):
print("Teardown class 测试结束后")
def test_001(self):
res = requests.get('http://www.baidu.com')
assert res.status_code == 200
def test_002(self):
datas = {
"shouji": "15618919442",
"appkey": "0c818521d38759e1"
}
res = requests.post(url="http://sellshop.5istudy.online/sell/shouji/query", data=datas)
assert res.status_code == 200
result = res.json()['result']
print(result)
assert result['shouji'] == '15618919442'
assert result['province'] == '上海'
assert result['city'] == '上海'
assert result['company'] == '中国联通'
assert result['areacode'] == '0571'
方法级:setup_method/teardown_method 开始于方法始末
import requests
class TestClass():
def setup_method(self):
print("Setup class 测试开始前")
def teardown_method(self):
print("Teardown class 测试结束后")
def test_001(self):
res = requests.get('http://www.baidu.com')
assert res.status_code == 200
def test_002(self):
datas = {
"shouji": "15618919442",
"appkey": "0c818521d38759e1"
}
res = requests.post(url="http://sellshop.5istudy.online/sell/shouji/query", data=datas)
assert res.status_code == 200
result = res.json()['result']
print(result)
assert result['shouji'] == '15618919442'
assert result['province'] == '上海'
assert result['city'] == '上海'
assert result['company'] == '中国联通'
assert result['areacode'] == '0571'
fixture
@pytest.fixture
是 Pytest 中的一个装饰器,用于定义测试的上下文环境,包括初始化和清理工作。相比于以上的setup和teardown,使用更加灵活。
(1)通过@pytest.fixture(scope='module')装饰一个函数,函数名可以自定义
(2)测试用例的参数,传递步骤1的函数名
模块级别
import pytest
@pytest.fixture(scope='module')
def func():
print("\n======测试开始前======\n")
yield
print("\n======测试结束后======\n")
def test_001(func):
assert True == True
def test_002(func):
assert True == True
函数级别,测试用例里可以选择传递参数,或者不传递参数
import pytest
@pytest.fixture(scope='function')
def func():
print("\n======测试开始前======\n")
yield
print("\n======测试结束后======\n")
def test_001(func):
assert True == True
def test_002():
assert True == True
pytest.ini
pytest.ini
是 Pytest 的配置文件,用于指定 Pytest 的运行参数和配置选项。
例如:
将日志级别设置为 DEBUG:
[pytest]
addopts = --log-level=DEBUG
指定测试文件或目录:
[pytest]
addopts = tests/unit
yaml
安装
pip install pyyaml
YAML(YAML Ain't Markup Language)是一种人类可读的数据序列化格式,常用于配置文件、数据交换等场景。YAML 的语法简洁明了,易于阅读和编写,支持多种数据结构,如标量(字符串、数字等)、序列(列表)和映射(字典)。
config.yaml文件写入以下内容:
person:
name: John Doe
age: 30
hobbies:
- reading
- hiking
- coding
address:
street: 123 Main St
city: Anytown
state: CA
zip: 12345
import yaml
with open('config.yaml', 'r') as outfile:
res = yaml.safe_load(outfile)
print(res['person']['name'])
print(res['person']['age'])
print(res['person']['hobbies'])
print(res['person']['address'])
Allure
安装
(1)windows下载解压allure-2.13.3.rar,并添加环境变量
(2)pycharm里安装allure_pytest
pip install allure_pytest
(3)执行命令
pytest --alluredir ../testoutput/result
allure serve ./result
parametrize
在 Pytest 中,parametrize
是一个非常强大的功能,用于实现参数化测试。通过 parametrize
,你可以为测试函数提供多个参数值,从而实现对不同输入的测试,而无需编写多个重复的测试函数。
@pytest.mark.parametrize("a,b,sum", [(1, 2, 3), (4, 5, 9)])
def test_004(a, b, sum):
assert sum == a + b