common.py
7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import configparser
import unittest
import time
from datetime import timedelta,date
from discover import DiscoveringTestLoader
from commons.Logging import log
def get_global_config(file,section, key):
"object file is *src/config/global_data.conf"
current_path=os.path.dirname(__file__)
src_path=os.path.dirname(current_path)
# global_setting_path=src_path+'/config/global_data.conf'
global_setting_path=src_path+'/config/'+file+".conf"
#验证文件是否存在
file_path = os.path.exists(global_setting_path)
if file_path:
#获取文件的数据
if isinstance(key,int):
log.error("key of section cannot be int Type :<%r> "%str(key))
raise Exception("key of section cannot be int Type :<%r> "%str(key))
else:
config = configparser.ConfigParser()
config.read(global_setting_path,encoding="utf-8")
return config.get(section, key)
else:
log.error("File Not Exist :<%r> "%str(global_setting_path))
raise Exception("File Not Exist :<%r> "%str(global_setting_path))
def get_api_config(api,section,key):
"object file is *src/config/api/*.conf"
current_path=os.path.dirname(__file__)
src_path=os.path.dirname(current_path)
api_path=src_path+"/config/api/"
api_config_path=api_path+api+".conf"
#验证文件是否存在
file_path = os.path.exists(api_config_path)
if file_path:
#获取文件的数据
if isinstance(key,int):
log.error("key of section cannot be int Type :<%r> "%str(key))
raise Exception("key of section cannot be int Type :<%r> "%str(key))
else:
config = configparser.ConfigParser()
config.read(api_config_path,encoding="utf-8")
return config.get(section, key)
else:
log.error("File Not Exist :<%r> "%str(api_config_path))
raise Exception("File Not Exist :<%r> "%str(api_config_path))
def get_market_config(api,section,key):
"object file is *src/config/marketConfig/*.conf"
current_path=os.path.dirname(__file__)
src_path=os.path.dirname(current_path)
api_path=src_path+"/config/marketConfig/"
api_config_path=api_path+api+".conf"
#验证文件是否存在
file_path = os.path.exists(api_config_path)
if file_path:
#获取文件的数据
if isinstance(key,int):
log.error("key of section cannot be int Type :<%r> "%str(key))
raise Exception("key of section cannot be int Type :<%r> "%str(key))
else:
config = configparser.ConfigParser()
config.read(api_config_path,encoding="utf-8")
return config.get(section, key)
else:
log.error("File Not Exist :<%r> "%str(api_config_path))
raise Exception("File Not Exist :<%r> "%str(api_config_path))
def run_one(name):
test_suite = unittest.TestSuite()
#创建测试套
test_suite.addTest(name)
#显示运行用例
print("运行用例为{}".format(test_suite))
runner = unittest.TextTestRunner()
runner.run(test_suite)
def run_list(name):
test_suite = unittest.TestSuite()
test_suite.addTests(name)
#显示运行用例
print("运行用例为{}".format(test_suite))
# with open(file="E:\\PycharmWorkspace\\jmsf-re-local\\report\\test.log", mode="a", encoding="utf-8") as file:
# runner = unittest.TextTestRunner(stream=file,verbosity=2)
# runner.run(test_suite)
runner = unittest.TextTestRunner()
runner.run(test_suite)
def run_class(name):
test_cases = unittest.TestLoader().loadTestsFromTestCase(name)
#显示运行用例
print("运行用例为{}".format(test_cases))
runner = unittest.TextTestRunner()
runner.run(test_cases)
def run_Module(name):
test_cases = unittest.TestLoader().loadTestsFromModule(name)
#显示运行用例
print("运行用例为{}".format(test_cases))
runner = unittest.TextTestRunner()
runner.run(test_cases)
def run_Name(name):
test_cases = unittest.TestLoader().loadTestsFromName(name)
#显示运行用例
print("运行用例为{}".format(test_cases))
runner = unittest.TextTestRunner()
runner.run(test_cases)
def mylog(func):
def RebackTest(self):
log.info("{}".format(func.__name__))
return func(self)
return RebackTest
def date_time_chinese():
"returns the current time string,format for YYYY年mm月dd日HH时MM分SS秒"
return time.strftime("%Y年%m月%d日 %H时%M分%S秒".decode('utf-8').encode('gbk'),time.localtime())
def date_chinese():
"returns the current time string, format for YYYY年mm月dd日"
return time.strftime("%Y年%m月%d日".decode('utf-8').encode('gbk'),time.localtime())
def time_chinese():
"returns the current time string,format for HH时 MM分 SS秒"
return time.strftime("%H时%M分%S秒".decode('utf-8').encode('gbk'),time.localtime())
def date_time():
"returns the current time string, format for YYYY-mm-dd HH:MM:SS"
return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
def date_time_slash():
"returns the current time string,format for YYYY/mm/dd HH:MM:SS"
return time.strftime("%Y/%m/%d %H:%M:%S",time.localtime())
def dates():
"returns the current time string,format for YYYY-mm-dd"
return time.strftime("%Y-%m-%d",time.localtime())
def date_slash():
"returns the current time string,format for YYYY/mm/dd"
return time.strftime("%Y/%m/%d",time.localtime())
def times():
"returns the current time string, format for HH:MM:SS"
return time.strftime("%H:%M:%S",time.localtime())
def year():
"returns the current time string,format for year"
return time.strftime("%Y",time.localtime())
def month():
"returns the current time string,format for month"
return time.strftime("%m",time.localtime())
def day():
"returns the current time string,format for day"
return time.strftime("%d",time.localtime())
def hour():
"returns the current time string, format for Hour"
return time.strftime("%H",time.localtime())
def minute():
"returns the current time string,format for minute"
return time.strftime("%M",time.localtime())
def seconds():
"return the current time string,format for seconds"
return time.strftime("%S",time.localtime())
def str_to_tuple(stime):
"returns the string variable into time tuples"
return time.strptime(stime,"%Y-%m-%d %H:%M:%S")
def add_date(day_num):
today=date.today()
print("returns the current date-%s and a time interval-%s" %(today,day_num))
times=today+timedelta(days=day_num)
return times
def sub_date(day_num):
today=date.today()
print("returns the current date-%s minus one time interval-%s" %(today,day_num))
times=today-timedelta(days=day_num)
return times
def time_stamp():
#返回时间戳
t=time.time()
# print (t) #原始时间数据
# print (int(t)) #秒级时间戳
# print (int(round(t * 1000))) #毫秒级时间戳
# print (int(round(t * 1000000))) #微秒级时间戳
return int(round(t * 1000))