Konrad Delong wrote:
On 23 April 2010 11:56, C.T. Matsumoto <[email protected]> wrote:
Hello all,

Does anyone know a good way to test function objects? It's easy enough to
test class attributes, but for some reason I'm finding it difficult to test
if a function has the right 'attributes'.

I'm using unittest and nosetests.

Sample function:

def foo():
   x = 3
   y = 4

This example is super simple but lets pretend that x and y were derived from
some more complicated code that was done inside the function. The best way I
can come up with is moving the complicated code into another function that
returns the derived value and then test the return value, but if i want to
keep the code in the function how can I get to those 'attribute' values to
test?


x and y in your example are not attributes, but variables. They
disappear right after the function returns. There is no way to access
their values from outside. The only thing you can test for functions
is its return value (for a given set of arguments). If x and y are in
fact generated by a complicated expression you should extract those
expressions into external function.

BEFORE
def foo(arg1, arg2):
  x = <huge complicated expression that depends on values of arg1 and arg2>
  return x + 7

AFTER
def huge_complicated_expression(arg1, arg2):
  return <huge complicated expression that depends on values of arg1 and arg2>

def foo(arg1, arg2):
  x = huge_complicated_expression(arg1, arg2)
  return x + 7


cheers,
Konrad
_______________________________________________
Python-nl mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-nl


Okay thanks Konrad & Zaheer

T

--
C.T. Matsumoto
Claes de Vrieselaan 60a III
3021 JR Rotterdam
The Netherlands

tel.: +31 (0)6 41 45 08 54
_______________________________________________
Python-nl mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-nl

Antwoord per e-mail aan