mardi 22 décembre 2015

How to mock a method used in method call

I would like to write a unit test for a function which uses os.path methods like listdir, os.path.join, etc.

# file my_function.py
import os
my_function():
    //do something with os, e.g call os.listdir
    return os.listdir(os.getcwd())

# file test_my_function.py
test_my_function():
    os = MagickMock()
    os.listdir = MagickMock(side_effect=["a","b"])
    self.assertEqual("a",my_function())

I assume os does it's job and want to restrict my unittest to test only my code and mock therefore the return values of the os method calls. Is this possible?

Aucun commentaire:

Enregistrer un commentaire