Python接口自动化 实例2

#!/usr/bin/python# coding=utf-8
import requests
import unittest
import pprint
from urllib.parse import urljoin


class RzesApi(object):

    def __init__(self, base_url):
        self.base_url = base_url

    def login(self, username, password, logintype):
        """
        登录接口
        :param username: 用户名
        :param password: 密码
        """
        url = urljoin(self.base_url, 'dologin')
        data = {'username': username, 'password': password, 'logintype': logintype}
        return requests.post(url, data=data).json()

    def get_token(self, username, password, logintype):
        """
        获取登录token
        """
        # r = requests.post(url=self.base_url + 'dologin',
        #                   data={'username': username, 'password': password, 'logintype': logintype})
        # # return r.json()['data']['token']
        # return r.json()
        url = urljoin(self.base_url, 'dologin')
        data = {'username': username, 'password': password, 'logintype': logintype
                }
        return requests.post(url, data=data).text

    def get_cookies(self, username, password, logintype):
        """
        获取登录cookies
        """
        url = urljoin(self.base_url, 'dologin')
        data = {'username': username, 'password': password, 'logintype': logintype
        }
        return requests.post(url, data=data).cookies

    def userinfo(self, cookies):
        """
        详情接口
        """
        url = urljoin(self.base_url, 'userinfo')
        return requests.get(url, cookies=cookies).json()


class TestLogin(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.base_url = 'http://192.168.0.43:8080/pallasa_cloudteachsly/evaluate/api/'
        cls.info_url = 'http://192.168.0.43:8080/pallasa_cloudteachsly/evaluate/api/userinfo'
        cls.username = '01010072sly'
        cls.password = '123456'
        cls.phone = '18270865236'
        cls.logintype = 0
        cls.logintype1 = 1
        cls.app = RzesApi(cls.base_url)

    def test_login1(self):
        """
        测试账号正常登录
        """
        response = self.app.login(self.username, self.password, self.logintype)
        pprint.pprint(response)
        # assert response['msg'] == '登陆成功!'

    def test_login2(self):
        """
        测试手机号登录
        """
        response = self.app.login(self.phone, self.password, self.logintype1)
        pprint.pprint(response)
        # assert response['msg'] == '登录成功!'

    def test_userinfo(self):
        """
        测试获取详情信息
        """
        response = self.app.get_token(self.username, self.password, self.logintype)
        #
        pprint.pprint(response)
        # assert response['code'] == 200
        # assert response['msg'] == 'success'
        # assert response['data'] == 'info'


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