美文网首页
[接口测试_B] 13 pytest+requests实战练习1

[接口测试_B] 13 pytest+requests实战练习1

作者: 乐大爷L | 来源:发表于2018-06-28 16:24 被阅读430次

接续这个文集中的文章:https://www.jianshu.com/nb/23293583

找不到免费的接口,所以依然以requests官方文档中出现的httpbin为示例,为了保持网络的稳定,可以把httpbin搭建在本地。

httpbin环境搭建

  • VMware12
  • ubuntu kylin 16.04_x64
  • python3.5
sudo apt-get install python3-pip
  • 安装虚拟环境,pipenv:
python3 -m pip install pipenv
# 在项目目录下
pipenv install
# 进入虚拟环境
pipenv shell

在安装pip后,创建虚拟环境,安装httpbin和gunicorn:

pipenv install httpbin
pipenv install gunicorn

启动httpbin,如果启动后,宿主机无法访问,查看防火墙设置,把端口加入iptables:

gunicorn -b :8088 httpbin:app
image.png image.png

测试httpbin接口

前面了解requests的过程中,多次用到httpbin接口做示例。对这个接口应该比较熟悉了。先实现简单的接口测试。

"""
Created by catleer on 2018-05-21.
"""
__author__ = 'catleer'

import re
from urllib.parse import urljoin

import requests
import pytest


url = 'http://192.168.86.131:8088/'

methods = ['GET', 'POST', 'PATCH', 'DELETE', 'PUT']
method = methods[1]
interface = ['HTTP methods', 'Auth', 'Status Codes', 'Requests Inspection',
             'Response Inspection', 'Response Formats', 'Dynamic data',
             'Cookies', 'Images', 'Redirects', 'Anything']


class TestHttpMethods:

    @classmethod
    def setup_class(cls):
        cls.url = 'http://192.168.86.131:8088/'
        cls.data = {'haha': 'nihao'}

    def test_get(self):
        url = urljoin(self.url, 'get')
        r = requests.get(url, params=self.data).json()
        print(r)
        # assert r['url'] == url
        assert r['args'] == self.data

    def test_post(self):
        url = urljoin(self.url, 'post')
        r = requests.post(url, data=self.data).json()
        assert r['url'] == url
        assert r['form'] == self.data

    def test_patch(self):
        url = urljoin(self.url, 'patch')
        r = requests.patch(url, data=self.data).json()
        assert r['url'] == url
        assert r['form'] == self.data

    def test_put(self):
        url = urljoin(self.url, 'put')
        r = requests.put(url, data=self.data).json()
        assert r['url'] == url
        assert r['form'] == self.data

    def test_delete(self):
        url = urljoin(self.url, 'delete')
        r = requests.delete(url, data=self.data).json()
        assert r['url'] == url
        assert r['form'] == self.data

class TestAuth:
    @classmethod
    def setup_class(cls):
        cls.url = 'http://192.168.86.131:8088/'
        # cls.data = {'haha': 'nihao'}

    def test_basicauth_y(self):
        data = 'aa', '123'
        url = urljoin(self.url, '/'.join(('basic-auth',) + data))
        print(data)
        r = requests.get(url, auth=(data)).json()
        print(r)
        assert r['user'] == data[0]

    def test_basicauth_n(self):
        data = ('aa', '123')
        url = urljoin(self.url, '/'.join(('basic-auth',) + data))
        r = requests.get(url, auth=('aa', 'bb'))
        assert r.status_code == 401

    def test_bearer_y(self):
        headers = {'Authorization': 'justtestauth'}
        url = urljoin(self.url, 'bearer')
        r = requests.get(url, headers=headers)
        assert r.status_code == 200
        r = r.json()
        assert r['token'] == headers['Authorization']

    def test_bearer_n(self):
        headers = {}
        url = urljoin(self.url, 'bearer')
        r = requests.get(url, headers=headers)
        assert r.status_code == 401

运行pytest命令

  • 运行pytest命令直接查看结果:
pytest -q run.py
  • 运行pytest命令生成测试报告:
pytest -q --tb=no --html=E:\python_interface_test\results\makehtml.html

总结

  • 在一个py文件中编写了一堆用例,并且能够输出报告了,终于踏出了第一步
  • 缺点说明:
    1、没有进行数据隔离:接口、请求参数、断言数据都在同一文件中;
    2、不能设置配置信息:如测试地址改变等,不能直接通过配置文件修改;
    3、没有记录日志信息:没有记录接口的执行信息,如果出现bug,无法提供定位信息;

相关文章

网友评论

      本文标题:[接口测试_B] 13 pytest+requests实战练习1

      本文链接:https://www.haomeiwen.com/subject/qnaslftx.html