对嵌套函数运行单元测试 [英] Running unit tests on nested functions

查看:93
本文介绍了对嵌套函数运行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Java世界,你可以隐藏变量和函数,然后使用反射对它们运行单元测试。我使用嵌套函数来隐藏我的类的实现细节,以便只有公共API是可见的。我试图写这些嵌套函数的单元测试,以确保我不打破他们,因为我发展。我尝试调用一个嵌套的函数,如:

I come from the Java world, where you can hide variables and functions and then run unit tests against them using reflection. I have used nested functions to hide implementation details of my classes so that only the public API is visible. I am trying to write unit tests against these nested functions to make sure that I don't break them as I develop. I have tried calling one of the nested functions like:

def outer():
    def inner():
        pass

outer.inner()

错误讯息:


AttributeError:'function'object没有属性'inner'

AttributeError: 'function' object has no attribute 'inner'

有没有办法为这些嵌套函数编写单元测试?如果没有,是否有一种方法来触发函数名的名称,像你可以为类变量前缀__?

Is there a way for me to write unit tests against these nested functions? If not, is there a way to trigger the name munging for function names like you can for class variables by prefixing them with __?

推荐答案

Python约定是以下划线命名私有函数和方法。当你看到一个前导下划线时,你不知道如何尝试使用它。

The Python convention is to name "private" functions and methods with a leading underscore. When you see a leading underscore, you know not to try and use it.

请记住, Python不是Java

这篇关于对嵌套函数运行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆