Is it possible for a unit test to assert that a method calls sys.exit()?

后端 未结 4 1572
旧巷少年郎
旧巷少年郎 2020-12-02 14:02

I have a Python 2.7 method that sometimes calls

sys.exit(1)

Is it possible to make a unit test that verifies this line of code is called when

4条回答
  •  清歌不尽
    2020-12-02 14:29

    I found the answer to your question in the Python Unit Testing documentation search for "Testing for Exceptions". Using your example, the unit test would look like the following:

    self.assertRaises(SystemExit, your_function, argument 1, argument 2)
    

    Remember to include all arguments needed to test your function.

提交回复
热议问题