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接口测试了,非常好用。

相关文章
|
3月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
4月前
|
缓存 监控 算法
唯品会item_search - 按关键字搜索 VIP 商品接口深度分析及 Python 实现
唯品会item_search接口支持通过关键词、分类、价格等条件检索商品,广泛应用于电商数据分析、竞品监控与市场调研。结合Python可实现搜索、分析、可视化及数据导出,助力精准决策。
|
3月前
|
Java 测试技术 网络安全
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
167 0
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
|
3月前
|
安全 Linux iOS开发
Burp Suite Professional 2025.10 发布 - Web 应用安全、测试和扫描
Burp Suite Professional 2025.10 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
334 3
|
4月前
|
缓存 监控 算法
苏宁item_get - 获得商品详情接口深度# 深度分析及 Python 实现
苏宁易购item_get接口可实时获取商品价格、库存、促销等详情,支持电商数据分析与竞品监控。需认证接入,遵守调用限制,适用于价格监控、销售分析等场景,助力精准营销决策。(238字)
|
4月前
|
监控 算法 数据安全/隐私保护
唯品会 item_get - 获得 VIP 商品详情接口深度分析及 Python 实现
唯品会item_get接口通过商品ID获取商品详情,支持价格、库存、促销等数据抓取,适用于电商分析、竞品监控与价格追踪,结合Python实现可高效完成数据获取、分析与可视化,助力精准营销决策。
|
安全 JavaScript 前端开发
AppSpider 7.5.020 发布 - Web 应用程序安全测试
AppSpider 7.5.020 for Windows - Web 应用程序安全测试
118 0
|
4月前
|
JSON 缓存 供应链
电子元件 item_search - 按关键字搜索商品接口深度分析及 Python 实现
本文深入解析电子元件item_search接口的设计逻辑与Python实现,涵盖参数化筛选、技术指标匹配、供应链属性过滤及替代型号推荐等核心功能,助力高效精准的电子元器件搜索与采购决策。
|
4月前
|
缓存 供应链 芯片
电子元件类商品 item_get - 商品详情接口深度分析及 Python 实现
电子元件商品接口需精准返回型号参数、规格属性、认证及库存等专业数据,支持供应链管理与采购决策。本文详解其接口特性、数据结构与Python实现方案。

推荐镜像

更多