Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:assertRaises()在引发时未捕获ldap.SERVER\u DOWN错误_Python_Python Unittest_Python Ldap - Fatal编程技术网

Python:assertRaises()在引发时未捕获ldap.SERVER\u DOWN错误

Python:assertRaises()在引发时未捕获ldap.SERVER\u DOWN错误,python,python-unittest,python-ldap,Python,Python Unittest,Python Ldap,提前谢谢你的帮助 我尝试测试以下类方法: 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_c

提前谢谢你的帮助

我尝试测试以下类方法:

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())
下面是单元测试:

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\u提取器时,从assertRaises语句调用了\u get\u ldap\u connection()
,但是没有捕获异常,测试失败

以下是输出:

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 2.7+/python 3.1+),则可以将
与self.assertRaises(…)
表单一起使用:


您没有正确使用
assertRaises

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

with self.assertRaises(ldap.SERVER_DOWN):
    test_extractor._get_ldap_connection()
或者通常的方式(
self.assertRaises(异常、函数、参数)

另见:


是的,当然。我真傻。谢谢你@falsetru。我会把你的答案标记为正确的。@eikonomega,不客气。这是我最喜欢的(?)错误之一。;)
self.assertRaises(ldap.SERVER_DOWN, test_extractor._get_ldap_connection)