坚持使用Python进行测试驱动开发(第6章)

时间:2016-04-03 13:00:42

标签: django tdd

我通过Harry Percival学习Python的测试驱动开发。

我在第6章的开头大约在这里:

http://chimera.labs.oreilly.com/books/1234000000754/ch06.html#_running_just_the_unit_tests

更准确地说是“python3 manage.py测试列表”。

让我们引用有问题的代码:

lists/tests.py
class HomePageTest(TestCase):

    def test_home_page_returns_correct_html(self):
        request = HttpRequest()
        response = home_page(request)
        expected_html = render_to_string('home.html')
        self.assertEqual(response.content.decode(), expected_html)

lists/views.py
def home_page(request):
    if request.method == 'POST':
        Item.objects.create(text=request.POST['item_text'])
        return redirect('/')

    items = Item.objects.all()
    return render(request, 'home.html', {'items': items})

lists/templates/home.html
<html>
    <head>
        <title>To-Do lists</title>
    </head>
    <body>
        <h1>Your To-Do list</h1>
        <form method="POST">
            <input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
            {% csrf_token %}
        </form>

        <table id="id_list_table">
            {% for item in items %}
                <tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
            {% endfor %}
        </table>
    </body>
</html>

上一章(第5号)的全部代码在这里:https://github.com/hjwp/book-example/tree/chapter_05

自第5章以来,列表应用程序中的测试没有改变。 在我坚持的时候,来自列表应用程序的所有测试都通过了(根据这本书)。

但我明白了:

======================================================================
FAIL: test_home_page_returns_correct_html (lists.tests.HomePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/michael/workspace/superlists/lists/tests.py", line 20, in test_home_page_returns_correct_html
    self.assertEqual(response.content.decode(), expected_html)
AssertionError: '<htm[238 chars]     <input type=\'hidden\' name=\'csrfmiddlew[163 chars]l>\n' != '<htm[238 char
s]     \n        </form>\n\n        <table id="i[66 chars]l>\n'

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

我尝试在调试器中评估表达式并得到它:

"expected_html" SafeText:        <html>\n    <head>\n        <title>To-Do lists</title>\n    </head>\n    <body>\n        <h1>Your To-Do list</h1>\n        <form method="POST">\n            <input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />\n            \n        </form>\n\n        <table id="id_list_table">\n            \n        </table>\n    </body>\n</html>\n 

"response.content.decode()" str: <html>\n    <head>\n        <title>To-Do lists</title>\n    </head>\n    <body>\n        <h1>Your To-Do list</h1>\n        <form method="POST">\n            <input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />\n            \n        </form>\n\n        <table id="id_list_table">\n            \n        </table>\n    </body>\n</html>\n

好吧,第一个是SafeText,第二个是str。

如果我将expected_html转换为字符串(expected_html = str(render_to_string('home.html')),则测试仍然失败:

======================================================================
FAIL: test_home_page_returns_correct_html (lists.tests.HomePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/michael/workspace/superlists/lists/tests.py", line 20, in test_home_page_returns_correct_html
    self.assertEqual(response.content.decode(), expected_html)
AssertionError: '<htm[238 chars]     <input type=\'hidden\' name=\'csrfmiddlew[163 chars]l>\n' != '<htm[238 char
s]     \n        </form>\n\n        <table id="i[66 chars]l>\n'
  <html>
      <head>
          <title>To-Do lists</title>
      </head>
      <body>
          <h1>Your To-Do list</h1>
          <form method="POST">
              <input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
-             <input type='hidden' name='csrfmiddlewaretoken' value='08xKa5oXRi6EEsEBmAqONg8ATnKeqXOE' />
+             
          </form>

          <table id="id_list_table">

          </table>
      </body>
  </html>

你能帮我试试通过考试吗?

0 个答案:

没有答案