python - 我可以在 python 中对内部函数进行单元测试吗?

标签 python unit-testing doctest

有什么方法可以为 innerfunc 编写 unittestsdoctests 吗?

def outerfunc():
    def innerfunc():
        do_something()
    return innerfunc()

最佳答案

仅当您提供一种方法来提取内部函数对象本身时,例如

def outerfunc(calltheinner=True):
    def innerfunc():
        do_something()
    if calltheinner:
        return innerfunc()
    else:
        return innerfunc

如果您的外部函数坚持将内部函数完全隐藏在自身内部(在适当的劝说下绝不让它渗透到外部),您的单元测试无力击败这种对极端和完全隐私的强烈要求;-)。

关于python - 我可以在 python 中对内部函数进行单元测试吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2136910/

相关文章:

python - 是否可以使用 "manage.py test"在 Django 中运行单个 doctest

python - 为什么我会收到无效语法 easy_install 错误?

python - Django内置的Web服务器: Usage and Reliability Concerns

python - 使用转义的 ascii 字符串正确解析 html 页面

python - 模拟 Flask before_first_request

python - 有没有办法在 python doctest 中重新启动或重置 python 解释器?

python - python 的 SHA1 字符串正则表达式

android - 让暂停功能不返回任何东西

c# - 如何在 Visual Studio 2017 15.6.0 中调试单元测试

python - 是否可以在模块中仅使用 doctest 测试特定功能?