python - 对 python 类属性进行单元测试

标签 python unit-testing python-unittest

假设我有一个类:

class Player(object):
    def __init__(self):
        self.goal = ''

    @property
    def goal(self):
        return self.goal

考虑到属性goal不可调用,是否可以对其编写单元测试?

最佳答案

怎么样:

def test_goal_property(self):
    dummy_player = Player()

    self.assertEqual(dummy_player.goal, '')     

def test_goal_property(self):
    dummy_player = Player()
    dummy_player.goal = 'something'

    self.assertEqual(dummy_player.goal, 'something')

关于python - 对 python 类属性进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59732625/

相关文章:

python - OpenPyXL - 页眉/页脚 - 无法将页眉写入工作表

python - 将 Pandas 列转换为日期时间

unit-testing - 从批处理脚本运行 MsTest.exe

java - 如何模拟 HTTP 响应

python - 导入错误: No module named zlib; tox

python - unittest.TestCase setUpClass 覆盖和继承

python - 对 PyQt 应用程序进行单元测试

python - 使用新数据库启动 django unittest

javascript - Sequelize 测试 - 有时验证错误

python - Django模板中字典的循环问题