免费网站建设空间,wordpress系统迁移,网络营销和网络销售的区别,进入秦皇岛最新规定1.安装
pip install pytestpytest官网#xff1a;https://docs.pytest.org/en/stable/
2.编写规则
1.测试文件已test开头#xff08;或以test结尾#xff09; 2.测试类以Test开头#xff0c;并且不能带有init方法 3.测试函数以test开头 4.断言使用基本的assert即可
3.p…1.安装
pip install pytestpytest官网https://docs.pytest.org/en/stable/
2.编写规则
1.测试文件已test开头或以test结尾 2.测试类以Test开头并且不能带有init方法 3.测试函数以test开头 4.断言使用基本的assert即可
3.pytest使用
1.运行
需要在pycharm设置界面配置
用绿色执行箭头 用main 方法执行
if __name__ __main__:pytest.main([demo3.py])用命令行
# 1.执行xxx.py文件里面的所有方法
pytest -v -s xxx.py
# 2.执行xxx.py文件里面指定的方法test_func1
pytest xxx.py::test_func1
pytest -k func1 xxxx.py # 使用模糊匹配使用-k选项标识使用pytest.mark在函数上进行标记 首先写个配置文件pytest.ini 在测试用例注释执行或不执行
import pytestclass TestLoginCase(object):pytest.mark.dodef test01(self):print(这是第一条测试用例)pytest.mark.undodef test02(self):print(这是第二条测试用例)# 命令执行xxx.py文件里标准执行的测试用例
pytest -m do xxx.py2.pytest参数化
pytest.mark.parametrize(argnames,argvalues) argvalues可以是列表元组字典
# 列表
data [123, 456]pytest.mark.parametrize(pwd, data)
def test1(pwd):print(pwd)
# 元组
data1 [(admin, 123, mbzx), (amdin, 456, m24x)]pytest.mark.parametrize(username, password, vector, data1)
def test2(username, password, vector):print(username, password, vector)# 字典
data2 ({user: 1,pwd: 2},{age: 3,email: fgqq.com}
)pytest.mark.parametrize(dic, data2)
def test3(dic):print(dic)
data3 [pytest.param(1, 2, 3, id(ab):pass), # id的值可以自定义只要方便理解每个用例是干什么的即可pytest.param(4, 5, 10, id(ab):fail)
]def add(a, b):return a bpytest.mark.parametrize(a,b,expect, data3)
def test04(a, b, expect):assert add(a, b) expect3.pytest.fixture
1.定义fixture跟定义普通函数差不多唯一区别就是在函数上加个装饰器pytest.fixture 2.fixture命名不要以test开头跟用例区分开。fixture是有返回值没有返回值默认为None。 3.用例调用fixture的返回值直接就是把fixture的函数名称当做变量名称。
pytest.fixture()
def demo():print(这是一个例子)return 1
def test05(demo):print(这是一个测试)4.setup和teardown
1.模块级setup_module/teardown_module开始于模块始末全局的 2.函数级setup_function/teardown_fuction只对函数用例生效不在类中
import pytestdef setup_module():print(setup_module)def teardown_module():print(teardown_module)def setup_function():print(setup_function)def teardown_function():print(teardown_function)def test1():print(test1)def test2():print(test2)3.类级setup_class/teardown_class只在类中前后运行一次在类中 4.方法级setup_method/teardown_method开始于方法始末在类中 5.类里面的setup/teardown运行在调用方法的前后
import pytestclass TestCase01(object):classmethoddef setup_class(cls):print(setup_class)classmethoddef teardown_class(cls):print(teardown_class)classmethoddef setup_method(cls):print(setup_method)classmethoddef teardown_method(cls):print(teardown_method)classmethoddef setup(cls):print(setup)classmethoddef taerdown(cls):print(teardown)def test1(self):print(test1)def test2(self):print(test2)def test3(self):print(test3)4.生成测试报告
1.安装
pip install allure-pytest2.官方文档https://docs.qameta.io/ 3.下载allure 地址https://dl.bintray.com/qameta/generic/io/qameta/allure/2.7.0/