Is there a way to get py.test to ignore SystemExit raised on a child process?

I am testing a Python module that contains the following code snippet.

        r, w = os.pipe()
        pid = os.fork()
        if pid:
            os.close(w)        # use os.close() to close a file descriptor
            r = os.fdopen(r)   # turn r into a file object
            # read serialized object from ``r`` and persists onto storage medium
            self.ofs.put_stream(bucket, label, r, metadata)
            os.waitpid(pid, 0) # make sure the child process gets cleaned up
        else:
            os.close(r)
            w = os.fdopen(w, 'w')
            # serialize object onto ``w``
            pickle.dump(obj, w)
            w.close()
            sys.exit(0)
        return result

All tests pass, but there are difficulties regarding sys.exit(0). When sys.exit(0)executed, it raises SystemExit, which is intercepted py.testand reported as an error in the console.

I don’t understand in detail what py.testit is doing internally, but it looks like it goes ahead and ultimately ignores such an event caused by a child process. In the end, all the tests pass, which is good.

But I want to have clean output in the console.

Is there any way to py.testget a clean output?

For information:

  • Debian Jessie, kernel 3.12.6
  • Python 2.7.6
  • pytest 2.5.2

Thank:)

+3
3

( )

, , . , sys.exit(n) os._exit(n), n - .

:

import os
os._exit(0)

: SystemExit sys.exit()?

+2

- sys.exit

@mock.patch('your.module.sys.exit')
def test_func(mock_sys_exit):
    mock_sys_exit.side_effect = SystemExit("system is exiting")
    with pytest.raises(SystemExit):
        # run function that supposed to trigger SystemExit
0
def test_mytest():

    try:
        # code goes here
        # like
        # import my_packaage.__main__
        # which can raise
        # a SystemExit 
    except SystemExit:
        pass

    assert(True)

I found it here .

0
source

All Articles