How to send the test cases results in tabular format in html using Python
<p>I am sending daily result mail of passed and fail in Text format.</p>

<p>I want it to be send in Tabular format in HTML format using Python.</p>

<pre><code>============Text I am sending daily===========================

423 EIR DIAMETER IMEI Software Version handling                                 5         5         0         100.0     
424 EIR DIAMETER eirDualImsiUpdateTimestamp                                     2         2         0         100.0     
EIR-Provisioning                                                                47        41        6         87.23     
-------------------------------------------------------------------------------------------------------------------------------------------------
Total Summary                                                                   839       828       11        98.68     
---------------------------------------------------------------------------------------------------------------------------------------------
</code></pre>

<p>I'd recommend using <a href="http://jinja.pocoo.org/docs/dev/api/#basics" rel="nofollow">jinja2</a>. It's so simple i don't think i need to explain anything. But say you have a <code>tests</code>folder with an empty <code>__init__.py</code> file inside (your folder is now a python package), you'll need to create a folder called <code>templates</code> and place your html file inside.</p>

<p>Your html file (say <code>report.html</code>) content would be something like this:</p>

<pre><code>&lt;table&gt;
   &lt;tbody&gt;
     {% for test in test_results %}
     &lt;tr&gt;
       &lt;td&gt;{{test.name}}&lt;/td&gt;
       &lt;td&gt;{{test.version}}&lt;/td&gt;
     &lt;/tr&gt;
     {% endfor %}
   &lt;/tbody&gt;
&lt;/table&gt;
</code></pre>

<p>Now in your python code you'd do something like this when your tests are run:</p>

<pre><code>from jinja2 import Environment, PackageLoader


env = Environment(loader=PackageLoader('tests', 'templates'))
html_report = env.get_template('report.html')
send_by_email(html_template.render(tests_results=dictionary_of_results))
</code></pre>

<p>Of course the <code>send_by_email</code>function does not exist and is yours to write.
Also your html file is really HTML so you can do anything you'd usually do, inline style and all. <code>jinja2</code> just makes it possible to customize its content on the fly with the help of <code>template tags</code> and render an HTML string to you use as you wish.</p>

python python-2.7 python-3.x