Is it possible to test a function using `os._exit ()`?

I want to check what the function performs os._exit(2)on error. I have seen many solutions with sys.exit()using SystemExit. I read both Python3 and Python2 and it seems that it is os._exit()not usingSystemExit

However, I tried this in case it was a misunderstanding of the documentation on my side, but it just comes out of the net test, this is not even a test error:

make: *** [test] Error 2

This is probably due to the call function os._exit(2)

+4
source share
2 answers

os.system() 8 , script :

import os

assert os.system('python script.py') >> 8 == 2

os._exit() sys.exit():

import os, sys
import script

os._exit = sys.exit
script.tested_method()  # raises SystemExit
+2

unittest.mock.MagicMock , .

from unittest import mock
import os

def funcToTest():
    os.exit(2)

def test_func():
    os._exit = mock.MagicMock()
    funcToTest()
    assert os._exit.called
+1

Source: https://habr.com/ru/post/1681766/


All Articles