1

我有要测试的代码:

from random import randint

class End(object):
          def __init__(self):
             self.quips=['You dead', 'You broke everything you can','You turn you head off']

          def play(self):
                print self.quips[randint(0, len(self.quips)-1)]
                sys.exit(1)

如何使用 accept_raises 检查它是否退出?

def test_End():
    end=End().play()
    assert_raises(what should I put here)
4

2 回答 2

1

I prefer the @raises decorator

from nose.tools import raises

@raises(SystemExit)
def test_End():
    end=End().play()
于 2013-08-15T09:21:56.670 回答
0

您可以在assertRaisesSystemExit中捕获异常:

with self.assertRaises(SystemExit):
    End().play()

sys.exit或通过mock修补:

with patch.object(sys, 'exit') as mock_method:
    End().play()
    self.assertTrue(mock_method.called)
于 2013-08-12T08:52:28.147 回答