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

问题描述 投票:0回答:1

我正在尝试测试一种方法在我的单元测试中是否有效。从下面的错误代码中可以看出,我正在尝试将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>'
python unit-testing in-memory-database
1个回答
0
投票

您可以通过定义__str__方法将Home对象转换为String。只要您运行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
© www.soinside.com 2019 - 2024. All rights reserved.