总结
如何自动生成 :
1、copy出一个模板(所有需要在初始化创建的文件) 所有调整需要更新模板
2、创建一个类,用来生成项目 utils–>projectUtil.py
3、步骤:
1、创建项目目录
2、创建项目子目录
3、创建初始化的文件
4、创建一个入口方法,用来调用我们项目生成类 manage.py
以下代码需要学习的点是:
1、读取和写入
read_run = open(os.path.join(CONFIG_PATH, "run_template"), "r")
write_run = open(os.path.join(self.project_path, "run.py"), "w")
run_content = read_run.read()
print(run_content)
write_run.write(run_content)
read_run.close()
write_run.close()
2、根目录的获取,且需要判断新项目是否存在后去创建项目
self.project_path = os.path.join(BASE_PATH, "project", project_name)
def __check_file(self):
if os.path.exists(self.project_path):
quit("项目已存在")
def __create_project_dir(self):
os.mkdir(self.project_path)
3、拼装生成子目录
dir_list = ["cases", "config", "data", "log", "report"]
def __create_child_dir(self):
for dir in dir_list:
# 拼装子目录的路径
dir_path = os.path.join(self.project_path, dir)
os.mkdir(dir_path)
4、生成单元测试报告
suite = unittest.defaultTestLoader.discover(CASE_PATH, case_expr)
report = nnreport.BeautifulReport(suite)
report.report(report_title, report_name, log_path=REPORT_PATH)
error = report.error_count
success = report.success_count
5、学习写法
def create_project():
project_name = input("请输入项目名称:")
p = Project(project_name)
p.main()
func_dict = {
"1": create_project,
"2": clean_log,
"3": bak_db,
"4": run_test
}
if __name__ == '__main__':
choice = input("请输入需要执行的操作:\n"
"1、创建项目 \n"
"2、清除日志\n"
"3、备份数据库\n"
"4、执行测试\n")
if choice in func_dict:
func_dict.get(choice)()
#create_project()
import os
from config.conf import BASE_PATH, CONFIG_PATH
dir_list = ["cases", "config", "data", "log", "report"]
class Project:
def __init__(self, project_name, ):
self.project_name = project_name
self.project_path = os.path.join(BASE_PATH, "project", project_name)
self.__check_file()
def __check_file(self):
if os.path.exists(self.project_path):
quit("项目已存在")
def __create_project_dir(self):
os.mkdir(self.project_path)
def __create_child_dir(self):
for dir in dir_list:
# 拼装子目录的路径
dir_path = os.path.join(self.project_path, dir)
os.mkdir(dir_path)
def __create_run(self):
read_run = open(os.path.join(CONFIG_PATH, "run_template"), "r")
write_run = open(os.path.join(self.project_path, "run.py"), "w")
run_content = read_run.read()
print(run_content)
write_run.write(run_content)
read_run.close()
write_run.close()
def __create_conf(self):
read_run = open(os.path.join(CONFIG_PATH, "conf_template"), "r")
write_run = open(os.path.join(self.project_path, "config", "const.py"), "w")
run_content = read_run.read()
print(run_content)
write_run.write(run_content)
read_run.close()
write_run.close()
def main(self):
print("main")
self.__create_project_dir()
self.__create_child_dir()
self.__create_run()
self.__create_conf()
其中conf_template和run_template为文本文件
#run_template
import unittest
import nnreport
import os, sys
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, ROOT_PATH)
from config.const import CASE_PATH, REPORT_PATH
def run(case_expr, report_title, report_name):
suite = unittest.defaultTestLoader.discover(CASE_PATH, case_expr)
report = nnreport.BeautifulReport(suite)
report.report(report_title, report_name, log_path=REPORT_PATH)
error = report.error_count
success = report.success_count
# 没有写如何报告
report_content = """
hi all:
本次自动化测试执行已完成,共执行了%s条case,成功%s条,失败%s条,详细信息见report:
http://xxxxxxxxxxxx
report.html
""" % (success + error, success, error)
if __name__ == '__main__':
run("Test*", "通过运行程序执行自动化case", "utp-report.html")
#conf_template
import os
from utils.logger import Logger
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
CASE_PATH = os.path.join(BASE_PATH, "cases")
REPORT_PATH = os.path.join(BASE_PATH, "report")
LOG_PATH = os.path.join(BASE_PATH, "log")
DATA_PATH = os.path.join(BASE_PATH, "data")
log = Logger("utp-autoTest", LOG_PATH).get_log()
# .conf ini
MYSQL_INFO = {
"host": "",
"user": "",
"password": "",
"port": 3306,
"db": "",
"charset": 'utf8',
"autocommit": True,
}
REDIS_INFO = {
"host": "",
"password": "",
"port": 6379,
"db": 0,
}
# USER_MAIL_LIST = ['445723280@qq.com']
# USER_MAIL_CC = ['hellolml@163.com']
#
# #host=host,user=user,password=password
# MAIL_INFO = {
# # 'host':'smtp.163.com',
# 'user':'besttest-stu@163.com',
# # 'password': 'besttest-stu',
# 'password':'KZHGBCUMKDQYKEHG',
# 'port':465
# } #邮箱信息
test_env = "test"
host = {
"test": "http://127.0.0.1:5050/"
}
print(BASE_PATH)
总启动代码
from utils.projectUtil import Project
def create_project():
project_name = input("请输入项目名称:")
p = Project(project_name)
p.main()
def clean_log():
pass
def bak_db():
pass
# todo 作业
def run_test():
# 发现所有项目的 case
# 让使用者输入 项目名字 ,然后执行该项目的测试case
pass
func_dict = {
"1": create_project,
"2": clean_log,
"3": bak_db,
"4": run_test
}
if __name__ == '__main__':
choice = input("请输入需要执行的操作:\n"
"1、创建项目 \n"
"2、清除日志\n"
"3、备份数据库\n"
"4、执行测试\n")
if choice in func_dict:
func_dict.get(choice)()
else:
print("输入错误")