用于测试Optparse错误/缺少参数的TestCase



我想测试传递给代码的选项/参数及其预期结果。我使用Optparse来接受这些论点。现在我正在测试的代码只是执行

parser.error("Some message")

当我错过了一个论点,或者如果我通过了一个糟糕的论点。

一旦进入一个糟糕的争论。我该如何测试?在unittest.TestCase中,我使用哪种方法来断言发生了parser.error()

您应该注意mock模块:

import mock
import unittest

class TestCase(unittest.TestCase):
    def test_parser(self):
        with mock.patch('path.to.your.function') as error_mock:
            call_your_code()
            self.assertEqual(error_mock.called, True)

最新更新