-1

Assume we have the following code:

def second_func(a, b, c):
    """ some other definition"""
    return something


def first_func(x, y, z):
    """ some definition"""
    d = second_func(a, b, c)
    """ some definition"""
    return something

and we are trying to write a test for first_func. As you can see first_func calls second_func with some arguments a, b, c that are generated inside its definition.

Is there any way to test what the return value of second_func call inside first_func would be? (in other words what d would be?)


It's just a simple form of a very bigger question. I'm testing my telegram bot with telethon user and I need to test some return value of some functions that is used due to sending message with bot.

That is I can not change return value or even definition of functions and I was hoping somehow (by wrapping or patching those functions) I could see what they returned inside my e2e test.

Ashkan Khademian
  • 307
  • 3
  • 12
  • 1
    If you make d global (not recommended) you can check the value of d after the function call. But why not write tests for ``second_func`` directly? – Mike Scotty Nov 02 '21 at 09:09
  • 4
    No. You want to test `first_func`'s input/output, ***not how it is implemented***. You want to ensure that when you pass `foo` to `first_func`, that it returns `bar`. *How* it does that is irrelevant and may in fact change in the future. You can write separate tests for `second_func` if you want to ensure *its* input/output. – deceze Nov 02 '21 at 09:10
  • edited my question so that I could explain my situation better. @deceze – Ashkan Khademian Nov 02 '21 at 09:14
  • 1
    "and we are trying to write a test for first_func" Okay, well, as long as the returned value is correct, the function is correct. There is nothing about the specification that requires `d` to have the correct value at any point - or to *exist*. "But I 'know' that `first_func` relies on `second_func`, and I want to make sure that the problem isn't in `second_func` if `first_func` gives the wrong answer" - Okay, so **write a separate test for `second_func`, which you need to do anyway**. – Karl Knechtel Nov 02 '21 at 09:15
  • 1
    "edited my question so that I could explain my situation better" That explanation doesn't matter (and wasn't needed or asked for); the advice is the same. – Karl Knechtel Nov 02 '21 at 09:16
  • I tried to come up with a function patch that modifies the scope of a local variable to a global variable, but after patching the function signature (moving ``d`` from co_varnames to co_names), I figured it was a very clumsy approach and finding+replacing he correct occurence of ``STORE_FAST`` with ``STORE_GLOBAL`` in the function's bytecode (``f.__code__.co_code``) is something I currently cannot invest more time in. So yeah, I think it's possible to patch the functions. If you want to give it a shot yourself, you can use this as a starting point: https://stackoverflow.com/questions/33348067/ – Mike Scotty Nov 02 '21 at 10:16

1 Answers1

-1

Try to specify default parameters that you need to use in your function and if you call it without params, you will get default result and may do with it what you want

def second_func(a=0, b=1, c=2):
    """ some other definition"""
    return a + b * c


def first_func():
    """ some definition"""
    d = second_func()
    print(type(d))
    """ some definition"""
    return 0

first_func()