Search code examples
pytest-html

how to customize details with assert error like adding html table in details using pytest-html?


I try this code:

assert 1=2, "<table>....</table>"

table not showing in details, but strings in the details how to show html tag with assert error message


Solution

  • i assert two file with difflib and show the message with htmlcontent

    self = <test_assert.TestAssert object at 0x00000178F8534748>
    
        def test_assert_match(self):
    
    
            # with open('c1.json', 'w') as f1:
            #     json.dump(content1,f1, indent =4, sort_keys = True, separators = (',',':'))
    
            content1 = self.readfile(os.path.join(curr,'1.json'))
            content2 = self.readfile(os.path.join(curr,'2.json'))
    
    >       MyAssert.assertMatch(content1, content2, True)
    
    Testcases\test_assert.py:34: 
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
    
    expect = ['{', '    "a": "123"', '}'], actual = ['{', '    "a": "123",', '    "b": "22"', '}'], isHtml = True
    
        @staticmethod
        def assertMatch(expect, actual, isHtml = False):
            msg = ''
    
            if isHtml:
                d = difflib.HtmlDiff()
                msg = d.make_file(expect,actual)
                with open('diff.html','w') as f:
                    f.write(msg)
    
            else:
                d = difflib.Differ()
                diff = d.compare(expect,actual)
                msg = "\n".join(list(diff))
    
    >       assert expect == actual, msg
    E       AssertionError: 
    E       <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    E                 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    E       
    E       <html>
    E       
    E       <head>
    E           <meta http-equiv="Content-Type"
    E                 content="text/html; charset=utf-8" />
    E           <title></title>
    E           <style type="text/css">
    E               table.diff {font-family:Courier; border:medium;}
    E               .diff_header {background-color:#e0e0e0}
    E               td.diff_header {text-align:right}
    E               .diff_next {background-color:#c0c0c0}
    E               .diff_add {background-color:#aaffaa}
    E               .diff_chg {background-color:#ffff77}
    E               .diff_sub {background-color:#ffaaaa}
    E           </style>
    E       </head>
    E       
    E       <body>
    E           
    E           <table class="diff" id="difflib_chg_to0__top"
    E                  cellspacing="0" cellpadding="0" rules="groups" >
    E               <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
    E               <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
    E               
    E               <tbody>
    E                   <tr><td class="diff_next" id="difflib_chg_to0__0"><a href="#difflib_chg_to0__0">f</a></td><td class="diff_header" id="from0_1">1</td><td nowrap="nowrap">{</td><td class="diff_next"><a href="#difflib_chg_to0__0">f</a></td><td class="diff_header" id="to0_1">1</td><td nowrap="nowrap">{</td></tr>
    E                   <tr><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="from0_2">2</td><td nowrap="nowrap">&nbsp;&nbsp;&nbsp;&nbsp;"a":&nbsp;"123"</td><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="to0_2">2</td><td nowrap="nowrap">&nbsp;&nbsp;&nbsp;&nbsp;"a":&nbsp;"123"<span class="diff_add">,</span></td></tr>
    E                   <tr><td class="diff_next"></td><td class="diff_header"></td><td nowrap="nowrap"></td><td class="diff_next"></td><td class="diff_header" id="to0_3">3</td><td nowrap="nowrap"><span class="diff_add">&nbsp;&nbsp;&nbsp;&nbsp;"b":&nbsp;"22"</span></td></tr>
    E                   <tr><td class="diff_next"></td><td class="diff_header" id="from0_3">3</td><td nowrap="nowrap">}</td><td class="diff_next"></td><td class="diff_header" id="to0_4">4</td><td nowrap="nowrap">}</td></tr>
    E               </tbody>
    E           </table>
    E           <table class="diff" summary="Legends">
    E               <tr> <th colspan="2"> Legends </th> </tr>
    E               <tr> <td> <table border="" summary="Colors">
    E                             <tr><th> Colors </th> </tr>
    E                             <tr><td class="diff_add">&nbsp;Added&nbsp;</td></tr>
    E                             <tr><td class="diff_chg">Changed</td> </tr>
    E                             <tr><td class="diff_sub">Deleted</td> </tr>
    E                         </table></td>
    E                    <td> <table border="" summary="Links">
    E                             <tr><th colspan="2"> Links </th> </tr>
    E                             <tr><td>(f)irst change</td> </tr>
    E                             <tr><td>(n)ext change</td> </tr>
    E                             <tr><td>(t)op</td> </tr>
    E                         </table></td> </tr>
    E           </table>
    E       </body>
    E       
    E       </html>
    
    Testcases\myAssert.py:50: AssertionError
    

    i want to show the html in the details not strings.. enter image description here