pytest+allure

57 阅读1分钟

pytest顺序

1、pytest插件安装

requirement.txt中包括了所需所有插件,只需在terminal中执行 pip install -r requirement.txt -i pypi.tuna.tsinghua.edu.cn/simple --trust-host pypi.tuna.tsinghua.edu.cn

2、pytest运行文件和运行配置

2.1 运行文件 run.py

2.2 运行配置 pytest.ini

[pytest]
addopts = -vs --html ./reports/report.html -m smoke   pytest命令行 
testpaths = ./testcases       执行测试用例的路径
python_files = test_*.py      执行文件的命名规则
python_classes =            执行类的命名规则
python_functions            执行方法的命名规则
markers =                    执行标记列表
   smoke:冒烟测试

3、书写规范

3.1 所有需要测试得用例、文件、方法都在前面带test_字样,类需加上Test

3.2 req和res方法常用格式

3.2.1 req

3.2.1.1 request.get(url,params,**kwargs)

参数格式

  • data:字典、字节序列或文件对象,作为Request的内容
  • headers:字典,HTTP定制头
  • cookies:字典或CookieJar,Request中的cookie

3.2.1.2 reqeuest.post(url,data)

参数格式

  • json:JSON格式的数据,作为Request的内容
  • data:字典、字节序列或文件对象,作为Request的内容
  • headers:字典,HTTP定制头
  • cookies:字典或CookieJar,Request中的cookie

3.2.1.2 reqeuest.put

3.2.1.2 reqeuest.delete

3.2.2 res

res.text   #返回字符串
res.content #返回字节数据
res.json() #返回json格式
res.status_code #状态码
res.cookie #响应cookie值
res.header #响应头

3.2.3 集合接口

RequestUtil().all_send_request(method,url,**kwargs)

3.2.4 yaml文件

extract.yaml中存放中间变量,在测试用例中,主要使用两种方法来读取(方法存在commons/yaml_util.py)

1read_yaml(key)
2write_yaml(data)

测试用例.yaml

-
  name: 
  request:
    method: 
    url: 
    data:
    headers: 
  validate: 

3.2.4 fixture--主要(实现指定用例的前后置操作)

@pytest.fixture(scope="作用域",params="数据驱动",autouse="是否自动执 行",ids=“参数别名”,name="Fixture别名")
# fixture单独文件命名   conftest.py

##

1、接口