[BangPypers] Compare two htmls for unittest

Arun Ravindran arunvr at gmail.com
Sun Mar 15 06:19:49 CET 2015


Hi Amit,

First, you need to consider why you need to compare the rendered HTML in
the first place. While writing unit tests we don't compare the rendered
output most of the time, rather we check the context variables. This is
because we don't need to test the render engine of the template language as
well in a *unit* test.

For example, in Django unit tests we almost always compare a value in the
context dictionary created by a view rather than the eventually rendered
template. You could also check if the correct template was invoked.

If you are certain that the exact HTML needs to be checked then you will
need to normalize the HTML so that the whitespace is ignored, ordering of
attributes is consistent, etc. You can check the assertHTMLEqual()
implementation in Django (docs
<https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.assertHTMLEqual>
and source
<https://github.com/django/django/blob/master/django/test/testcases.py#L633-L651>).
I think the implementation converts the HTML to Python objects which can be
easily compared.

Regards,
Arun

On Sat, Mar 14, 2015 at 5:40 PM, konark modi <modi.konark at gmail.com> wrote:

> Can you share few examples of valid and invalid cases. ?
>
> On Sat, Mar 14, 2015 at 1:01 PM, Amit Sethi <amit.pureenergy at gmail.com>
> wrote:
>
> > Hi All , I plan to port my html templates for mail to a new templating
> > engine. So i am setting up some unit tests for that. However I am not
> > sure how do i compare two html strings for equality. There might be
> > places where the strings generated may be different from my test
> > samples but they may render as similar. How should I tackle that. I
> > tried consuming both the strings as BeautifulSoup objects and
> > comparing the result. But still i get failure for htmls which I think
> > are same.
> >
> >
> > Thanks
> > Amit
> >
> > --
> > A-M-I-T S|S
> > _______________________________________________
> > BangPypers mailing list
> > BangPypers at python.org
> > https://mail.python.org/mailman/listinfo/bangpypers
> >
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list