Maybe you should raise an exception when x or y have the wrong value, because you don't want the wrong local variables in your function anyway, I assume. Then (unit)test if this actually happens with the "wrong" parameters in your function.
Gr, Zaheer 2010/4/23 Konrad Delong <kon...@gmail.com> > On 23 April 2010 11:56, C.T. Matsumoto <c.t.matsum...@gmail.com> 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 > Python-nl@python.org > http://mail.python.org/mailman/listinfo/python-nl >
_______________________________________________ Python-nl mailing list Python-nl@python.org http://mail.python.org/mailman/listinfo/python-nl