Say you have a function you want to test that looks like this:
dp = disk_partitioner.DiskPartitioner(...)
where the DiskPartitioner class looks like this:
def __init__(self, ...):
def add_partition(self, ...):
and you have existing test code like this:
and you want to add a new test function, but adding a new patch just for your new test.
You want to verify that the class is instantiated with the right options, and you need to mock the add_partition method as well. How do you use the existing test class (with the mock of the execute function), add a new mock for the DiskPartitioner.add_partition function, and the __init__ of the DiskPartitioner class?
After a little trial and error, this is how:
def test_make_partitions_with_gpt(self, mock_dp, mock_exc):
# Need to mock the function as well
mock_dp.add_partition = mock.Mock(return_value=None)
disk_utils.make_partitions(...) # Function under test
Things to note:
1) The ordering of the mock parameters to test_make_partitions_with_gpt isn't immediately intuitive (at least to me). You specify the function level mocks first, followed by the class level mocks.
2) You need to manually mock the instance method of the mocked class. (i.e. the add_partition function)
You can see the whole enchilada over here in the review.
Post a Comment