python 接口自动化 实例1

#!/usr/bin/env python
#-*-coding:utf-8-*-

import requests
import unittest
import time as t
import os
import pprint


def getHeaders():
    """获取headers """
    return {"Host": "hd.ruizhiedu.com:8888",
                        "Connection": "keep-alive",
                        "Content-Length": "53",
                        "Accept": "application/json, text/javascript, */*; q=0.01",
                        "Origin": "http://hd.ruizhiedu.com:8888",
                        "X-Requested-With": "XMLHttpRequest",
                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
                                      "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36",
                        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                        # "Referer": "http://hd.ruizhiedu.com:8888/pallasa_cloudteach/teacher/index/index",
                        "Accept-Encoding": "gzip, deflate",
                        "Accept-Language": "zh-CN,zh;q=0.9"}


def url():
    """ url"""
    return "http://hd.ruizhiedu.com:8888/pallasa_cloudteach/teacher/"


def login():
    """把token写入到文件中"""
    r = requests.post(
        url=url() + 'login',
        payload={"username": "01019992gww",
                 "password": "123456"},
        headers=getHeaders(), timeout=5)
    with open(base_dir(), 'w') as f:
        f.write(r.json()['data']['token'])


def base_dir():
    """获取当前文件的目录"""
    return os.path.join(os.path.dirname(__file__), 'token.md')


def gettoken():
    """读取存储在文件中的token"""
    with open(base_dir(), 'r') as f:
        return f.read()


class CloudTeacherLoginTest(unittest.TestCase):
    def setUp(self):
        self.url = "http://hd.ruizhiedu.com:8888/pallasa_cloudteach/teacher/"
        self.headers = {"Host": "hd.ruizhiedu.com:8888",
                          "Connection": "keep-alive",
                          "Content-Length": "53",
                          "Accept": "application/json, text/javascript, */*; q=0.01",
                          "Origin": "http://hd.ruizhiedu.com:8888",
                          "X-Requested-With": "XMLHttpRequest",
                          "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
                                        "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36",
                          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                          "Referer": "http://hd.ruizhiedu.com:8888/pallasa_cloudteach/teacher/index/index",
                          "Accept-Encoding": "gzip, deflate",
                          "Accept-Language": "zh-CN,zh;q=0.9"}
        self.payload = {"username": "01019992gww",
                        "password": "123456"}

    def tearDown(self):
        t.sleep(1)

    def test1_login(self):
        """验证:测试login接口是否正确"""
        self.r = requests.post(url=self.url+'index/login',
                               data=self.payload, headers=getHeaders(), timeout=5)

        self.assertIn("0", self.r.text)


    def test2_logout(self):
        """验证:测试/index/logout接口是否正确"""
        self.r = requests.post(url=self.url+'index/logout',
                               data={"token": gettoken()}, headers=getHeaders(), timeout=5)
        self.assertIn("0", self.r.text)


if __name__ == '__main__':
    unittest.main(verbosity=2)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值