How to test or mock “if __name__ == '__main__'” contents

前端 未结 7 704
無奈伤痛
無奈伤痛 2020-12-07 20:07

Say I have a module with the following:

def main():
    pass

if __name__ == \"__main__\":
    main()

I want to write a unit test for the b

7条回答
  •  无人及你
    2020-12-07 20:37

    I found this solution helpful. Works well if you use a function to keep all your script code. The code will be handled as one code line. It doesn't matter if the entire line was executed for coverage counter (though this is not what you would actually actually expect by 100% coverage) The trick is also accepted pylint. ;-)

    if __name__ == '__main__': \
        main()
    

提交回复
热议问题