[关闭]
@coolfish 2017-04-10T03:47:29.000000Z 字数 6415 阅读 1398

一、安装&更新

1.1、内网环境

  1. # 安装
  2. pip install pithy --extra-index-url http://pypi.dev.elenet.me/eleme/eleme --trusted-host pypi.dev.elenet.me
  3. # 更新
  4. pip install -U pithy --extra-index-url http://pypi.dev.elenet.me/eleme/eleme --trusted-host pypi.dev.elenet.me

1.2、线上环境

  1. # 安装
  2. pip install pithy --extra-index-url http://pypi.elenet.me/eleme/eleme --trusted-host pypi.elenet.me
  3. # 更新
  4. pip install -U pithy --extra-index-url http://pypi.elenet.me/eleme/eleme --trusted-host pypi.elenet.me

二、生成接口测试项目

  1. (pyevn)➜pithy-cli init
  2. 请选择项目类型,输入interface或者app: # 输入项目类型
  3. 请输入项目名称,如sargeras-api-test: # 输入项目名称
  4. 开始创建hgbac项目
  5. 开始渲染...
  6. 开始渲染 interface/.gitignore.jinja
  7. 开始渲染 interface/apis/__init__.py.jinja
  8. 开始渲染 interface/common/__init__.py.jinja
  9. 开始渲染 interface/db/__init__.py.jinja
  10. 开始渲染 interface/README.MD.jinja
  11. 开始渲染 interface/requirements.txt.jinja
  12. 开始渲染 interface/test_suites/__init__.py.jinja
  13. 生成成功,请使用编辑器打开该项目

进入项目文件夹内,执行git init,然后与gitlib上的项目仓库关联

三、接口测试

3.1、HTTP接口

3.1.1、使用POST方法

  1. from pithy import request
  2. @request(url='http://xxxx/login', method='post')
  3. def login(phone=None, password=None):
  4. """
  5. 登录
  6. """
  7. headers = {'xx': xx, 'xx': xx}
  8. body = {
  9. 'phone': phone,
  10. 'password': password
  11. }
  12. # 使用
  13. response = login('13111111111', '123abc').to_json() # 解析json字符,输出为字典
  14. response = login('13111111111', '123abc').json # 解析json字符,输出为字典
  15. response = login('13111111111', '123abc').to_content() # 输出为字符串
  16. response = login('13111111111', '123abc').content # 输出为字符串
  17. response = login('13111111111', '123abc').get_cookie() # 输出cookie对象
  18. response = login('13111111111', '123abc').cookie # 输出cookie对象
  19. # 结果取值, 假设此处response = {'a': 1, 'b': { 'c': [1, 2, 3, 4]}}
  20. response = login('13111111111', '123abc').json
  21. print response.b.c # 通过点号取值,结果为[1, 2, 3, 4]
  22. print response('$.a') # 通过object path取值,结果为1
  23. for i in response('$..c[@>3]'): # 通过object path取值,结果为选中c字典里大于3的元素
  24. print i

3.1.2、使用GET方法,数据传输方式为json方式

  1. from pithy import request
  2. @request(url='http://xxxx/login')
  3. def login(phone=None, password=None):
  4. """
  5. 登录
  6. """
  7. headers = {'xx': xx, 'xx': xx}
  8. params = {
  9. 'phone': phone,
  10. 'password': password
  11. }

3.1.3、使用POST方法,数据传输方式为form方式

  1. from pithy import request
  2. @request(url='http://xxxx/login', method='post', data_type='form')
  3. def login(phone=None, password=None):
  4. """
  5. 登录
  6. """
  7. headers = {'xx': xx, 'xx': xx}
  8. body = {
  9. 'phone': phone,
  10. 'password': password
  11. }

3.1.4、使用类的方式组织用接口,使用同一session,指定base_url

  1. from pithy import request
  2. class PithyApp(object):
  3. """
  4. PithyApp 相关接口
  5. """
  6. def __init__(self):
  7. self.session = None # 定义session,方便后面操作, 同时也可以不定义self.session,会自动创建
  8. self.base_url = 'http://xxx.com' # 指定base_url,也可以不指定
  9. @request(url='login', method='form')
  10. def _login(self, phone=None, password=None):
  11. """
  12. 登录接口
  13. """
  14. headers = {'xx': xx, 'xx': xx}
  15. body = {
  16. 'phone': phone,
  17. 'password': password
  18. }
  19. def login(self, phone=None, password=None):
  20. """
  21. 登录
  22. """
  23. login_response = self._login('13111111111', '111111').to_json()
  24. self.session.cookies.set('session', login_response.data.session)
  25. @request(url='pithy-webapi/get_info')
  26. def get_info(self):
  27. """
  28. 获取信息
  29. """
  30. pass
  31. # 使用,此处两个接口使用同一request session请求
  32. pithy = Pithy()
  33. pithy.login('13111111111', '123abc').json
  34. pithy.get_info().json

3.1.5、不使用类组织接口,使用指定的session

  1. from pithy import request, make_session
  2. @request(url=config.BASE_URL + '/pithy-webapi/login', method='form')
  3. def login(phone=None, password=None, session=None):
  4. """
  5. 登录
  6. """
  7. headers = {'xx': xx, 'xx': xx}
  8. body = {
  9. 'phone': phone,
  10. 'password': password
  11. }
  12. # 调用
  13. session = make_session()
  14. response = login('13111111111', '123abc', session=session).json

四、数据库相关

这里使用sqlalchemy orm,使用方法如下:

  1. # 因执行了sys.module['pithy_db'] = xxx,所以当加载后,可以直接from pithy_db import ...
  2. from pithy import db
  3. pithy_db = db("mysql://user:password@host:port/database?charset=utf8", 'pithy_db')
  4. # 使用从模块导入方式
  5. from pithy_db import o_order, session
  6. query_result = session.query(o_order).all()
  7. # 不使用从模块导入的方式
  8. o_order = pithy_db.o_order
  9. session = pithy_db.session
  10. session.query(o_order).all()

五、工具类

5.1、操作日期函数

  1. from pithy import HumanDateTime
  2. # 解析时间戳
  3. print repr(HumanDateTime(1490842267))
  4. print HumanDateTime(1490842267000)
  5. print HumanDateTime(1490842267.11111)
  6. print HumanDateTime(1490842267111.01)
  7. # 解析字符串格式日期
  8. print HumanDateTime('2017-02-02')
  9. print HumanDateTime('Thu Mar 30 14:21:20 2017')
  10. print HumanDateTime(time.ctime())
  11. print HumanDateTime('2017-3-3')
  12. print HumanDateTime('3/3/2016')
  13. print HumanDateTime('2017-02-02 00:00:00')
  14. # 解析datetime或date类型时间
  15. print HumanDateTime(datetime(year=2018, month=11, day=30, hour=11))
  16. print HumanDateTime(date(year=2018, month=11, day=30))
  17. # 增加减少时间
  18. print HumanDateTime('2017-02-02').add_day(1)
  19. print HumanDateTime('2017-02-02').sub_day(1)
  20. print HumanDateTime('2017-02-02').add_hour(1)
  21. print HumanDateTime('2017-02-02').sub_hour(1)
  22. print HumanDateTime('2017-02-02').add(days=1, hours=1, weeks=1, minutes=1, seconds=6)
  23. print HumanDateTime('2017-02-02').sub(days=1, hours=1, weeks=1, minutes=1, seconds=6)
  24. # 转换为时间戳
  25. print HumanDateTime(1490842267.11111).timestamp_second
  26. print HumanDateTime(1490842267.11111).timestamp_microsecond
  27. print HumanDateTime('2017-02-02 12:12:12.1111').add_day(1).timestamp_microsecond
  28. print HumanDateTime('2017-02-02 12:12:12 1111').add_day(1).timestamp_microsecond
  29. # 比较大小
  30. print HumanDateTime('2017-02-02 12:12:12 1111') < HumanDateTime('2017-02-02 12:12:11 1111')
  31. print HumanDateTime('2017-02-02 12:12:12 1111') < HumanDateTime('2017-02-02 12:13:11 1111')
  32. print HumanDateTime('2017-02-02 12:12:12 1111') < '2017-02-02 12:11:11'
  33. print HumanDateTime('2017-02-02 12:12:12 1111') < '2017-02-02 12:13:11 1111'
  34. print HumanDateTime('2017-02-02 12:12:12 1111') == '2017-02-02 12:13:11 1111'
  35. print HumanDateTime('2017-02-02 12:12:12 1111') == '2017-02-02 12:13:12 1111'
  36. print HumanDateTime('2017-02-02 12:12:12 1111') <= '2017-02-02 12:13:11 1111'
  37. print HumanDateTime('2017-02-02 12:12:12 1111') >= '2017-02-02 12:13:11 1111'
  38. print HumanDateTime('2017-02-02 12:12:12 1111') != time.time()
  39. print HumanDateTime('2017-02-02 12:12:12 1111') <= time.time()
  40. print HumanDateTime('2017-02-02 12:12:12 1111') >= time.time()
  41. # 约等于或者接近
  42. print HumanDateTime('2017-02-02 12:12:12 1111').approach('2017-02-02 12:12:11 1111')
  43. print HumanDateTime('2017-02-02 12:12:12 1111').approach('2017-02-02 12:12:10 1111')
  44. print HumanDateTime('2017-02-02 12:12:12 1111').approach('2017-02-02 12:12:10 1111', offset=2)
  45. print HumanDateTime('2017-02-02 12:12:12 1111').approach('2017-02-02 12:12:14 1111', offset=2)
  46. # 调用datetime的方法和属性
  47. print HumanDateTime('2017-02-02 12:12:12 1111').day
  48. print HumanDateTime('2017-02-02 12:12:12 1111').year
  49. print HumanDateTime('2017-02-02 12:12:12 1111').second
  50. print HumanDateTime('2017-02-02 12:12:12 1111').date()

5.2、操作复杂JSON或字典

优化JSON字符串和字典的取值方式

  1. # 1、操作JSON的KEY
  2. from pithy.tools import JSONProcessor
  3. dict_data = {'a': 1, 'b': {'a': [1, 2, 3, 4]}}
  4. json_data = json.dumps(dict_data)
  5. result = JSONProcessor(json_data)
  6. print result.a # 结果:1
  7. print result.b.a # 结果:[1, 2, 3, 4]
  8. # 这里使用的object path的取值方式,详细语法见:http://objectpath.org/reference.html
  9. for i in result('$..a[@>3]'): # 结果: 4
  10. print i
  11. # 2、操作字典的KEY
  12. dict_data = {'a': 1, 'b': {'a': [1, 2, 3, 4]}}
  13. result = JSONProcessor(dict_data)
  14. print result.a # 1
  15. print result.b.a # [1, 2, 3, 4]
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注