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

后端 未结 4 1567
旧巷少年郎
旧巷少年郎 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:46

    As an additional note to Pavel's excellent answer, you can also check for specific statuses if they're provided in the function you're testing. For example, if your_method() contained the following sys.exit("Error"), it would be possible to test for "Error" specifically:

    with self.assertRaises(SystemExit) as cm:
        your_method()
        self.assertEqual(cm.exception, "Error")
    

提交回复
热议问题