【问题标题】:Trying to compare Home Object to String Object in unit testing尝试在单元测试中将 Home 对象与字符串对象进行比较
【发布时间】:2020-08-10 13:14:43
【问题描述】:

我正在尝试在我的单元测试中测试一个方法是否有效。正如我的错误中的以下代码所示,我正在尝试将我的 Home 对象与我的 String 对象进行比较,并且在打印出它们时,它们肯定是彼此相等的。我的问题是如何将我的 Home 对象更改为 String 对象或将我的 String 对象更改为 Home 对象,以便测试通过。此外,如果它有助于这也是内存数据库测试。谢谢!

单元测试代码

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】:

    您可以通过定义 __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
    

    【讨论】:

    • 我会将两者之一直接放在我的单元测试文件中吗?还是我的 main.py 文件?
    • 您将修改 Home 类的定义。根据 import 语句,这可能在 Database.py 中
    猜你喜欢
    • 2011-01-13
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2016-06-10
    • 2017-06-23
    • 1970-01-01
    相关资源
    最近更新 更多