Mon Sep 25 10:52:56 2006

Ticket #92 (Closed: fixed)

@with_setup doesn't mix well with @raises


Priority: normal Reporter: guest
Severity: major Assigned to: jpellerin
Component: nose Status: closed
Version: 0.9 Resolution: fixed
Milestone: 0.9.2 Keywords:  

Description by guest:

Hi,

I tried to write the following code:

@raises(NotEnoughBytes?) @with_setup(setup1) def test32():

...

This failed in a strange way. It seems nose either called the wrong setup function (I have different setup functions for different tests), or didn't call one at all (I didn't investigate very far). The fix came by inverting the decorator order:

@with_setup(setup1) @raises(NotEnoughBytes?) def test32():

...

Attachments

  • patch-92.txt (2 kB) - patch against current svn rev, added by guest on Wed Oct 25 05:37:21 2006.
  • patch-92-good.txt (2 kB) - better patch, added by guest on Wed Oct 25 08:39:13 2006.

Changelog

Mon Sep 25 10:56:54 2006: Modified by guest

    btw, since there I didn't find a way to register on the wiki, you can contact me at antoine.pitrou|at|wengo.fr.

    Thu Oct 12 13:56:49 2006: Modified by jpellerin

    • milestone set to 0.9.2
    • status changed from new to assigned

    Tue Oct 17 09:10:23 2006: Modified by guest

      The following code in tools.py should fix it (at least it works for me):

      def test_decorator(d):
          """
          Wraps a test decorator so as to properly replicate metadata
          of the decorated function, including Nose's additional stuff
          (namely, setup and teardown).
          """
          def wrapped(*args, **kargs):
              def decorate(func):
                  name = func.__name__
                  newfunc = d(*args, **kargs)(func)
                  try:
                      newfunc.__doc__ = func.__doc__
                      newfunc.__module__ = func.__module__
                      newfunc.__name__ = name
                  except TypeError:
                      # can't set func name in 2.3
                      newfunc.compat_func_name = name
                  try:
                      newfunc.setup = func.setup
                  except AttributeError:
                      pass
                  try:
                      newfunc.teardown = func.teardown
                  except AttributeError:
                      pass
                  return newfunc
              return decorate
          return wrapped
      

      And then:

      raises = test_decorator(raises)
      timed = test_decorator(timed)
      

      Regards

      Antoine (e-mail above).

      Wed Oct 25 05:37:21 2006: Modified by guest

      • attachment added: patch-92.txt

      Wed Oct 25 05:38:58 2006: Modified by guest

        The patch I just attached is against current SVN trunk (rev. 108). It is also slightly better than the previous proposal because the decorator applies at the right level, semantically.

        Antoine.

        Wed Oct 25 08:38:37 2006: Modified by guest

          Sorry, the patch is buggy. Attaching a new version.

          Wed Oct 25 08:39:13 2006: Modified by guest

          • attachment added: patch-92-good.txt

          Sun Nov 12 20:37:22 2006: Modified by jpellerin

          • resolution set to fixed
          • status changed from assigned to closed

          Patch applied (just renamed nose_wrapper to make_decorator) in [111]. Thank you!