Question

Thanks for your help in advance.

I've got the following class method that I'm trying to test:

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))

And here's the unittest:

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())

So far, so good. But when I execute the unit tests (via nose) test_extractor._get_ldap_connection() is called from the assertRaises statement, but the exception is not caught and the test fails.

Here is the output:

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

Help me!

Était-ce utile?

La solution

Don't call, just pass function (method) itself; drop ():

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

Alternatively, you can use with self.assertRaises(..) form if you are using recent version of python (Python 2.7+ / Python 3.1+):

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

Autres conseils

You are not using assertRaises correctly.

You can use it as a context manager:

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

or the usual way (self.assertRaises(exception, function, args):

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

Also see:

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top