jeudi 1 novembre 2018

Define variable for testing only in Python

This is probably a relatively simple question, but I struggle to find an answer otherwise.

I am working on a small Python project, and I would like to do this in a test driven way. File operations in a defined folder in the User's home directory are quite essential to the programme, but I would like these to happen in a separate temporary folder for the tests.

Is there a way to set a variable in my app that is different if the app realises it's run as part of the testing environment? My current workarounds (def some_function(self, test=False) or to include lots of @patch decorators) are not very elegant...

I'm thinking of something along the lines of:

def return_home_folder():
    if testing:
        home = os.getcwd() + '/testrun'
    else:
        home = os.path.expanduser('~')
    returnvalue = home + '/appname/'
    return returnvalue

Aucun commentaire:

Enregistrer un commentaire