pytest+dubbo接口
2020-03-30 本文已影响0人
dittoyy3991
自动化框架,代码放到测试服务器 == 可telnet到dubbo服务提供者地址
1,dubbo接口,支持加参数发命令,接受返回(代码的原理其实就是dubbo支持telnet 调用格式 telnet 服务IP 端口 ,然后会出现dubbo> ,可以通过ls查看服务列表和invoke调用)
2,其它公共模块:数据库查询,excel处理,yaml解析,
3,pytest框架,用例目录结构,文件所以def_**,class Test**
4,allure格式编写用例
5,数据分离,参数化数据写yaml里
6,jenkins持续集成(+安装allure插件,allure commandline tool,git插件,建job配置--pytest xx --alluredir .jenkins/workspace/allure/allure-result)+allure报告格式+邮件通知()
本地环境配置:
安装jdk1.8,下载allure2.7配置环境变量
pip install pytest
pip install allure-pytest
pip install pytest-repeat
pip install pytest-html
pip install allure-python-commons
pip install pytest-xdist -n=2
pip install pytest-parallel支持多线程 --workers 2 --tests-per-worker 4:2个进程并行,且每个进程最多4个线程运行,即总共最多8个线程运行。
##Linux 下安装allure2.7
mv /app/scripts/allure-2.7.0/ /usr/bin/allure
cd /usr/bin/allure/
export PATH=$PATH:/usr/bin/allure/bin
export PATH=/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
用例编写用allure格式
'''
allure对用例的等级划分成五个等级
blocker 阻塞缺陷(功能未实现,无法下一步)
critical 严重缺陷(功能点缺失)
normal 一般缺陷(边界情况,格式错误)
minor 次要缺陷(界面错误与ui需求不符)
trivial 轻微缺陷(必须项无提示,或者提示不规范)
'''
#allure格式:
test_xx.py下的test_xxx函数,上面加这些个装饰器,
@allure.story("用例story")#用例story---没啥用
@allure.severity("critical")#用例严重程度,默认normal
@allure.testcase("")#用例大链接
@allure.issue("")#用例小链接
@allure.title("用例标题")#用例标题,不加默认为函数名
@allure.step("用例步骤说明")#用例步骤说明
with allure.step('步骤一') # 用于将一个测试用例,分成几个步骤在报告中输出
"""xxxx"""#用例简述Description
conftest.py文件
conftest.py文件(conftest放的fixture下面的函数,是后面test函数里要用到的(入参))
import pytest
@pytest.fixture(scope="session")#session 是多个文件调用一次,可以跨.py文件调用
def loge():
print("用例开始操作")
function1()
yield#结束函数(如果是在setup处异常,是不会执行了的)
print("用例结束操作")
用例里常用装饰器
@pytest.mark.repeat(3)
#用例重复执行几次
@pytest.mark.skip("跳过用例")
#跳过执行
@pytest.mark.demo
#tags标签
#用例数据参数化
1,笛卡尔积
@pytest.mark.parametrize("psw", ["", "admin1", "123"])
@pytest.mark.parametrize("username", ["", "admin2", "124"])
2,一一对应
@pytest.mark.parametrize("username, psw, expected",
[("admin", "123", True),
("admin", "124", False)])
3,读取yaml
a = readyml(patth)
testdata = a['test_add_param_demo']
@pytest.mark.parametrize("test_input, expected", testdata)
pytest 常见执行参数cmd下
pytest
#直接执行目录下所有的test_*.py
pytest -s test_1.py test_1.py
#执行指定的几个test文件
#多进程运行cases
pip install -U pytest-xdist
pytest test_se.py -n NUM
#重试运行cases
pip install -U pytest-rerunfailures
pytest test_se.py --reruns NUM
pytest --html=report.html --self-contained-html
#自带html报告
allure generate report/allure-raw -o allure-reports/ --clean
#allure报告生成
#用例单独调试运行时,末尾加
if __name__ == "__main__":
pytest.main()
pytest.ini执行参数配置
[pytest]
markers =
demo: Run the demo case
demo1: Run the demo1 case
xfail_strict = true
addopts = --html=report.html --self-contained-html
#addopts = --alluredir=./report/allure-raw
norecursedirs = .* build dist CVS _darcs {arch} *.egg venv src
log_cli = 1
dubbotele.py
#!/usr/bin/python
import json,re
import socket
import telnetlib
class Dubbo(telnetlib.Telnet):
prompt = 'dubbo>'
coding = 'utf-8'
def __init__(self, host=None, port=0,
timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
super().__init__(host, port)
self.write(b'\n')
def command(self, flag, str_=""):
data = self.read_until(flag.encode())
self.write(str_.encode() + b"\n")
return data
def invoke(self, service_name, method_name, arg=''):
'''invoke--入参默认为空和dict'''
if isinstance(arg, (dict,)):
arg = json.dumps(arg)#如果是dict,就先转为‘str
command_str = "invoke {0}.{1}({2})".format(
service_name, method_name, arg)
# print('======command_str========'+command_str)
self.command(Dubbo.prompt, command_str)
data = self.command(Dubbo.prompt, "")
data = data.decode(Dubbo.coding,errors='ignore')
data = re.findall(r'result:(.*)', data)[0]#只取result:返回时str或者json
return data
def do(self, arg):
'''执行非invoke命令,直接返回全部ls那种等'''
command_str = arg
self.command(Dubbo.prompt, command_str)
data = self.command(Dubbo.prompt, command_str)
data = data.decode(Dubbo.coding,errors='ignore')
return data
if __name__ == '__main__':
Host = '10.x.x.x' # Doubble服务器IP
Port = 20880 # Doubble服务端口
conn = Dubbo(Host, Port)
# command_str = 'invoke com.test.DubboService.getRes("1","2","3")'
command_str = 'invoke ApplyService.LoanAmount()'
print(conn.do(command_str))
print(conn.invoke('IApplyService','sumAmount'))
# json_data = {
# "applicationKey":"mac",
# "imei":"",
# "mobile":"ssss",
# "createIP":"127.0.0.1",
# "createBy":"172.17.0.1"
# }
# json_data ={'client_id':'ssssss'}
# print(conn.invoke('CreditService','getClientId',json_data))
conn.close()
写完搜索发现,以前别人的思路和我差不多==https://cloud.tencent.com/developer/news/306611
基础学习
==http://www.testclass.net/pytest/