How is the oder of test functions determined?

17 views
Skip to first unread message

Raghuram Devarakonda

unread,
Aug 7, 2009, 12:02:57 PM8/7/09
to nose-...@googlegroups.com
I thought that nose collects (and runs) test functions in the order
that they are defined in a file. This generally seems to be the case.
How ever, one of those functions is decorated, the oder seems to
change. For example, check the following code:

-----
def test_g():
pass

def test_f():
pass
-----

When this file is passed to nose, g() and f() are run in order as
expected. How ever, when f() is decorated as follows:

-----
import functools

def dectest(func):
def wrapper(*args, **kw):
return func()

return functools.update_wrapper(wrapper, func)


def test_g():
pass

@dectest
def test_f():
pass
-----

the oder of tests becoms f() and g(). Can some one tell me how
decorator is impacting the oder here?

On a separate note, nose docs say that the test methods in a class are
run alphabetically. Is there any way for them to be run in the same
order they are defined?

Thanks,
Raghu

jason pellerin

unread,
Aug 7, 2009, 12:51:37 PM8/7/09
to nose-...@googlegroups.com
On Fri, Aug 7, 2009 at 12:02 PM, Raghuram
Devarakonda<drag...@gmail.com> wrote:
>
> I thought that nose collects (and runs) test functions in the order
> that they are defined in a file. This generally seems to be the case.
> How ever, one of those functions is decorated, the oder seems to
> change.

> the oder of tests becoms f() and g(). Can some one tell me how


> decorator is impacting the oder here?

This is because the decorator replaces the function; nose uses the
co_firstlineno attribute of the func_code of the function to put tests
in order. When nose sees the function it doesn't see f(), but wrapper,
which is defined earlier in the file. If you want to avoid that, use
nose.tools.make_decorator.

> On a separate note, nose docs say that the test methods in a class are
> run alphabetically. Is there any way for them to be run in the same
> order they are defined?

Not automatically, no. They don't have func_code.co_firstlineno
attributes to use for sorting. So if you want them to come out in the
same order you have them in the file, you have to name them
alphabetically (test_01_real_name, test_02_another_test, etc).

JP

Raghuram Devarakonda

unread,
Aug 7, 2009, 1:12:16 PM8/7/09
to nose-...@googlegroups.com
On Fri, Aug 7, 2009 at 12:51 PM, jason pellerin<jpel...@gmail.com> wrote:
>
> On Fri, Aug 7, 2009 at 12:02 PM, Raghuram
> Devarakonda<drag...@gmail.com> wrote:
>>
>> I thought that nose collects (and runs) test functions in the order
>> that they are defined in a file. This generally seems to be the case.
>> How ever, one of those functions is decorated, the oder seems to
>> change.
>
>> the oder of tests becoms f() and g(). Can some one tell me how
>> decorator is impacting the oder here?
>
> This is because the decorator replaces the function; nose uses the
> co_firstlineno attribute of the func_code of the function to put tests
> in order. When nose sees the function it doesn't see f(), but wrapper,
> which is defined earlier in the file. If you want to avoid that, use
> nose.tools.make_decorator.

Thanks. Using nose.tools.make_decorator does the trick.

Kenneth Arnold

unread,
Aug 7, 2009, 2:06:38 PM8/7/09
to nose-...@googlegroups.com
On Fri, Aug 7, 2009 at 12:51 PM, jason pellerin<jpel...@gmail.com> wrote:
> Not automatically, no. They don't have func_code.co_firstlineno
> attributes to use for sorting. So if you want them to come out in the
> same order you have them in the file, you have to name them
> alphabetically (test_01_real_name, test_02_another_test, etc).

IPython can find method source, so I dug into this a bit.
method.im_func.func_code.co_firstlineno seems to work.

-Ken

Reply all
Reply to author
Forward
0 new messages