FavoriteLoading
0

Python + Excel + django 实现接口自动化测试并生成 HTML 报告

前段时间在testhome的QQ群中看到了 ‘BJ-行者’ 说的接口自动化测试,一时兴起,想着自己也写个,说干就干吧,具体思路如下:

  1. Excel中接口数据的整理,根据开发出具的Excel接口文档设计接口用例
  2. Python读取Excel中的接口数据,通过httplib发送接口协议
  3. 通过django的web框架将python中的参数传递到HTML报告中

接下来还是上代码说明测试思路:

一、Excel中用例的设计

二、从Excel读取接口参数


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
  <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">nrows</span><span class="p">):</span>
        <span class="n">n</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">address</span> <span class="o">=</span> <span class="n">sheet</span><span class="o">.</span><span class="n">cell</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">value</span>
        <span class="n">name</span> <span class="o">=</span> <span class="n">sheet</span><span class="o">.</span><span class="n">cell</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">value</span>
        <span class="c"># 获取第i+1行,第2列的数据</span>
        <span class="n">method</span> <span class="o">=</span><span class="n">sheet</span><span class="o">.</span><span class="n">cell</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">value</span>
        <span class="k">print</span> <span class="s">'address:'</span><span class="o">+</span><span class="n">address</span><span class="o">+</span><span class="s">' method:'</span><span class="o">+</span><span class="n">method</span>
        <span class="n">data</span> <span class="o">=</span><span class="n">sheet</span><span class="o">.</span><span class="n">cell</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">value</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">sub_nrows</span><span class="p">):</span>
            <span class="n">sub_testId</span> <span class="o">=</span> <span class="n">sub_sheet</span><span class="o">.</span><span class="n">cell</span><span class="p">(</span><span class="n">j</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">value</span>
            <span class="n">testId</span> <span class="o">=</span> <span class="s">'Sheet2'</span> <span class="o">+</span> <span class="s">':'</span> <span class="o">+</span> <span class="n">sub_testId</span>
            <span class="n">sub_ncol</span> <span class="o">=</span> <span class="n">sub_sheet</span><span class="o">.</span><span class="n">row_values</span><span class="p">(</span><span class="n">j</span><span class="p">)</span>
            <span class="c"># 获取第j+1行所有的数据</span>
            <span class="n">sub_ncols</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">sub_ncol</span><span class="p">)</span>
            <span class="c"># 计算当前行的列数</span>
            <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">sub_ncols</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">sub_ncol</span><span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="o">==</span> <span class="s">''</span><span class="p">:</span>
                    <span class="n">sub_ncols</span> <span class="o">=</span> <span class="n">sub_ncols</span> <span class="o">-</span><span class="mi">1</span>
                    <span class="c"># for循环,排除为空值的列</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">data</span> <span class="o">==</span> <span class="n">testId</span><span class="p">):</span>
                <span class="n">n</span> <span class="o">=</span> <span class="n">n</span><span class="o">+</span><span class="mi">1</span>
                <span class="n">m</span> <span class="o">=</span> <span class="n">j</span><span class="o">-</span><span class="n">n</span>
                <span class="c"># 为了保证参数名称每次都一样,所有当j值加1后,然后通过n传参再减1,这样就保证当前用例的m值始终一致</span>
                <span class="n">listA</span><span class="o">=</span><span class="p">[]</span>
                <span class="c"># 初始化一A数组</span>
                <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">sub_ncols</span><span class="p">):</span>
                    <span class="n">parameter1</span> <span class="o">=</span> <span class="n">sub_sheet</span><span class="o">.</span><span class="n">cell</span><span class="p">(</span><span class="n">j</span><span class="p">,</span><span class="n">k</span><span class="p">)</span><span class="o">.</span><span class="n">value</span>
                    <span class="c"># k为列值变量,从第4列开始计数,依次读取某一行的所有列的数据</span>
                    <span class="n">parameter2</span> <span class="o">=</span> <span class="n">sub_sheet</span><span class="o">.</span><span class="n">cell</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="n">k</span><span class="p">)</span><span class="o">.</span><span class="n">value</span>
                    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">parameter1</span><span class="p">)</span><span class="o">.</span><span class="n">__name__</span> <span class="o">==</span> <span class="s">"unicode"</span><span class="p">:</span>
                        <span class="n">Parameter1</span> <span class="o">=</span> <span class="n">parameter1</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">"utf-8"</span><span class="p">)</span>
                        <span class="c"># 字符类型转换,若当前为unicode类型,则转换为utf-8类型</span>
                    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">parameter1</span><span class="p">)</span><span class="o">.</span><span class="n">__name__</span><span class="o">==</span><span class="s">"float"</span><span class="p">:</span>
                        <span class="n">Parameter1</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">parameter1</span><span class="p">)</span>
                       <span class="c"># print Parameter1</span>
                    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">parameter2</span><span class="p">)</span><span class="o">.</span><span class="n">__name__</span> <span class="o">==</span> <span class="s">"unicode"</span><span class="p">:</span>
                        <span class="n">Parameter2</span> <span class="o">=</span> <span class="n">parameter2</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">"utf-8"</span><span class="p">)</span>
                      <span class="c">#  print Parameter2</span>
                    <span class="n">parameter</span> <span class="o">=</span> <span class="s">"'"</span> <span class="o">+</span> <span class="n">Parameter2</span> <span class="o">+</span> <span class="s">"'"</span> <span class="o">+</span> <span class="s">":"</span> <span class="o">+</span> <span class="s">"'"</span> <span class="o">+</span> <span class="n">Parameter1</span> <span class="o">+</span> <span class="s">"'"</span> <span class="o">+</span> <span class="s">","</span>
                    <span class="c">#print Parameter</span>
                    <span class="n">listA</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">parameter</span><span class="p">)</span>
                    <span class="c"># 将参数parameter添加到数组A中</span>
                <span class="n">listB</span> <span class="o">=</span> <span class="s">'{'</span> <span class="o">+</span> <span class="s">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">listA</span><span class="p">)</span> <span class="o">+</span> <span class="s">'}'</span>
                <span class="n">listC</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="n">listB</span><span class="p">)</span>
                <span class="c"># 将数组A中所有的数据进行拼接,然后通过eval转成unicode类型</span>

三、将获得的参数传递给服务器


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    <span class="n">params</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">Parameter</span><span class="p">)</span>
    <span class="n">headers</span> <span class="o">=</span> <span class="p">{</span><span class="s">"Content-type"</span><span class="p">:</span> <span class="s">"application/x-www-form-urlencoded"</span><span class="p">}</span>
    <span class="n">httpClient</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s">"test.qshealthcare.com"</span><span class="p">,</span> <span class="mi">80</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span>
    <span class="n">httpClient</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="n">Method</span><span class="p">,</span> <span class="n">Address</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span>
    <span class="n">resp</span> <span class="o">=</span> <span class="n">httpClient</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
    <span class="k">print</span> <span class="n">resp</span><span class="o">.</span><span class="n">status</span>
    <span class="k">print</span> <span class="n">resp</span><span class="o">.</span><span class="n">reason</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">resp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
    <span class="k">print</span> <span class="n">result</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">"utf-8"</span><span class="p">)</span>
    <span class="n">my_data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
    <span class="k">if</span><span class="p">(</span><span class="n">my_data</span><span class="p">[</span><span class="s">'message'</span><span class="p">]</span> <span class="o">==</span> <span class="s">u"success"</span><span class="p">):</span>
        <span class="c"># 用于判断my_data输出中message的值是否为success,以此判断用例是否执行成功</span>
        <span class="n">flag</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">flag</span> <span class="o">=</span> <span class="bp">False</span>
    <span class="n">Flag</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">flag</span><span class="p">)</span>

四:插播下django web 框架的配置

  1. 下载setup.py的django包,通过python setup.py install 进行安装,import Django然后Django.VERSION 查看是否安装成功
  2. 创建网站项目:python django-admin.py startproject mysite
  3. 启动http服务:python manage.py runserver

五:将返回结果通过django传递给HTML


1
2
3
4
    <span class="n">t</span> <span class="o">=</span> <span class="n">get_template</span><span class="p">(</span><span class="s">'report.html'</span><span class="p">)</span>  <span class="c">#在settings.py中需要设置模板的路径</span>
    <span class="n">excel_data</span><span class="p">(</span><span class="n">xlsfile1</span><span class="p">)</span>
    <span class="n">html</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">Context</span><span class="p">({</span><span class="s">'datas'</span><span class="p">:</span><span class="n">datas</span><span class="p">,</span><span class="s">'Case_time'</span><span class="p">:</span><span class="n">time</span><span class="p">,</span><span class="s">'Case_Tnum'</span><span class="p">:</span><span class="n">Tnum</span><span class="p">,</span><span class="s">'Case_Fnum'</span><span class="p">:</span><span class="n">Fnum</span><span class="p">,</span><span class="s">'Case_num'</span><span class="p">:</span><span class="n">num</span><span class="p">}))</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">html</span><span class="p">)</span>

六:展现一下HTML报告的样式

声明:本文转载自 TesterHome 移动测试社区,作者为 TesterHome 移动测试社区,原文网址:https://testerhome.com/topics/4857

最后编辑于:2016/11/16作者: 聚合

聚合类文章源自互联网, 感谢原作者的无私分享。

关注微信公众号 – 聚合软件测试类精华

关注微信公众号 – 聚合软件测试类精华