使用Python+ behave +request做接口测试

准备:

  • Python
  • pip install behave
  • pip install request

目的:

behave 是一种行为驱动开发的模式,我们这里拿它来做接口测试,似乎有点大材小用。

基础认识:

behave项目如要被执行,需要至少两个文件

  1. feature files,描述了我们要执行的测试功能,步骤,样本数据。
  2. 一个steps文件夹,该文件夹里面有一个python文件,里面包含具体的实施。

步骤

feature 文件:

Feature: http api REQUEST testing
  Scenario Outline: for http api REQUEST testing
    Given request type is <request_type>
    When I input HTTP api <url> and <parametes>
    Then The status code is 200

   Examples: all request type
    |request_type|url                    |parametes|
    |get		|http://wr.com/lenoapi/api/client/getclients? 	|{}|
	|get		|http://wr.com/lenoapi/api/helper/analyzebyurl?	|{'url': 'http://industry.caijing.com.cn/20190321/4572060.shtml'}|
    |post       |http://wr.com/v1/api/account/login			|{'type': 'loginTypes_REQUEST', 'uname': 'w', 'upwd': '81dced055'}|

steps 文件描述了具体的实施:

# coding:utf-8
__author__ = 'wayne'

import requests
from behave import *
from compare import expect
import json

headers={'content-type': 'application/json'}
url1 = "http://wr.com/v1/api/account/login"
data1= {'type': 'loginTypes_REQUEST', 'uname': 'wa', 'upwd': '81dc9ed055'}



def apiTest(method, url, data ,headers=headers):
        try:
            if method == "post":
                results = requests.post(url=url, data=json.dumps(data), headers=headers)
            if method == "get":
                results = requests.get(url=url, params=data, headers=headers)
            if method == "put":
                results = requests.put(url, data=json.dumps(data), headers=headers)
            if method == "delete":
                results = requests.delete(url, headers=headers)
            if method == "options":
                results == requests.options(url, headers=headers)
            #response = results.json()
            return results
        except Exception as e:
            logging.error("service is error", e)

@Given('request type is {request_type}')
def step_impl(context,request_type):
    context.request_type = request_type

@When('I input HTTP api {url} and {parameters}')
def step_impl(context,url,parameters):
    context.url = url
    context.parameters = parameters

@Then('The status code is 200')
def step_impl(context):
    try:
        r= apiTest('post',url1,data1,headers=headers)

        d= r.json()
        mm = d['data']['accesstoken']
	
        acc ={'accesstoken':mm}
		 
        if context.request_type=='get':
            acc.update( eval(context.parameters))
			
            r = apiTest('get',context.url,acc) 			
           
        elif context.request_type == 'post':
            r = apiTest('post',context.url,context.parameters) 			
                        			
        
        expect(r.status_code).to_equal(200)
    
    except requests.HTTPError as e:
        e.strerror

运行behave

总结:

  1. 这里我们用到了compare 这个module ,目的是为了做验证,你也可以使用assert
  2. give/ when / then 这些step 的实现和 feature中的描述是对应的。

猜你喜欢

转载自blog.csdn.net/wangweimic/article/details/89181116
今日推荐