util--urlutil
#-*- coding:utf-8 -*-
# 定义类
class URL(object):
# 定义类
# 测试环境
SERVER_URL = "http://39.104.88.42:8080/jd/"
# 生成环境
# SERVER_URL = "http://www.baidu.com/jd/"
# 登陆的接口
LOGIN_URL = "login"
# 登陆的参数
LOGIN_OBJECT = {
"username":"zhangsan1",
"password":"123456"
}
unit--login
#-*- coding:utf-8 -*-
# 导入 单元测试
import unittest
# 导入网络请求框架
import requests
# 导入工具类
from util import urlutil
# 导入数据库
import MySQLdb
# 声明类
class Login(unittest.TestCase):
# 实例化对象
@classmethod
def setUpClass(self):
# 实例化
self.login = urlutil.URL()
pass
# 测试用例运行之前的方法
def setUp(self):
pass
# 测试用例运行之后的
def tearDown(self):
pass
# 登陆的测试用例
def test_login_us_pw(self):
# 请求网络
self.login_object = requests.post(
self.login.SERVER_URL+self.login.LOGIN_URL
,self.login.LOGIN_OBJECT)
# 打印返回值
print self.login_object.json()
# 进行解析
self.json = self.login_object.json()
# 获取内容
self.userId = self.json['userId']
self.token = self.json['tpken']
print self.userId,self.token
# 链接数据库,查询数据,进行数据对比
# 设置host
host = "116.62.200.230"
# 设置 用户名
user = "root"
# 设置密码
pwd = "root"
# 设置数据量
database = "qiezzi"
# 设置端口号
port = 3306
# 链接数据库
db = MySQLdb.connect(host,user,pwd,database,port)
# 获取游标
course = db.cursor()
# 执行sql语句
sql = "select * from qiezzilogin where username = 'zhangsan1'"
course.execute(sql)
# 获取persions 对象
persons = course.fetchall()
# 使用for循环
for person in persons:
# 获取数据
self.table_userid = person[0]
# 获取token
self.table_token = person[3]
print person[0],person[1]
# 进行断言
self.assertEqual(self.userId,self.table_userid)
# 断言token
self.assertEqual(self.token,self.table_token)
pass
suit -- suit:
#-*- coding:utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf8') import unittest from unit import login import HTMLTestRunner import os suit = unittest.TestSuite() suit.addTest(unittest.makeSuite(login.Login)) files=os.getcwd()+"/jd.html" filename=open(files,'wb') runner= HTMLTestRunner.HTMLTestRunner(title=u'茄子',description=u'茄子医生测试用例',stream=filename) runner.run(suit)