Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a function that creates a directory?

How to test a function in Python which creates a directory? Can you give an example make_directory_test() function?

def make_directory(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)
like image 334
Valeriy Avatar asked Sep 23 '15 19:09

Valeriy


1 Answers

Just test that directory exists

def test_make_directory(self):
    directory_path = '...' # somepath

    make_directory(directory_path)

    self.assertTrue(os.path.exists(directory_path))

And do ensure that any directories created in unit tests are deleted after each tests in tearDown or setUp, to ensure independency of tests

like image 116
hspandher Avatar answered Sep 21 '22 12:09

hspandher