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

前端 未结 7 706
無奈伤痛
無奈伤痛 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:23

    One approach is to run the modules as scripts (e.g. os.system(...)) and compare their stdout and stderr output to expected values.

提交回复
热议问题