[本文出自天外归云的博客园]

之前写过一篇关于nose使用方法的博客。最近在做一元乐购产品的接口测试,结合着python的requests库可以很方便的进行web接口测试并生成测试结果。接口测试脚本示例如下(脚本路径为“E:\forPytest\test_new_product_detail.py”):

# -*- coding: utf-8 -*-
from nose.tools import nottest,istest,assert_equal,assert_in
from nose_ittr import IttrMultiplier, ittr
import requests,json '''
用户信息配置
'''
user1 = ""
user2 = ""
pwd = "" class TestNewProductDetail(object):
__metaclass__ = IttrMultiplier '''
非进行中商品
'''
@nottest
@ittr(productId=["2016101716PT022944258","2016101411PT022935002"],accountId=[user1,user2])
def test_new_product_detail_1(self):
s = requests.Session()
url = "https://api.winyylg.com/new_product_detail.html"
periodId = ""
data = {
"productId":self.productId,
"accountId":self.accountId,
"sessionId":getSessionId(s,self.accountId,pwd),
"periodId":periodId
}
r = json.loads(s.get(url=url,params=data).text)
if self.productId == "":
assert_equal(r["result"],-1,msg="productId is null or not exists.")
else:
assert_equal(r["result"],100,msg="result not 100")
basic_asserts(r) '''
没有sessionId
没有accountId
非进行中商品
'''
@nottest
@ittr(productId=["2016101716PT022944258"])
def test_new_product_detail_2(self):
s = requests.Session()
url = "https://api.winyylg.com/new_product_detail.html"
periodId = ""
data = {
"productId":self.productId,
"periodId":periodId
}
r = json.loads(s.get(url=url,params=data).text)
assert_equal(r["result"],100,msg="result not 100")
basic_asserts(r) '''
进行中商品
'''
@istest
@ittr(productId=["2016102016PT023048118"],accountId=[user1,user2])
def test_new_product_detail_3(self):
s = requests.Session()
url = "https://api.winyylg.com/new_product_detail.html"
periodId = ""
data = {
"productId":self.productId,
"accountId":self.accountId,
"sessionId":getSessionId(s,self.accountId,pwd),
"periodId":periodId
}
r = json.loads(s.get(url=url,params=data).text)
if self.productId == "":
assert_equal(r["result"],-1,msg="productId is null or not exists.")
else:
assert_equal(r["result"],100,msg="result not 100")
basic_asserts(r)
for coupon in r["participateInfo"]["couponList"]:
assert_coupon_fields(coupon)
print coupon @nottest
def basic_asserts(result):
print "\n"
tylan_assert_in("resultDesc",result)
tylan_assert_in("product",result)
tylan_assert_in("participateInfo",result)
tylan_assert_in("prizeUser",result)
tylan_assert_in("calculationDetailUrl",result)
tylan_assert_in("imageTextUrl",result)
tylan_assert_in("additionalModel",result)
tylan_assert_in("userParticipateRecords",result)
tylan_assert_in("pastDetails",result)
#print result["participateInfo"]
tylan_assert_in("couponList",result["participateInfo"])
#print result["participateInfo"]["couponList"] @nottest
def assert_coupon_fields(result):
tylan_assert_in("couponId",result)
tylan_assert_in("couponSchemeId",result)
tylan_assert_in("couponType",result)
tylan_assert_in("status",result)
tylan_assert_in("threshold",result)
tylan_assert_in("couponAmount",result)
tylan_assert_in("remainAmount",result)
tylan_assert_in("accountId",result)
tylan_assert_in("takenId",result)
tylan_assert_in("createTime",result)
tylan_assert_in("updateTime",result)
tylan_assert_in("activeTime",result)
tylan_assert_in("expireTime",result)
tylan_assert_in("expireDay",result)
tylan_assert_in("couponName",result)
tylan_assert_in("couponDesc",result)
tylan_assert_in("couponApply",result)
tylan_assert_in("couponApplyDesc",result) @nottest
def tylan_assert_in(a,b):
assert_in(a,b,msg=a+" not include in "+str(b)) @nottest
def getSessionId(session,accountId,pwd):
url = "https://hygtest.ms.netease.com/winyyg/scripts"
data = {
"username":accountId,
"password":pwd,
"tag":"winyylg_login"
}
r = json.loads(session.post(url,data).text)
return r[0][1] '''
新的奖品详情页接口,将原来的奖品详情页的两个接口合成了一个接口:
1. https://api.winyylg.com/product_detail.html
2. https://api.winyylg.com/participate_records.html
新的接口为:https://api.winyylg.com/new_product_detail.html
接口类型为:GET
改动:去掉historyAwardUrl
请求参数:
productId
accountId
sessionId
periodId
返回参数:
result
resultDesc
product
...
participateInfo
...
couponList
...
prizeUser
...
calculationDetailUrl
imageTextUrl
additionalModel
...
userParticipateRecords
pastDetails
'''

在命令行中用例所在的目录下执行命令“nosetests --with-html-output --html-out-file=test_result.html -v”(若想查看脚本中的输出需要在命令结尾再加一个“-s”):

生成的结果文件:

利用nose框架的assert方法、@istest和@nottest装饰器、@ittr装饰器(需安装传参插件)等封装可以很方便的进行测试,再结合python的requests库就可以进行web接口测试了,非常好用。

Python nose单元测试框架结合requests库进行web接口测试的更多相关文章

  1. 【转】使用Python的Requests库进行web接口测试

    原文地址:使用Python的Requests库进行web接口测试 1.Requests简介 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写, ...

  2. Python nose单元测试框架的安装与使用

    [本文出自天外归云的博客园] 安装(Python2下安装) pip install nose 原理与命名规则 Nose会自动查找源文件.目录或者包中的测试用例,符合正则表达式(?:^|[\b_\.%s ...

  3. 使用Python的Requests库进行web接口测试

    1.Requests简介Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2 模块提 ...

  4. Python 单元测试框架系列:聊聊 Python 的单元测试框架(一):unittest

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  5. (转)Python爬虫利器一之Requests库的用法

    官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...

  6. python nose测试框架全面介绍十---用例的跳过

    又来写nose了,这次主要介绍nose中的用例跳过应用,之前也有介绍,见python nose测试框架全面介绍四,但介绍的不详细.下面详细解析下 nose自带的SkipTest 先看看nose自带的S ...

  7. python nose测试框架全面介绍七--日志相关

    引: 之前使用nose框架时,一直使用--logging-config的log文件来生成日志,具体的log配置可见之前python nose测试框架全面介绍四. 但使用一段时间后,发出一个问题,生成的 ...

  8. python nose测试框架全面介绍六--框架函数别名

    之前python nose测试框架全面介绍二中介绍了nose框架的基本构成,但在实际应该中我们也会到setup_function等一系列的名字,查看管网后,我们罗列下nose框架中函数的别名 1.pa ...

  9. Python爬虫利器一之Requests库的用法

    前言 之前我们用了 urllib 库,这个作为入门的工具还是不错的,对了解一些爬虫的基本理念,掌握爬虫爬取的流程有所帮助.入门之后,我们就需要学习一些更加高级的内容和工具来方便我们的爬取.那么这一节来 ...

随机推荐

  1. ROS学习(五)—— 编译ROS Package

    提前准备: 记得事先source你的环境配置(setup)文件,在Ubuntu中的操作指令如下. source /opt/ros/kinetic/setup.bash 一.使用catkin_make ...

  2. 【Linux】分割命令split

    如果你有文件太大,导致一些携带式装置无法复制的问题,嘿嘿!找 split 就对了! 他可以帮你将一个大文件,依据文件大小或行数来分割,就可以将大文件分割成为小文件了! 快速又有效啊!真心不错- [ro ...

  3. linux文件系统 - 初始化(二)

    加载initrd(上) 一.目的 本文主要讲述linux3.10文件系统初始化过程的第二阶段:加载initrd. initrd是一个临时文件系统,由bootload负责加载到内存中,里面包含了基本的可 ...

  4. python md5 问题(TypeError: Unicode-objects must be encoded before hashing)

    import hashlib import sys def md5s(): m=hashlib.md5() strs=sys.argv[1] m.update(strs.encode("ut ...

  5. Windows8.1远程桌面时提示凭据不工作的解决方案

    本人两台电脑都是win8.1.首先确认以下三点: 1.密码没有错 2.用户连接没有达到上线(只有我一个人尝试连) 3.该用户已开启远程连接 此时还说凭据不工作的原因是域的问题,因为mstsc默认使用M ...

  6. kafka 集群的部署安装

    这里我们罗列一下我们的环境 10.19.18.88 zk1 10.19.16.84 zk2 10.19.11.44 zk3 这里公司需要接入kafka用于zipkin来定位调用链 kafka 的地址是 ...

  7. linux上NFS性能参数

    linux nfs客户端对于同时发起的NFS请求数量进行了控制,若该参数配置较小会导致IO性能较差,查看该参数: cat /proc/sys/sunrpc/tcp_slot_table_entries ...

  8. Spring MVC+Hibernate JPA搭建的博客系统项目中所遇到的坑

    标签: springmvc hibernate 2016年12月21日 21:48:035133人阅读 评论(0) 收藏 举报  分类: Spring/Spring MVC(6)  Hibernate ...

  9. Android:使用 DownloadManager 进行版本更新,出现 No Activity found to handle Intent 及解决办法

    项目中,进行版本更新的时候,用的是自己写的下载方案,最近看到了使用系统服务 DownloadManager 进行版本更新,自己也试试. 在下载完成以后,安装更新的时候,出现了一个 crash,抓取的 ...

  10. MQ有啥用

    Q:最近看了一些MSMQ的资料,感觉很是奇怪,在IIS中装上此服务后,感觉这东西就像一个小数据库一样,暂时保存一些发送过来的数据,然后另一端再去收取?A:是的. Q:这样有什么用呢?直接在数据库中建立 ...