Commit eb6e6f8a1a65ac24104f8878ee66690a361fc5a0
Committed by
liujiqiang
1 parent
ff1cef5f
删除之前的main,把之前的main_bu修改为main
Showing
2 changed files
with
51 additions
and
281 deletions
src/main.py
1 | 1 | #!/usr/bin/python |
2 | 2 | # -*- coding: UTF-8 -*- |
3 | -import sys,os | |
4 | -import getopt | |
3 | +import os,time | |
4 | +import sys | |
5 | 5 | import unittest |
6 | -import time | |
7 | -# import HTMLTestRunner | |
6 | +import re | |
8 | 7 | import HTMLTestRunner_cn |
9 | -import commons.SendEmail as em | |
10 | -import commons.common as com | |
11 | -from commons.Logging import Logger | |
12 | -#DiscoveringTestLoader解决了代码在liunx无法成功加载用例的问题 | |
13 | 8 | from discover import DiscoveringTestLoader |
14 | -log=Logger() | |
9 | +from commons import SendEmail as em | |
10 | +from commons import common as com | |
15 | 11 | |
16 | - | |
17 | -def Create_Testcase_suite(filename=None,testclass=None,casename=None,emailaddr=False,priority=False,casemark=None): | |
18 | - '''可以拓展该方法,为读取相对路径,然后传入指定文件测试用例: | |
19 | - Create_Testcase_suite(最顶层用例文件Case_Sences,下一级具体接口文件,) | |
20 | - ''' | |
12 | +def Create_Testcase_suite(): | |
13 | + '''创建测试套件''' | |
21 | 14 | testunit=unittest.TestSuite() |
22 | 15 | test_Loader = DiscoveringTestLoader() |
23 | - #可以通过a="./Case_Report/test2.txt" 和a="E:/EclipseWorkspace/WorksapceDemo/Request-demo/src/Case_Report/test2.txt" | |
24 | - #来获取指定文件内容 | |
25 | - path="./testcase/" | |
26 | - if filename: | |
27 | - #path=os.path.dirname(__file__)不能通过命令行获取当前文件路径,返回路径为空 | |
28 | - #path=os.path.dirname(__file__) | |
29 | -# filename=path+"/testcase/"+filename | |
30 | - filename=path+filename | |
31 | - print("filename",filename) | |
32 | - else: | |
33 | -# listaa = "E:\\EclipseWorkspace\\nong12test\\Request-fresh\\src\\testcase\\" | |
34 | - filename=path | |
35 | - | |
36 | - log.info("用例路径%s"%path) | |
37 | - print("filename==",filename) | |
38 | - #discover会展示有多少test_*.py的文件,然后for依次添加这些文件夹下test_*.py里面的testcase | |
39 | - #这个方法会识别指定目录下所有test_*.py文件和test_*.py文件中所有class *(unittest.TestCase)以及其中的case | |
40 | - #每一个接口文件下面都得添加一个_init_.py不然无法识别该文件下的test_*.py | |
41 | - #_init_.py把所在目录当作一个package处理, from-import 语句导入子包时需要用到它。 如果没有用到, 他们可以是空文件 | |
42 | - #如引入package.module下的所有模块from package.module import * | |
43 | - #submitOrder文件下不加_init_.py时,test_submitOrder文件无法被搜索 | |
44 | - | |
45 | - #提前设置用户自定义用例等级,提前设置全局变量以免报错 | |
46 | - com.set_priority(priority) | |
47 | - #提前设置用户自定义用例标记,提前设置全局变量以免报错 | |
48 | - com.set_mark(casemark) | |
49 | - #discover会把指定的文件都加载一遍,会把所有文件中class测试类外和if __name__ == "__main__"外的代码加载一遍, | |
50 | - #如果测试文件中有import公共方法Case_Method.ConfigAction,系统并不会去加载公共方法内的代码 | |
51 | - #如果通过创建全局变量来控制case的优先级并再py文件中读取全局变量时,要提前给全局变量赋值,以免在discover用例文件时,全局变量还为空,会报错 | |
52 | - log.info("222222") | |
53 | - if filename and testclass and casename: | |
54 | - print("1-1-1") | |
55 | - discover=test_Loader.discover(filename,pattern='test_*.py',top_level_dir=None) | |
56 | - for test_suite in discover: | |
57 | - for test_class in test_suite: | |
58 | - if testclass in str(test_class): | |
59 | - for test_num in test_class: | |
60 | - if casename in str(test_num): | |
61 | - testunit.addTest(test_num) | |
62 | - | |
63 | - elif filename and testclass and (not casename): | |
64 | - print("1-1-0") | |
65 | - discover=test_Loader.discover(filename,pattern='test_*.py',top_level_dir=None) | |
66 | - for test_suite in discover: | |
67 | - for test_class in test_suite: | |
68 | - if testclass in str(test_class): | |
69 | - testunit.addTests(test_class) | |
70 | - | |
71 | - elif filename and (not testclass) and (not casename): | |
72 | - print("1-0-0") | |
73 | - discover=test_Loader.discover(filename,pattern='test_*.py',top_level_dir=None) | |
74 | - for test_suite in discover: | |
75 | - for test_class in test_suite: | |
76 | - testunit.addTests(test_class) | |
77 | - | |
78 | - elif (not filename) and (not testclass) and (not casename): | |
79 | - print("0-0-0") | |
80 | - discover=test_Loader.discover(filename,pattern='test_*.py',top_level_dir=None) | |
81 | - for test_suite in discover: | |
82 | - for test_class in test_suite: | |
83 | - testunit.addTests(test_class) | |
84 | - | |
85 | - elif (not filename) and testclass and (not casename): | |
86 | - print("0-1-0") | |
87 | - discover=test_Loader.discover(filename,pattern='test_*.py',top_level_dir=None) | |
88 | - for test_suite in discover: | |
89 | - for test_class in test_suite: | |
90 | - if testclass in str(test_class): | |
91 | - testunit.addTests(test_class) | |
92 | - | |
93 | - elif (not filename) and testclass and casename: | |
94 | - print("0-1-1") | |
95 | - discover=test_Loader.discover(filename,pattern='test_*.py',top_level_dir=None) | |
96 | - for test_suite in discover: | |
97 | - for test_class in test_suite: | |
98 | - if testclass in str(test_class): | |
99 | - for test_num in test_class: | |
100 | - if casename in str(test_num): | |
101 | - testunit.addTest(test_num) | |
102 | - | |
103 | - elif filename and (not testclass) and casename: | |
104 | - print("1-0-1") | |
105 | - discover=test_Loader.discover(filename,pattern='test_*.py',top_level_dir=None) | |
106 | - for test_suite in discover: | |
107 | - for test_class in test_suite: | |
108 | - for test_num in test_class: | |
109 | - if casename in str(test_num): | |
110 | - testunit.addTest(test_num) | |
111 | - else: | |
112 | - print("0-0-1") | |
113 | - discover=test_Loader.discover(filename,pattern='test_*.py',top_level_dir=None) | |
114 | - for test_suite in discover: | |
115 | - for test_class in test_suite: | |
116 | - for test_num in test_class: | |
117 | - if casename in str(test_num): | |
118 | - testunit.addTest(test_num) | |
119 | - | |
16 | + discover=test_Loader.discover("./testcase/",pattern='test_*.py',top_level_dir=None) | |
17 | + print(discover) | |
18 | + for test_suite in discover: | |
19 | + testunit.addTests(test_suite) | |
20 | +# for test_case in test_suite: | |
21 | +# testunit.addTests(test_case) | |
22 | +# print(testunit) | |
120 | 23 | print(testunit) |
121 | - log.info("发送的邮件地址%s:"%emailaddr) | |
122 | - log.info("运行的用例优先级%s:"%priority) | |
123 | - log.info("运行的用例标记%s:"%casemark) | |
124 | - log.info("测试用例加载结果%s"%testunit) | |
24 | + return testunit | |
125 | 25 | |
26 | + | |
27 | +def Run_Testcase(testsuit): | |
28 | + '''运行测试用例并生成报告''' | |
126 | 29 | now = time.strftime("%Y-%m-%d %H_%M_%S",time.localtime()) |
127 | - #获取当前文件的路径,sys.argv[0]为当前文件 | |
128 | - path=os.path.dirname(os.path.abspath(sys.argv[0])) | |
129 | - #filename="E:\\EclipseWorkspace\\nong12test\\Request-fresh\\src\\Case_Report\\"+now+"_result.html" | |
30 | + path=os.path.dirname(os.path.abspath(sys.argv[0])) | |
130 | 31 | report_file=path+"/report/"+now+"_result.html" |
131 | - log.info("测试报告路径%s:"%report_file) | |
32 | + #创建报告文件 | |
132 | 33 | fp=open(report_file,'wb') |
133 | - | |
34 | + | |
134 | 35 | runner=HTMLTestRunner_cn.HTMLTestRunner( |
135 | 36 | stream=fp, |
136 | - title=u'搜索功能测试报告', #设置HTML报告的title | |
137 | - description=u'用例执行情况:',#设置HTML报告内容中的描述 | |
138 | - verbosity = 2,#设置报告详细级别,设置为1时有bug,无法失败连续重跑,最好为2(已验证连续3个用例重跑) | |
139 | - retry=2, #设置重跑次数,默认为0 | |
140 | - save_last_try=False) #为True,一个用例仅显示最后一次测试的结果;为False,则显示所有重试的结果; | |
141 | - | |
142 | - log.info("********运行测试用例********") | |
143 | - runner.run(testunit) | |
37 | + title=u'搜索功能测试报告', | |
38 | + description=u'用例执行情况:', | |
39 | + verbosity = 2) | |
40 | + #执行用例 | |
41 | + runner.run(testsuit) | |
42 | + #关闭文件 | |
144 | 43 | fp.close() |
145 | - | |
146 | - if emailaddr: | |
147 | - em.send_email(report_file,emailaddr) | |
148 | - else: | |
149 | - em.send_email(report_file) | |
150 | - | |
151 | -def main_get(argv): | |
152 | - """ | |
153 | - 通过 getopt模块 来识别参数demo, http://blog.csdn.net/ouyang_peng/ | |
154 | - """ | |
155 | - | |
156 | - _filename = False | |
157 | - _testclass = False | |
158 | - _casename = False | |
159 | - _emailaddr = [] | |
160 | - _priority = [] | |
161 | - _casemark = False | |
44 | + return report_file | |
162 | 45 | |
163 | - try: | |
164 | - """ | |
165 | - options, args = getopt.getopt(args, shortopts, longopts=[]) | |
166 | - | |
167 | - 参数args:一般是sys.argv[1:]。过滤掉sys.argv[0],它是执行脚本的名字,不算做命令行参数。 | |
168 | - 参数shortopts:短格式分析串。例如:"hp:i:",h后面没有冒号,表示后面不带参数;p和i后面带有冒号,表示后面带参数。 | |
169 | - 参数longopts:长格式分析串列表。例如:["help", "ip=", "port="],help后面没有等号,表示后面不带参数;ip和port后面带冒号,表示后面带参数。 | |
170 | - | |
171 | - 返回值options是以元组为元素的列表,每个元组的形式为:(选项串, 附加参数),如:('-i', '192.168.0.1') | |
172 | - 返回值args是个列表,其中的元素是那些不含'-'或'--'的参数。 | |
173 | - 若命令行-c或--testclass后面不加任何值的话或报错,若-和c之间有空格,或者-c后面有连续两个参数,会导致参数不匹配 | |
174 | - """ | |
175 | - opts, args = getopt.getopt(argv, "hf:c:t:e:p:m:", ["help", "filename=", "testclass=", "casename=","emailaddr=","priority=","mark="]) | |
46 | +def Send_email(filename): | |
47 | + '''判断邮件发送逻辑''' | |
48 | + l=eval(com.get_global_config("global_data", "email","list").lower()) | |
49 | + if type(l)!=type([]): | |
50 | + raise Exception("error,pls input list type send-email address") | |
51 | + elif len(l)==0: | |
52 | + print("\n list of send-email is null,won't send email!") | |
53 | + elif len(l)!=0: | |
54 | + for i in l: | |
55 | + print(" check send-email format : {}".format(i)) | |
56 | + if re.match(r'^[0-9a-zA-Z_]{1,19}@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}$',i): | |
57 | + pass | |
58 | + else: | |
59 | + raise Exception("error,pls check your send-email format") | |
60 | + #发送邮件 | |
61 | + em.send_email(filename, l) | |
62 | + else: | |
63 | + print("\n Haven't sent the email,pls check send-email address!") | |
176 | 64 | |
177 | - except getopt.GetoptError: | |
178 | - print('''Error: test_arg.py -f <filename> -c <testclass> -t <casename> -e <emailaddr> -p <priority> -m <mark>''') | |
179 | - print(''' or: test_arg.py --filename=<filename> --testclass=<testclass> --casename=<casename> | |
180 | - --emailaddr=<emailaddr> --priority=<priority> --mark=<mark>''') | |
181 | - sys.exit(2) | |
182 | - | |
183 | - # 处理 返回值options是以元组为元素的列表。 | |
184 | -# print "opts",opts | |
185 | -# print "args",args | |
186 | -# print "argv",argv | |
187 | - for opt, arg in opts: | |
188 | - print('opt',opt) | |
189 | - print('arg',arg) | |
190 | - if opt in ("-h", "--help"): | |
191 | - print('''test_arg.py -f <filename> -c <testclass> -t <casename> -e <emailaddr> -p <priority> -m <mark>''') | |
192 | - print('''or: test_arg.py --filename=<filename> --testclass=<testclass> --casename=<casename> | |
193 | - --emailaddr=<emailaddr> --priority=<priority> --mark=<mark>''') | |
194 | - sys.exit() | |
195 | - elif opt in ("-f", "--filename"): | |
196 | - _filename = arg | |
197 | - elif opt in ("-c", "--testclass"): | |
198 | - _testclass = arg | |
199 | - elif opt in ("-t", "--casename"): | |
200 | - _casename = arg | |
201 | - elif opt in ("-e", "--emailaddr"): | |
202 | - _emailaddr.append(arg) | |
203 | - elif opt in ("-p", "--priority"): | |
204 | - #arg传入的都是字符串,控制优先级时需要转换 | |
205 | - _priority.append(int(arg)) | |
206 | - elif opt in ("-m", "--mark"): | |
207 | - #arg传入的都是字符串,控制优先级时需要转换 | |
208 | - _casemark=arg | |
209 | - | |
210 | - print('filename=', _filename) | |
211 | - print('testclass=', _testclass) | |
212 | - print('casename=', _casename) | |
213 | - print('emailaddr=', _emailaddr) | |
214 | - print('priority=', _priority) | |
215 | - print('mark=', _casemark) | |
216 | - | |
217 | - print(len(sys.argv)) | |
218 | - | |
219 | - # 打印 返回值args列表,即其中的元素是那些不含'-'或'--'的参数。 | |
220 | - for i in range(0, len(sys.argv)): | |
221 | - print(u'输入参数 %s 为:%s' % (i + 1, sys.argv[i])) | |
222 | - | |
223 | - Create_Testcase_suite(filename=_filename,testclass=_testclass,casename=_casename,emailaddr=_emailaddr,priority=_priority,casemark=_casemark) | |
224 | - | |
65 | + | |
225 | 66 | |
226 | 67 | if __name__ == "__main__": |
227 | - # sys.argv[1:]为要处理的参数列表,里边的项为用户输入的参数,sys.argv[0]为脚本名,所以用sys.argv[1:]过滤掉脚本名。 | |
228 | - main_get(sys.argv[1:]) | |
229 | - | |
230 | - | |
68 | +# #创建测试套,运行测试用例,生成报告 | |
69 | + report=Run_Testcase(Create_Testcase_suite()) | |
70 | + #发送邮件 | |
71 | + Send_email(report) | ... | ... |
src/main_bu.py deleted
100644 → 0
1 | -#!/usr/bin/python | |
2 | -# -*- coding: UTF-8 -*- | |
3 | -import os,time | |
4 | -import sys | |
5 | -import unittest | |
6 | -import re | |
7 | -import HTMLTestRunner_cn | |
8 | -from discover import DiscoveringTestLoader | |
9 | -from commons import SendEmail as em | |
10 | -from commons import common as com | |
11 | - | |
12 | -def Create_Testcase_suite(): | |
13 | - '''创建测试套件''' | |
14 | - testunit=unittest.TestSuite() | |
15 | - test_Loader = DiscoveringTestLoader() | |
16 | - discover=test_Loader.discover("./testcase/",pattern='test_*.py',top_level_dir=None) | |
17 | - print(discover) | |
18 | - for test_suite in discover: | |
19 | - testunit.addTests(test_suite) | |
20 | -# for test_case in test_suite: | |
21 | -# testunit.addTests(test_case) | |
22 | -# print(testunit) | |
23 | - print(testunit) | |
24 | - return testunit | |
25 | - | |
26 | - | |
27 | -def Run_Testcase(testsuit): | |
28 | - '''运行测试用例并生成报告''' | |
29 | - now = time.strftime("%Y-%m-%d %H_%M_%S",time.localtime()) | |
30 | - path=os.path.dirname(os.path.abspath(sys.argv[0])) | |
31 | - report_file=path+"/report/"+now+"_result.html" | |
32 | - #创建报告文件 | |
33 | - fp=open(report_file,'wb') | |
34 | - | |
35 | - runner=HTMLTestRunner_cn.HTMLTestRunner( | |
36 | - stream=fp, | |
37 | - title=u'搜索功能测试报告', | |
38 | - description=u'用例执行情况:', | |
39 | - verbosity = 2) | |
40 | - #执行用例 | |
41 | - runner.run(testsuit) | |
42 | - #关闭文件 | |
43 | - fp.close() | |
44 | - return report_file | |
45 | - | |
46 | -def Send_email(filename): | |
47 | - '''判断邮件发送逻辑''' | |
48 | - l=eval(com.get_global_config("global_data", "email","list").lower()) | |
49 | - if type(l)!=type([]): | |
50 | - raise Exception("error,pls input list type send-email address") | |
51 | - elif len(l)==0: | |
52 | - print("\n list of send-email is null,won't send email!") | |
53 | - elif len(l)!=0: | |
54 | - for i in l: | |
55 | - print(" check send-email format : {}".format(i)) | |
56 | - if re.match(r'^[0-9a-zA-Z_]{1,19}@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}$',i): | |
57 | - pass | |
58 | - else: | |
59 | - raise Exception("error,pls check your send-email format") | |
60 | - #发送邮件 | |
61 | - em.send_email(filename, l) | |
62 | - else: | |
63 | - print("\n Haven't sent the email,pls check send-email address!") | |
64 | - | |
65 | - | |
66 | - | |
67 | -if __name__ == "__main__": | |
68 | -# #创建测试套,运行测试用例,生成报告 | |
69 | - report=Run_Testcase(Create_Testcase_suite()) | |
70 | - #发送邮件 | |
71 | - Send_email(report) |