5

easiest way to explain this one:

import unittest
from element import Element

class TestHTMLGen(unittest.TestCase):

    def test_Element(self):
        page = Element("html", el_id=False)
        self.assertEqual(page, Element("html", el_id=False))  # this is where I need help

I get the following error:

AssertionError: <element.Element object at 0x025C1B70> != <element.Element object at 0x025C1CB0>

I know the objects are not exactly the same but is there any way to check that they are equal? I would think that assertEqual would work.

edit: I am working with the addTypeEqualityFunc. However, I am still having trouble

def test_Element(self):
    page = Element("html", el_id=False)
    self.addTypeEqualityFunc(Element, self.are_elements_equal)
    self.assertEqual(page, Element("html", el_id=False))

def are_elements_equal(self, first_element, second_element, msg=None):
    print first_element.attribute == second_element.attribute
    return type(first_element) is type(second_element) and first_element.tag == second_element.tag and first_element.attribute == second_element.attribute

This is the output I get: False and it says the test passed. It should not pass because first_element.attribute is not equal to second_element.attribute. Furthermore, even if I just have return false for are_elements_equal, the test still passes.

Slaknation
  • 2,124
  • 3
  • 23
  • 42
  • can you post what the Element looks like? It is possible that object initialization creates different properties/attributes. You are right though, assertEqual essentially does a `==` check which should be sufficient to determining if objects are the same: [similar question](https://stackoverflow.com/questions/1227121/compare-object-instances-for-equality-by-their-attributes-in-python). You can also create your own _eq_() method to check for equality assuming you're using python 2.5 or greater – ajoseps Aug 31 '17 at 14:44
  • You can also create a [addTypeEqualityFunc](https://docs.python.org/2/library/unittest.html#unittest.TestCase.addTypeEqualityFunc) specifically for these objects – ajoseps Aug 31 '17 at 14:47
  • @ajoseps check the edit – Slaknation Aug 31 '17 at 15:53
  • 1
    Within the docs, it mentions you need to raise an exception in order for it to fail: `It must raise self.failureException(msg) when inequality between the first two parameters is detected – possibly providing useful information and explaining the inequalities in details in the error message.` – ajoseps Aug 31 '17 at 15:56

2 Answers2

3

Solution:

import unittest
from element import Element

class TestHTMLGen(unittest.TestCase):

    def test_Element(self):
        page = Element("html", el_id=False)
        self.addTypeEqualityFunc(Element, self.are_elements_equal)
        self.assertEqual(page, Element("html", el_id=False))  # this is where I need help

    def are_elements_equal(self, first_element, second_element, msg=None):
        self.assertEqual(type(first_element), type(second_element))
        self.assertEqual(first_element.tag, second_element.tag)
        self.assertEqual(first_element.attribute, second_element.attribute)

however, a lot of times self.assertEqual(vars(page), vars(Element("html", el_id=False))) will do the trick

edit: also, I should add. I made a cool little function that can check if objects are equal. Should work in most cases.

def are_elements_equal(self, first_element, second_element, msg=None):
    self.assertEqual(type(first_element), type(second_element))
    try:
        type(vars(first_element)) is dict
    except:
        self.assertEqual(first_element, second_element)
    else:
        for i in vars(first_element).keys():
            try:
                type(vars(vars(first_element)[i])) is dict
            except:
                if type(vars(first_element)[i]) is list:
                    for j in range(len(vars(first_element)[i])):
                        self.are_elements_equal(vars(first_element)[i][j], vars(second_element)[i][j])
                else:
                    self.assertEqual(vars(first_element)[i], vars(second_element)[i])
            else:
                self.are_elements_equal(vars(first_element)[i], vars(second_element)[i])
Slaknation
  • 2,124
  • 3
  • 23
  • 42
1

Use vars():

Return the dict attribute for a module, class, instance, or any other object with a dict attribute.

self.assertEqual(vars(page), vars(Element("html", el_id=False)))
neu242
  • 15,796
  • 20
  • 79
  • 114