Robotframework 入门教程(九)

== 使用RF驱动http接口测试==

简单写一个利用python requests库发送请求的Library,例如下面的reqLib,实现了基本的get和post请求和一个校验http_code的方法。
可以根据需要添加些url和参数的预处理添加个签名,时间戳之类的,或一些自定义的校验方法检查某个字段的值。
import requests
from robot.api import logger


class reqLib(object):
    def __init__(self):
        self.session = requests.Session()
        self.response = None

    def _send(self, method, url, params=None, data=None, headers=None, files=None):
        """
        send request.
        """
        req = requests.Request(method, url, params=params, data=data, headers=headers, files=files)
        prepped = self.session.prepare_request(req)
        logger.info('=  = \n'+str(params))
        logger.info('=  = \n'+str(data))
        self.response = self.session.send(prepped)
        logger.info('=  = \n'+str(self.response.status_code))
        logger.info('=  = \n'+str(self.response.content))
        return self.response

    def get(self, url, **params):
        """
        send get request

        Params:
        | url | request url|
        | params | query args dict |

        Return: response object
        """
        # params = self._add_public_para(**params) # url = self._handle_url(url)         self.response = self._send('GET', url, params=params)
        return self.response

    def post(self, **params):
        """
        send post request

        Params:
        | url | request url|
        | params | body args dict |

        Return: response object
        """
        # params = self._add_public_para(**params)
 # url = self._handle_url(url)
 self.response = self._send('POST', url, data=params) return self.response def check_status_code(self, code): """ check response http code Params: | code | expected http code | Examples: | check_status_code | 400 | """ assert int(code) == self.response.status_code, \ 'The expected code is {}, actually {}'.format(code, self.response.status_code)
 然后就是创建一个testsuite调用这个custome Library 
   
*** Settings ***
Library           reqLib.py

*** Test Cases ***
get test
    get    http://httpbin.org/get    action=hadogen
    check status code    200








你可能感兴趣的:(Robotframework 入门教程(九))