def __init__(self): self.host = ConFig().get_conf('sql', 'host') self.user = ConFig().get_conf('sql', 'user') self.password = ConFig().get_conf('sql', 'password') self.port = eval(ConFig().get_conf('sql', 'port')) self.mysql = pymysql.connect( host=self.host, user=self.user, password=self.password, port=self.port) # 这里不传入数据库名,因为真实的项目下,不会就一个数据库的,所以查询语句直接填写对应的数据库名 self.cursor = self.mysql.cursor() # 建立游标
class context_1: #上下文类,数据的管理和准备 admin_mobilephone = ConFig().get_conf('admin','mobilephone') admin_pwd = ConFig().get_conf('admin','pwd') admin_id = ConFig().get_conf('admin','memberId') invest_mobilephone = ConFig().get_conf('invest','invest_mobilephone') invest_pwd = ConFig().get_conf('invest','invest_pwd') invest_id = ConFig().get_conf('invest', 'invest_id') #11642 # def replace(s,d): # p = "\$\{(.*?)}" # while re.search(p,s): #rearch 没有的时候,就返回none,找到一个就返回match,找到一个返回一个 # m=re.search(p,s) # key = m.group(1) # value = d[key] # s = re.sub(p,value,s,count=1) # return s #print(replace()) def replace_1(self,s): p = "\$\{(.*?)}" while re.search(p,s): #rearch 没有的时候,就返回none,找到一个就返回match,找到一个返回一个 m=re.search(p,s) key = m.group(1) if hasattr(context_1,key): #判断是否有key这个属性 value = getattr(context_1, key) # 利用反射动态的获取属性 s = re.sub(p,value,s,count=1) else: return None # 没有就抛出一个异常,告知没有这个属性 return s
from common.do_excel import doexcel from common import get_path from conf.conf1 import ConFig import unittest from ddt import ddt, data from common.request import request cases = doexcel(get_path.cases_path).read_data('invest') from common.test_re import context_1 #from common import test_re #不需要再次导入 from common.mysql import domysql ConFig = ConFig() import json @ddt class investTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.t = request() # 实例化对象 这步很重要,可以确保session运行在同一个对象内 cls.domysql = domysql() #cls.sql = ConFig.get_conf('sql', 'sql_2') #domysql.do_sql(cls.sql) def setUp(self): print('.............开始执行测试用例...............') def tearDown(self): print('.............测试用例执行结束...............')
import requests import json from common import get_path from conf.conf1 import ConFig http = ConFig().get_conf('http', 'url') class request: def __init__(self): self.session = requests.sessions.session() #实例化一个session对象,存取cookies def requests(self, method, url, data): url = http + url print(url) method = method.upper() if method == 'GET': resp = self.session.request(method=method, url=url, params=data) #get方法使用params return resp #返回的resp是一个对象 else: resp = self.session.request(method=method, url=url, data=data) #post方法使用data return resp if __name__ == '__main__': t = request() #to=t.requests('get','/member/register',{"mobilephone":13999997779,"pwd":"123456"}) to = t.requests('get', '/member/login', { "mobilephone": 13799997779,
def __init__(self): self.host = ConFig().get_conf('sql', 'host') self.user = ConFig().get_conf('sql', 'user') self.password = ConFig().get_conf('sql', 'password') self.port = eval(ConFig().get_conf('sql', 'port')) self.mysql = pymysql.connect( host=self.host, user=self.user, password=self.password, port=self.port) # 这里不传入数据库名,因为真实的项目下,不会就一个数据库的,所以查询语句直接填写对应的数据库名 self.cursor = self.mysql.cursor() # 建立游标 def do_sql(self, sql): # 数据库名字也要添加 cursor = self.mysql.cursor() # 建立游标 #sql = ConFig().get_conf('sql', 'sql_2') #封装的时候,不需要写具体的SQL语句 cursor.execute(sql) # 执行SQL语句 res = cursor.fetchone() # 返回的是元祖,返回执行的测试语句 return res def close(self): self.cursor.close() # 关闭浮标 self.mysql.close() # 关闭数据库 if __name__ == '__main__': sql = ConFig().get_conf('sql', 'sql_1') domysql().do_sql(sql)[0] print(int(domysql().do_sql(sql)[0])) domysql().close()
import logging import logging.handlers #需要导入这个 import os from common import get_path from conf.conf1 import ConFig config = ConFig() #封装的时候,不需要必须使(用类 也可以直接使用函数 def get_logger(logger_name): logger = logging.getLogger(logger_name) set_level = config.get_conf('log','ste_level') logger.setLevel(set_level) #日志收集级别 fmt = "%(asctime)s - %(name)s - %(levelname)s - %(pathname)s -%(message)s " #日志输出格式 formate = logging.Formatter(fmt) #日志输出格式 file_name = os.path.join(get_path.log_path,'case.log') #使用绝对地址,最好是使用os.path ,可以确保所有的日志在同一个文件里 print( file_name) file_handler = logging.handlers.RotatingFileHandler( file_name,maxBytes = 20*1024*1024,backupCount=10,encoding='utf-8') #文件输出渠道 out_level = config.get_conf('log','out_level') #RotatingFileHandler 可以定义最大的日志字节 file_handler.setLevel(out_level) #日志输出等级 file_handler.setFormatter(formate) #日志输出格式 logger.addHandler(file_handler) # #以下是输出到控制台 console_handler = logging.StreamHandler() console_level = config.get_conf('log','console_level') console_handler.setLevel(console_level) console_handler.setFormatter(formate) logger.addHandler(console_handler) return logger if __name__ == '__main__': logger = get_logger(logger_name = 'kkk').error('kkk')
from common.request import request from common.do_excel import doexcel import unittest from ddt import ddt, data from common import get_path import json t = request() cases = doexcel(get_path.cases_path).read_data('register') # 获取Excel数据 from conf.conf1 import ConFig from libext.ddtnew import ddt, data cof = ConFig().get_conf('sql', 'sql_1') # 获取SQL语句 from common.mysql import domysql from log.mylog import get_logger logger = get_logger(logger_name='python') @ddt class register(unittest.TestCase): def setUp(self): self.sql_result = int(domysql().do_sql( cof)[0]) # 把SQL语句发到setup语句下,就可以替换接下来的手机号 setup是执行一次测试用例,就执行一次 logger.info('..........开始测试............') def tearDown(self): logger.info('..........测试结束............') @data(*cases) def test_register(self, cases): params = json.loads(cases.param)