尝试在单元测试中将 Home 对象与 String 对象进行比较

Trying to compare Home Object to String Object in unit testing

我正在尝试在我的单元测试中测试一个方法是否有效。正如我在下面的错误代码中看到的那样,我试图将我的 Home 对象与我的 String 对象进行比较,当打印出它们时,它们肯定是相等的。我的问题是如何将我的主对象更改为字符串对象或将我的字符串对象更改为主对象以便测试通过。此外,如果它有帮助,这也是内存数据库测试。谢谢!

单元测试代码

from unittest import TestCase
from main import ContactApp
from database import CombinedDatabase, Home, Person, Location, Contact

     class TestContactApp(TestCase):
         def test_commit_data(self):
         url = CombinedDatabase.construct_in_memory_url()
         contact_database = CombinedDatabase(url)
         contact_database.ensure_tables_exist()
         session = contact_database.create_session()
         home_address = Home(address='3069 U St.')
         patient1 = Person(patient_name='Mike Piazza')
         ContactApp.commit_data(session, str(patient1), str(home_address), '456324A', 96.4, 'USA',  'Nebraska', 'Omaha', '54229')
         actual = session.query(Home).one()
         actual2 = session.query(Person).one()
         print(home_address, actual.address)
         self.assertEqual(home_address, actual.address)
         self.assertEqual(patient1, actual2.patient_name)

错误

<database.Home object at 0x7fe4ae20ce48> <database.Home object at 0x7fe4ae20ce48>
#printed out objects

<database.Home object at 0x7fe4ae20ce48> != <database.Home object at 0x7fe4ae20ce48>

<database.Home object at 0x7fe4ae20ce48>
<database.Home object at 0x7fe4ae20ce48>
<Click to see difference>

Traceback (most recent call last):
  File "/home/cse/Pycharmedu2019.1.1/pycharm-edu-2019.1.1/helpers/pycharm/teamcity/diff_tools.py", line 32, in _patched_equals
old(self, first, second, msg)
  File "/usr/lib64/python3.6/unittest/case.py", line 829, in assertEqual
assertion_func(first, second, msg=msg)
  File "/usr/lib64/python3.6/unittest/case.py", line 822, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: <database.Home object at 0x7fe4ae20ce48> != '<database.Home object at 0x7fe4ae20ce48>'

您可以通过定义 __str__ 方法将 Home 对象转换为字符串。每当您 运行 str(home):

时都会调用它
class Home:
    def __init__(self, address):
        self.address = address

    def __str__(self):
        return self.address


def main():
    address = '3069 U St.'
    my_home = Home(address=address)
    print(my_home)  # Prints '3069 U St.' because printing converts to a String
    print(my_home == address)  # False, because my_home is a Home, not a String
    print(str(my_home) == address)  # True, because we defined the __str__ method

如果您不想每次都将其转换为字符串,您还可以将该逻辑移至 __eq__ 方法中,每当您测试是否相等时都会调用该方法:

class Home:
    def __init__(self, address):
        self.address = address

    def __eq__(self, other):
        if isinstance(other, str):
            return other == self.address
        elif isinstance(other, Home):
            return other.address == self.address
        else:
            return False


def main():
    address = '3069 U St.'
    my_home = Home(address=address)
    print(my_home == address)  # True, because we redefined what equality means