您的位置:首页 > 其它

生成jmeter可用文件和必要参数化的csv

2016-04-20 14:54 344 查看
领导说让用jmeter这个工具参数化测试soap接口,做完参数化后保存的文件是jmx格式,经过同事启发,打开一看这个不是xml格式文件么,应该可以利用python写一个xml格式然后保存成jmx格式的,这样只要维护excel中的测试用例就行了,真正的实现数据驱动参数化。不过在过程中也遇到过编码问题,实在头疼,所以我建议大家还是用英文在线程组或者循环中定义名称吧。在excel中写好接口测试用例,然后运行此文件后生成jmeter可用的jmx和csv文件

#-*-coding:utf8-*-
#__author__=xu.duan
import xlrd
import sys
import os
import os.path
import getopt
import csv
reload(sys)
sys.setdefaultencoding('gb18030')

def getPathFileName(path): #从path中获取线程组名称
tmp = path.split('/');
return tmp[len(tmp) -1 ].split('.')[0];

def xlsToxmlPath(path):#把excel表名称直接引用成xml的名称
return path.split('.')[0] + '.jmx';

def CSVToxmlPath(path):#把excel表名称直接引用成csv的名称
return path.split('.')[0] + '.csv';

def exportjmx(path):#生成jmx文件过程
data = xlrd.open_workbook(path,
formatting_info = True, encoding_override="utf-8")
table = data.sheets()[0]

f = open(xlsToxmlPath(path), 'wb')
f.write(u'<?xml version="1.0" encoding="UTF-8"?>\n')
f.write(u'<jmeterTestPlan version="1.2" properties="2.6" jmeter="2.11 r1554548">\n')

f.write(u'''<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="info3" enabled="true">
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<intProp name="LoopController.loops">-1</intProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">1</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<longProp name="ThreadGroup.start_time">1281132211000</longProp>
<longProp name="ThreadGroup.end_time">1281132211000</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
</ThreadGroup>
<hashTree>
<LoopController guiclass="LoopControlPanel" testclass="LoopController" testname="Step 1" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">2</stringProp>
</LoopController>
<hashTree>
<CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="CSV Data Set Config" enabled="true">
<stringProp name="delimiter">,</stringProp>
<stringProp name="fileEncoding">UTF-8</stringProp>
<stringProp name="filename">E:/T/info3.0/text doc/info3.csv</stringProp>
<boolProp name="quotedData">true</boolProp>
<boolProp name="recycle">true</boolProp>
<stringProp name="shareMode">shareMode.all</stringProp>
<boolProp name="stopThread">false</boolProp>
<stringProp name="variableNames">IP,uri,words,language,channel,output,sign</stringProp>
</CSVDataSet>
<hashTree/>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="testcase1" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">''')

f.write(u'<collectionProp name="Arguments.arguments">\n')

for i in range(2, table.ncols):
#s = u'<collectionProp name="Arguments.arguments">\n';
#tmp = [u'%s = "%s"' % (str(table.cell_value(0,j)), str(table.cell_value(i,j))) for j in range(table.ncols)];
#s +=(u'elementProp name="{{{0}}}" elementType="HTTPArgument"'.format(table.cell_value(0,i)));
s =(u'''<elementProp name="{0}" elementType="HTTPArgument">
<boolProp name="HTTPArgument.always_encode">true</boolProp>
<stringProp name="Argument.value">${{{0}}}</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
<boolProp name="HTTPArgument.use_equals">true</boolProp>
<stringProp name="Argument.name">{0}</stringProp>
</elementProp> \n'''.format(table.cell_value(0,i)));
#s += ' '.join(map(str,tmp));

f.write(s);
f.write(u'</collectionProp>\n');
f.write(u'''</elementProp>
<stringProp name="HTTPSampler.domain">${{{0}}}</stringProp>
<stringProp name="HTTPSampler.port"></stringProp>
<stringProp name="HTTPSampler.connect_timeout"></stringProp>
<stringProp name="HTTPSampler.response_timeout"></stringProp>
<stringProp name="HTTPSampler.protocol">http</stringProp>
<stringProp name="HTTPSampler.contentEncoding"></stringProp>
<stringProp name="HTTPSampler.path">${{{1}}}</stringProp>
<stringProp name="HTTPSampler.method">GET</stringProp>
<boolProp name="HTTPSampler.follow_redirects">false</boolProp>
<boolProp name="HTTPSampler.auto_redirects">true</boolProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
<stringProp name="HTTPSampler.implementation">Java</stringProp>
<boolProp name="HTTPSampler.monitor">false</boolProp>
<stringProp name="HTTPSampler.embedded_url_re"></stringProp>
</HTTPSamplerProxy>
<hashTree/>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="response to assert" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="1259833018">Successful</stringProp>
</collectionProp>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
<intProp name="Assertion.test_type">16</intProp>
</ResponseAssertion>
<hashTree/>
</hashTree>\n'''.format(table.cell_value(0,0),table.cell_value(0,1)));
f.write(u'''<ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="Check the result tree" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>true</xml>
<fieldNames>false</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
</value>
</objProp>
<objProp>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>false</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<threadCounts>true</threadCounts>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
<ResultCollector guiclass="StatVisualizer" testclass="ResultCollector" testname="Aggregate Report" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>true</xml>
<fieldNames>false</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
</value>
</objProp>
<objProp>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>false</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<threadCounts>true</threadCounts>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>\n''')
f.write(u'</jmeterTestPlan>');
f.close

def exportcsv(path):#转换csv过程
data = xlrd.open_workbook(path,
formatting_info = True, encoding_override="utf-8")
table = data.sheet_by_name('test1')
csv_file = open(CSVToxmlPath(path), 'wb')
wr = csv.writer(csv_file, quoting=csv.QUOTE_ALL)

for rownum in xrange(table.nrows):
wr.writerow(table.row_values(rownum))
csv_file.close()

def usage():#定义argv
print '1.generate a CSV and jmx  :'
print sys.argv[0], '--mode genCSVJMX --input_file <file_path> --output_file <output_excel>'

if __name__ == '__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], 'm:i:h',
['mode=','input_file=',
'help'])
except getopt.GetoptError, e:
print str(e)
sys.exit(2)
cur_mode = 'genCSVJMX'
input_file = None

for k, v in opts:
if k in ('-h', '--help'):
usage()
sys.exit(1)
elif k in ('-m', '--mode'):
cur_mode = v
elif k in ('--input_file', '-i'):
input_file = v
else:
print 'None option'

if cur_mode == 'genCSVJMX':
if os.path.exists(input_file):
exportjmx('{0}'.format(input_file))
exportcsv('{0}'.format(input_file))
else:
if len(input_file) == 0:
print 'Error: lack input para --input_file'
usage()
sys.exit(1)

if not os.path.exists(input_file):
print 'Error: log path does not exist!'
sys.exit(1)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: