【问题标题】:Function not returning what is expected, even when it appears to in debugger函数没有返回预期的结果,即使它出现在调试器中
【发布时间】:2012-09-22 03:44:41
【问题描述】:

只是一点背景:这是一个类似于 STRIPS 的程序。 这个测试一直返回假,即使它应该是真的。从我在调试器中看到的应该是真的,我看不出为什么它会返回假。

感谢您的帮助。 调试器图片:

世界:

def __init__(self):
    self.logic = {}
    self.actions = {}
    self.goals = set()
    self.curr_state = set()

测试用例:

def setUp(self):
    self.world = World()

def testWorldis_true(self):
     pred = "on"
     params = ("A", "B")
     self.assertFalse(self.world.is_true(pred, params))
     self.world.logic[pred] = params
     self.assertTrue(self.world.is_true(pred, params))

输出:

======================================================================
FAIL: testWorldis_true (objorstrippertests.TestWorldFunctions)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".stripperAssignment\src\objorstrippertests.py", line 54, in testWorldis_true
    self.assertTrue(self.world.is_true(pred, params))
AssertionError: False is not true

【问题讨论】:

  • 您将不得不为我们提供更多的合作机会。一些可执行的测试代码和一个测试用例怎么样?
  • @KeithRandall 补充了一点,够了吗?对不起,缺乏实质内容。

标签: python unit-testing debugging return


【解决方案1】:

替换:

 self.world.logic[pred] = params

与:

 self.world.logic[pred] = set([params])

甚至更好(保持封装!):

 self.world.set_true(pred, params)

logic 是从谓词到一组参数元组的映射,而不是单个参数元组。

【讨论】:

    【解决方案2】:

    测试实际上工作正常:

    >>> d = {}
    >>> d['on'] = ('A','B')
    >>> ('A','B') in d['on']
    False
    

    这是False,因为('A','B') 不在元组('A','B') 中。对('A','B') 的每次迭代都将返回AB

    >>> for x in d['on']:
    ...    print x
    ... 
    A
    B
    

    如果您将退货更改为==,那么它将正常工作:

    >>> d['on'] == ('A','B')
    True
    

    【讨论】:

    • 感谢您的解释。
    猜你喜欢
    • 1970-01-01
    • 2018-10-24
    • 2016-06-28
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    相关资源
    最近更新 更多