Search code examples
pythonwebweb-applicationscherrypy

CherryPy expose function only when testing


I am building my first CherryPy application and want to test a method that should NOT be publicly visable (not exposed) I can test the method perfectly fine if it is exposed however I was wondering if theres a way to toggle exposing the method depending on what file calls the function. For example if the function is being called from so if it is called from the mainApp it will not be exposed but if it is called from the test file be exposed?

the code I was thinking of is along the lines of

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

however I can see this doesnt work and have done some research but cant seem to figure out how to do this, any suggestions? Thanks


Solution

  • You may use custom decorator for this purpose:

    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'