Python 单元测试是否可以断言方法调用 sys.exit()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15672151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:40:22  来源:igfitidea点击:

Is it possible for a unit test to assert that a method calls sys.exit()

pythonunit-testing

提问by Travis Bear

I have a python 2.7 method that sometimes calls

我有一个有时会调用的 python 2.7 方法

sys.exit(1) 

Is it possible to make a unit test that verifies this line of code is called when the right conditions are met?

是否可以进行单元测试来验证在满足正确条件时调用了这行代码?

采纳答案by Pavel Anossov

Yes. sys.exitraises SystemExit, so you can check it with assertRaises:

是的。sys.exitraises SystemExit,所以你可以检查它assertRaises

with self.assertRaises(SystemExit):
    your_method()

Instances of SystemExithave an attribute codewhich is set to the proposed exit status, and the context manager returned by assertRaiseshas the caught exception instance as exception, so checking the exit status is easy:

的实例SystemExit具有code设置为建议退出状态的属性,并且 返回的上下文管理器assertRaises具有捕获的异常实例 as exception,因此检查退出状态很容易:

with self.assertRaises(SystemExit) as cm:
    your_method()

self.assertEqual(cm.exception.code, 1)

 

 

sys.exit Documentation:

sys.exit 文档

Exit from Python. This is implemented by raising the SystemExitexception ... it is possible to intercept the exit attempt at an outer level.

退出 Python。这是通过引发SystemExit异常来实现的……可以在外部级别拦截退出尝试。

回答by Travis Bear

Here's a complete working example. In spite of Pavel's excellent answer it took me a while to figure this out, so I'm including it here in the hope that it will be helpful.

这是一个完整的工作示例。尽管 Pavel 的回答很好,但我还是花了一段时间才弄明白,所以我把它包括在这里,希望它会有所帮助。

import unittest
from glf.logtype.grinder.mapping_reader import MapReader

INCOMPLETE_MAPPING_FILE="test/data/incomplete.http.mapping"

class TestMapReader(unittest.TestCase):

    def test_get_tx_names_incomplete_mapping_file(self):
        map_reader = MapReader()
        with self.assertRaises(SystemExit) as cm:
            tx_names = map_reader.get_tx_names(INCOMPLETE_MAPPING_FILE)
        self.assertEqual(cm.exception.code, 1)

回答by binarysubstrate

As an additional note to Pavel's excellent answer you can also check for specific statuses if they're provided in the function you're testing. For example if your_method()contained the following sys.exit("Error")it would be possible to test for "Error" specifically:

作为对 Pavel 出色答案的附加说明,您还可以检查特定状态(如果它们在您正在测试的函数中提供)。例如,如果your_method()包含以下内容,sys.exit("Error")则可以专门测试“错误”:

with self.assertRaises(SystemExit) as cm:
    your_method()
    self.assertEqual(cm.exception, "Error")

回答by sleepykyle

I found the answer to your question in the Python Unit Testing documentationsearch for "Testing for Exceptions". Using your example, the unit test would look like the following:

我在Python 单元测试文档搜索“异常测试”中找到了您的问题的答案。使用您的示例,单元测试如下所示:

self.assertRaises(SystemExit, your_function, argument 1, argument 2)

Remember to include all arguments needed to test your function.

请记住包含测试函数所需的所有参数。