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)

你可能感兴趣的:(Python接口自动化)