python - Use `unittest` to verify that the `exit` function was called -
i defined simple configuration manager (parses config files , verifies keys exist in them) , i'm writing tests it.
in many cases config file is invalid, want configuration handler call exit()
. there way can write tests ensure exit
called , still continue testing? can perhaps, testing environment only, "mock" exit
function?
i using python 2.7 , unittest
.
sys.exit()
raises systemexit
exception, should caught unittest framework. can check systemexit
raised test cases.
example, exit_test.py
:
import sys import unittest def func(): sys.exit() class mytest(unittest.testcase): def test_func(self): self.assertraises(systemexit, func) if __name__ == "__main__": unittest.main()
run:
$ python exit_test.py . ---------------------------------------------------------------------- ran 1 test in 0.000s ok
Comments
Post a Comment