[Python] How to mock a function which gets executed during the import time?

Discussão em 'Python' iniciado por Stack, Julho 24, 2025.

  1. Stack

    Stack Membro Participativo

    Here the ABC() and obj.print_1() get called during the import time and it prints "making object" and "printed 1" respectively. How can we mock all the three functions, __init__(), print_1(), and print_2()?

    xyz.py

    from abc import ABC
    obj = ABC()
    obj.print_1()

    def func():
    return obj.print_2(2)


    abc.py

    class ABC():
    def __init__(self):
    print("making object")

    def print_1(self):
    print("printed 1")
    return None

    def print_2(self, val):
    print("printed ", val)
    return None

    Continue reading...

Compartilhe esta Página