Unit testing beginner question

Discussion in 'Python' started by Andrius, May 23, 2011.

  1. Andrius

    Andrius Guest

    Hello,

    would be gratefull for the explonation.

    I did a simple test case:

    def setUp(self):
    self.testListNone = None

    def testListSlicing(self):
    self.assertRaises(TypeError, self.testListNone[:1])

    and I am expecting test to pass, but I am getting exception:
    Traceback (most recent call last):
    self.assertRaises(TypeError, self.testListNone[:1])
    TypeError: 'NoneType' object is unsubscriptable

    I thought that assertRaises will pass since TypeError exception will
    be raised?

    Ta,
    Andrius
     
    Andrius, May 23, 2011
    #1
    1. Advertising

  2. Andrius

    Ian Kelly Guest

    On Mon, May 23, 2011 at 4:30 PM, Andrius <> wrote:
    > and I am expecting test to pass, but I am getting exception:
    > Traceback (most recent call last):
    >    self.assertRaises(TypeError, self.testListNone[:1])
    > TypeError: 'NoneType' object is unsubscriptable
    >
    > I thought that assertRaises will pass since TypeError exception will
    > be raised?


    The second argument to assertRaises must be a function that
    assertRaises will call. assertRaises can't catch the error above
    because it is raised when the argument is evaluated, before
    assertRaises has even been called.

    This would work:

    self.assertRaises(TypeError, lambda: self.testListNone[:1])

    Cheers,
    Ian
     
    Ian Kelly, May 23, 2011
    #2
    1. Advertising

  3. Andrius

    Roy Smith Guest

    In article <>,
    Ian Kelly <> wrote:

    > This would work:
    >
    > self.assertRaises(TypeError, lambda: self.testListNone[:1])


    If you're using the version of unittest from python 2.7, there's an even
    nicer way to write this:

    with self.assertRaises(TypeError):
    self.testListNone[:1]
     
    Roy Smith, May 24, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Maclean
    Replies:
    1
    Views:
    346
    Martin P. Hellwig
    Apr 13, 2010
  2. Ulrich Eckhardt

    unit-profiling, similar to unit-testing

    Ulrich Eckhardt, Nov 16, 2011, in forum: Python
    Replies:
    6
    Views:
    336
    Roy Smith
    Nov 18, 2011
  3. Bill Mosteller
    Replies:
    0
    Views:
    230
    Bill Mosteller
    Oct 22, 2009
  4. Avi
    Replies:
    0
    Views:
    497
  5. Avi
    Replies:
    0
    Views:
    467
Loading...

Share This Page