有什么区别?(Python unittest失败(?)由于多个字符串中的前导空格)

2024-09-27 23:16:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试构建一个月的HTML标题行。我有一个测试用例,当我直观地检查比较或代码生成的部分时,我得到一个断言错误(例如,我的案例没有通过)。奇怪的是,当我目测时,输出看起来是一样的。在

我做了些手脚,缩小了问题的范围。请参阅下面的代码。在

这是我的测试用例:

class xyz(unittest.TestCase):

    def test__render_table_header(self):
        self.maxDiff = None
        testy = self.testcal1
        htmltest = testy._render_table_header(date(2014, 8, 1))
        htmlcase = """<table>
            <th colspan='7'>
                <div class="headercontainer">
                    <div class="montheader">{}</div>
                    <div class="yearheader">{}</div>
                </div>
            </th>
            <tr>
                <td class='dayheader'>Sun</td>
                <td class='dayheader'>Mon</td>
                <td class='dayheader'>Tues</td>
                <td class='dayheader'>Wed</td>
                <td class='dayheader'>Thurs</td>
                <td class='dayheader'>Fri</td>
                <td class='dayheader'>Sat</td>
            </tr>
            <tr>""".format('August', '2014')
        self.assertEqual(htmlcase, htmltest)

我的职能是:

^{pr2}$

以下是我得到的错误和差异:

FAIL: test__render_table_header 

(__main__.test_enhanced_cal_helper_functions)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "court_app_timeline.py", line 430, in test__render_table_header
    self.assertEqual(htmlcase, htmltest)
AssertionError: '<table>\n            <th colspan=\'7\'>\n                <div [585 chars]<tr>' != '<table>\n                <th colspan=\'7\'>\n                 [649 chars]<tr>'
  <table>
-             <th colspan='7'>
+                 <th colspan='7'>
? ++++
-                 <div class="headercontainer">
+                     <div class="headercontainer">
? ++++
-                     <div class="montheader">August</div>
+                         <div class="montheader">August</div>
? ++++
-                     <div class="yearheader">2014</div>
+                         <div class="yearheader">2014</div>
? ++++
-                 </div>
+                     </div>
? ++++
-             </th>
+                 </th>
? ++++
-             <tr>
+                 <tr>
? ++++
-                 <td class='dayheader'>Sun</td>
+                     <td class='dayheader'>Sun</td>
? ++++
-                 <td class='dayheader'>Mon</td>
+                     <td class='dayheader'>Mon</td>
? ++++
-                 <td class='dayheader'>Tues</td>
+                     <td class='dayheader'>Tues</td>
? ++++
-                 <td class='dayheader'>Wed</td>
+                     <td class='dayheader'>Wed</td>
? ++++
-                 <td class='dayheader'>Thurs</td>
+                     <td class='dayheader'>Thurs</td>
? ++++
-                 <td class='dayheader'>Fri</td>
+                     <td class='dayheader'>Fri</td>
? ++++
-                 <td class='dayheader'>Sat</td>
+                     <td class='dayheader'>Sat</td>
? ++++
-             </tr>
+                 </tr>
? ++++
-             <tr>+                 <tr>? ++++


----------------------------------------------------------------------

我要向你们承认,我不是最有才华的程序员。事实上,我的整个编程生涯都只能追溯到5个月前。随之而来的是在阅读差异方面的某种无能。在我看来,输出之间的主要差异与前导空格有关。我该怎么解决这个问题?在

PS-任何提示,评论,指点等都非常感谢。在


Tags: testselfdivtablerendertrclasstd
3条回答

一种可能的解决方案是简单地匹配构成testcase的字符串和函数中使用的字符串上的缩进。这有助于测试通过。在

然而,它似乎有点愚蠢(不可伸缩,而且真正没有触及函数的核心)。直接忽略前导空格会更好。也许另一个评论者可以协助。当我们在做的时候,我很想听听人们的评论:用python生成HTML,以及确保在输出上保持正确的标签嵌套(为了视觉吸引力,如果没有其他的话)的最佳方法是什么。在

首先:多行字符串不考虑缩进。这意味着,如果你的测试用例在一个类中,而你的生成器在一个函数中,那将是一个问题。在

更有用的是,空白在HTML中基本上是可忽略的。。。我建议至少做些类似的事情:

def strip_white_space(str):
   return str.replace(" ", "").replace("\t", "").replace("\n", "")

self.assertEqual(strip_white_space(htmlcase), strip_white_space(htmltest))

一个更好的方法,我不知道怎么做在我的头顶上,将这两个弦组合起来。Clean Up HTML in Python有一些建议,例如:

^{pr2}$

(尽管我不确定是否会一直删除空白)

不要在两个不同的地方定义HTML代码。在一个地方定义它,并在需要的地方使用它。在

一种常见的方法是将它定义为模块中的全局变量,然后从该模块导入它。例如,您可以在一个名为html_examples.py的模块中使用它:

TABLE_CODE = """
    <table>
        <th colspan='7'>
         blah blah...
"""

当您需要访问另一个模块中的文本时,您可以这样说:

^{pr2}$

相关问题 更多 >

    热门问题