gpt4 book ai didi

python - 有什么区别? (由于多字符串中的前导空格,Python 单元测试失败(?))

转载 作者:行者123 更新时间:2023-11-28 22:40:26 26 4
gpt4 key购买 nike

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

我做了一些摆弄,缩小了问题的范围。请看下面的代码。

这是我的测试用例:

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)

这是我的功能:

def _render_table_header(self, dateobj):

TOP_OF_TABLE = """<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>"""
month = dateobj.strftime('%B')
year = dateobj.strftime('%Y')
return TOP_OF_TABLE.format(month, year)

这是我得到的错误和差异:

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 - 非常感谢任何提示、评论、指示等。

最佳答案

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

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

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

然后当你需要在另一个模块中访问该文本时,你可以这样说:

from html_examples import TABLE_CODE

关于python - 有什么区别? (由于多字符串中的前导空格,Python 单元测试失败(?)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33659074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com