CherryPy 仅在测试时公开功能

CherryPy expose function only when testing

我正在构建我的第一个 CherryPy 应用程序,并想测试一个不应公开可见(不公开)的方法,如果它被公开,我可以很好地测试该方法,但是我想知道是否有办法切换公开该方法取决于调用该函数的文件。例如,如果函数是从 mainApp 调用的,那么如果它是从 mainApp 调用的,它不会被公开,但是如果它是从测试文件调用的,那么它会被公开吗?

the code I was thinking of is along the lines of

if __name__ != '__main__': @cherrypy.expose
def supersecretmethod(self)

但是我发现这不起作用并且做了一些研究但似乎无法弄清楚如何做到这一点,有什么建议吗?谢谢

您可以为此目的使用自定义装饰器:

def expose_if_not_main(func):
    if __name__ != '__main__':
        return cherrypy.expose(func)
    else:
        return func


@expose_if_not_main
def supersecretmethod(self):
    return 'result'