Задать вопрос

Тел: +7 965 3737 888

513

Просмотров

0

Ответов

"Zoom in" on rendered HTML that the test client returns

<p>If you have this as your base class for all unit tests you can do the following:</p>
class TestViews(BaseTestCase):
    def test_generated_stats(self):
        "test that certain stuff in the response"

        ...create some content for testing or use fixtures...

        response = self.client.get('/some/page/')

        # At this point response.content is a huge string filled with HTML tags and
        # "junk" that you don't need for testing the content thus making it difficult
        # to debug the generated HTML because it so huge. 
        # So we can zoom in on the &lt;div id="stats&gt;...&lt;/div&gt; node
        html = self._zoom_html(response.content, '#stats')

        # the variable 'html' will now be something like this:
        """
        &lt;div id="stats"&gt;
            &lt;p&gt;
                &lt;strong&gt;2&lt;/strong&gt; students&lt;br/&gt;
                &lt;em&gt;1&lt;/em&gt; warning.
            &lt;/p&gt;
        &lt;/div&gt;
        """

        # This makes it easier to debug the response and easier to test
        # against but the HTML might still be in the way so this would fail:
        self.assertTrue('2 students' in html) # will fail

        # To strip away all html use _strip_html()
        content = self._strip_html(html)

        # Now this will work
        self.assertTrue('2 students' in content) # will work

<p>It works for me and I find this very useful so I thought I'd share it.           <br></p>

Вопрос полезен? Да0/Нет0
file_4679.py(939.0б)
None