Unit Testing: Assert that a file/path exists

sazr picture sazr · Apr 13, 2013 · Viewed 20.4k times · Source

I am attempting to create a regression test for my Installer. The regression test is a script written in Python. The test checks that the correct files have been installed in the correct place.

Is there a way to assert that a file/folder exists? I am getting a AssertionError error for the following code:

assert os.path.exists(LOCAL_INSTALL_DIR) == 1

Why am I getting this error and how can I fix it? My function:

def check_installation_files_exist():
    assert os.path.exists(LOCAL_INSTALL_DIR) == 1
    assert os.path.exists(INSTALL_DIR) == 1
    correct_install_files = normalise_file_names( os.listdir( LOCAL_INSTALL_DIR ) )
    installed_files       = normalise_file_names( os.listdir( INSTALL_DIR ) )

Answer

Michael Hoffman picture Michael Hoffman · Apr 13, 2013

The path described by LOCAL_INSTALL_DIR either does not exist, is a broken symbolic link, or you do not have permission to stat() it.