Python nose单元测试框架结合requests库进行web接口测试

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

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

之前写过一篇关于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 = "1"
        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 = "1"
        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 = "1"
        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接口测试了,非常好用。

相关文章
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
740 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
1477 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
685 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
数据可视化 前端开发 测试技术
接口测试新选择:Postman替代方案全解析
在软件开发中,接口测试工具至关重要。Postman长期占据主导地位,但随着国产工具的崛起,越来越多开发者转向更适合中国市场的替代方案——Apifox。它不仅支持中英文切换、完全免费不限人数,还具备强大的可视化操作、自动生成文档和API调试功能,极大简化了开发流程。
|
Java 测试技术 容器
Jmeter工具使用:HTTP接口性能测试实战
希望这篇文章能够帮助你初步理解如何使用JMeter进行HTTP接口性能测试,有兴趣的话,你可以研究更多关于JMeter的内容。记住,只有理解并掌握了这些工具,你才能充分利用它们发挥其应有的价值。+
1908 23
|
SQL 安全 测试技术
2025接口测试全攻略:高并发、安全防护与六大工具实战指南
本文探讨高并发稳定性验证、安全防护实战及六大工具(Postman、RunnerGo、Apipost、JMeter、SoapUI、Fiddler)选型指南,助力构建未来接口测试体系。接口测试旨在验证数据传输、参数合法性、错误处理能力及性能安全性,其重要性体现在早期发现问题、保障系统稳定和支撑持续集成。常用方法包括功能、性能、安全性及兼容性测试,典型场景涵盖前后端分离开发、第三方服务集成与数据一致性检查。选择合适的工具需综合考虑需求与团队协作等因素。
2585 24
|
JSON 前端开发 测试技术
大前端之前端开发接口测试工具postman的使用方法-简单get接口请求测试的使用方法-简单教学一看就会-以实际例子来说明-优雅草卓伊凡
大前端之前端开发接口测试工具postman的使用方法-简单get接口请求测试的使用方法-简单教学一看就会-以实际例子来说明-优雅草卓伊凡
1482 10
大前端之前端开发接口测试工具postman的使用方法-简单get接口请求测试的使用方法-简单教学一看就会-以实际例子来说明-优雅草卓伊凡
|
JSON 前端开发 API
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
1259 5
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
|
SQL 测试技术
除了postman还有什么接口测试工具
最好还是使用国内的接口测试软件,其实国内替换postman的软件有很多,这里我推荐使用yunedit-post这款接口测试工具来代替postman,因为它除了接口测试功能外,在动态参数的支持、后置处理执行sql语句等支持方面做得比较好。而且还有接口分享功能,可以生成接口文档给团队在线浏览。
967 2
|
存储 JSON API
Python测试淘宝店铺所有商品接口的详细指南
本文详细介绍如何使用Python测试淘宝店铺商品接口,涵盖环境搭建、API接入、签名生成、请求发送、数据解析与存储、异常处理等步骤。通过具体代码示例,帮助开发者轻松获取和分析淘宝店铺商品数据,适用于电商运营、市场分析等场景。遵守法规、注意调用频率限制及数据安全,确保应用的稳定性和合法性。

推荐镜像

更多