【问题标题】:self.assertTrue(False) what exactly does it testself.assertTrue(False) 它究竟测试了什么
【发布时间】:2015-09-23 22:53:55
【问题描述】:

我正在为我们的应用程序编写一些测试,但不确定我在这里测试的是正确的东西。这是我的测试。

def test_ReservationExtensionFalseWrongResource(self):
        'does not create a reservation that is an extension if different resource'
        try:
            reservation1 = Reservation.objects.create(user=self.regularUser1, resource=self.resource1, modality=self.modality, timeFrom=datetime(2015, 6, 11, 20, tzinfo=pytz.utc), timeTo=datetime(2015, 6, 11, 21, tzinfo=pytz.utc), count=1, notes=None, extendedReservation=None)
            reservation = create_reservation(self.regularUser2, self.regularUser2, None, self.resource2, self.modality, datetime(2015, 6, 11, 20, tzinfo=pytz.utc), datetime(2015, 6, 11, 21, tzinfo=pytz.utc), 1, reservation1.uuid)
            self.assertTrue(False, "Should not create reservation")
        except Exception, e:
            self.assertTrue(True, "Not authorized")

我想确定如果保留扩展是不同的资源,则无法创建它,因此该行应该在 try 块中失败:

 reservation = create_reservation(self.regularUser2, self.regularUser2, None, self.resource2, self.modality, datetime(2015, 6, 11, 20, tzinfo=pytz.utc), datetime(2015, 6, 11, 21, tzinfo=pytz.utc), 1, reservation1.uuid)

这样做:

self.assertTrue(False, "Should not create reservation")

断言预留创建导致 False 值?或者我是否错误地理解了断言匹配器。我已经尝试过查看文档,但我在 try catch 块中看不到任何类似的示例,这对我来说很明显。

感谢您的帮助。

【问题讨论】:

  • @Gocht 不行,assertTrue 不能那样工作
  • 哦,我在想 AsserEqual

标签: python django unit-testing django-testing


【解决方案1】:

python 文档中的函数签名

 assertTrue(expr, msg=None)

检查给定的expr 是否为True。如果exprFalse,它将使用提供的消息(msg)引发错误

self.assertTrue(True, "Not authorized") # Is always True 

self.assertTrue(False, "Should not create reservation")  # Is always False and it will throw an error saying "Should not create reservation"

所以在你的 try 块中,如果没有引发任何异常,self.assertTrue(False, "Should not create reservation") 将抛出错误,因为 False 不是 True

如果引发异常,则您的代码将输入Except blockself.assertTrue(True, "Not authorized") 不会引发任何错误,因为 True 是 True。

所以,如果你想抛出异常,如果发生任何异常,请将self.assertTrue(False, "Should not create reservation")移动到Except块

except Exception, e:
    self.assertTrue(False, "Should not create reservation")

【讨论】:

    【解决方案2】:

    虽然不是直接回答标题,但这应该回答您的实际问题。

    1. 你永远不应该抓住Exception(除非你之后重新加注)。

      except Exception, e:
      

      处理异常时,必须指定要捕获的确切错误。例如,调用create_reservation 的代码可能会捕获您定义的ReservationError 异常。

      如果您希望此代码正常工作,则必须进行测试并确保 create_reservation 引发 正确 错误。所以即使在测试中,你仍然必须使用正确的错误。

    2. 如果您需要测试某些代码引发异常,请使用适当的断言,即assertRaises。它可以以两种方式使用。要么作为电话:

      self.assertRaises(ReservationError, create_reservation, self.regularUser2, .........)
      

      或者您可以将其用作上下文管理器,使用 with 关键字(需要 python 2.7 或 python 3.1 或更高版本):

      with self.assertRaises(ReservationError):
          create_reservation(self.regularUser2, ...........)
      

      两个示例都将运行create_reservation,检查它是否引发了异常,如果它引发了异常,则通过并且它是正确的类型,否则失败。

    【讨论】:

      【解决方案3】:

      我可能不会那样设置我的测试。您要做的是确定您的方法的行为究竟是什么,并进行相应的测试。

      因此,如果您正在测试失败案例,例如,如果您的方法失败,则返回类似“保留失败”的内容,那么您应该执行以下操作:

      # where msg is the response of your call
      self.assertEqual(msg, "reservation failed", "assertion failed")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-25
        • 2012-07-23
        • 2016-09-10
        • 2023-03-15
        • 2012-10-17
        • 2021-06-04
        • 1970-01-01
        • 2018-07-30
        相关资源
        最近更新 更多