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

Posted 开源优测

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[接口测试_B] 13 pytest+requests实战练习相关的知识,希望对你有一定的参考价值。

接续这个文集中的文章: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



测试httpbin接口

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


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文件中编写了一堆用例,并且能够输出报告了,终于踏出了第一步


以上是关于[接口测试_B] 13 pytest+requests实战练习的主要内容,如果未能解决你的问题,请参考以下文章

[接口测试_B] 02 Pytest的简单示例

[接口测试_B] 07 Pytest的测试报告

[接口测试_B] 05 Pytest参数化处理

[接口测试_B] 06 Pytest的setup和teardown

[接口测试_B] 03 Pytest断言处理_assert和异常断言

[接口测试_B] 14 pytest+requests实战-参数化