Setting command line arguments for main function tests

后端 未结 3 1801
我在风中等你
我在风中等你 2021-01-12 04:51

I have a main() function in python that gets command line arguments. Is there a way for me to write pytest tests for this function and define the arguments in the code?

3条回答
  •  误落风尘
    2021-01-12 05:18

    The best solution I found so far is this

    def test_main():
        sys.argv = ["some_name", "abc"]
        res = main()
    

    and for flags:

    sys.argv.append("-f")
    sys.argv.append("v")
    

提交回复
热议问题