【问题标题】:Python: assertRaises( ) not catching ldap.SERVER_DOWN error when raisedPython:assertRaises() 引发时未捕获 ldap.SERVER_DOWN 错误
【发布时间】:2013-08-23 23:43:38
【问题描述】:

提前感谢您的帮助。

我正在尝试测试以下类方法:

def _get_ldap_connection(self):
    """
    Instantiate and return simpleldap.Connection object.

    Raises:
        ldap.SERVER_DOWN: When ldap_url is invalid or server is
        not reachable.

    """
    try:
        ldap_connection = simpleldap.Connection(
            self.ldap_url, encryption='ssl', require_cert=False,
            debug=False, dn=self.ldap_login_dn,
            password=self.ldap_login_password)

    except ldap.SERVER_DOWN:
        raise ldap.SERVER_DOWN(
            "The LDAP server specified, {}, did not respond to the "
            "connection attempt.".format(self.ldap_url))

这是单元测试:

def test__get_ldap_connection(self):
    """
    VERY IMPORTANT: This test refers to your actual config.json file.
    If it is correctly populated, you can expect this test to fail.

    """

    # Instantiate Class
    test_extractor = SakaiLdapExtractor('config_files/config.json')

    # Monkey with ldap server url to ensure error.
    test_extractor.ldap_url = "invalid_ldap_url"

    self.assertRaises(
        ldap.SERVER_DOWN, test_extractor._get_ldap_connection())

到目前为止,一切都很好。但是当我执行单元测试时(通过nose)test_extractor._get_ldap_connection() 是从assertRaises 语句中调用的,但是没有捕获到异常并且测试失败了。

这是输出:

vagrant@precise64:/vagrant/sakai-directory-integration$ nosetests
...E..
======================================================================
ERROR: VERY IMPORTANT: This test refers to your actual config.json file.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/vagrant/sakai-directory-integration/test_sakaiLdapExtractor.py", line 77, in test__get_ldap_connection
    ldap.SERVER_DOWN, test_extractor._get_ldap_connection())
  File "/vagrant/sakai-directory-integration/sakai_ldap_integration.py", line 197, in _get_ldap_connection
    "connection attempt.".format(self.ldap_url))
SERVER_DOWN: The LDAP server specified, invalid_ldap_url, did not respond to the connection attempt.

----------------------------------------------------------------------
Ran 6 tests in 0.159s

帮帮我!

【问题讨论】:

    标签: python python-unittest python-ldap


    【解决方案1】:

    不调用,只传递函数(方法)本身;放弃():

    self.assertRaises(
        ldap.SERVER_DOWN, test_extractor._get_ldap_connection)
    

    或者,如果您使用的是最新版本的python(Python 2.7+ / Python 3.1+),您可以使用with self.assertRaises(..) 表单:

    with self.assertRaises(ldap.SERVER_DOWN):
        test_extractor._get_ldap_connection()
    

    【讨论】:

    • 是的,当然。我多么愚蠢。谢谢@falsetru。我会将您的答案标记为正确。
    • @eikonomega,不客气。这是我最喜欢的(?)错误之一。 ;)
    【解决方案2】:

    你没有正确使用assertRaises

    您可以将其用作上下文管理器:

    with self.assertRaises(ldap.SERVER_DOWN):
        test_extractor._get_ldap_connection()
    

    或通常的方式(self.assertRaises(exception, function, args):

    self.assertRaises(ldap.SERVER_DOWN, test_extractor._get_ldap_connection)
    

    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      • 2014-10-11
      相关资源
      最近更新 更多