mercredi 16 septembre 2015

Python Unit Testing - Should Other ClassMethods be Mocked?

This is more of a process question than anything else, but I have been programming in Python for a bit now, and I am trying to understand the differences between Unit Tests, Functional Tests, and when to appropriately use mocks in order to test functions. I have the following arrangement:

@classmethod
def get_value(cls, key):
    if cls._definitionsDict is None:
        cls.load_definitions()

    if not key in cls._definitionsDict:
        return None
    else:
        return cls._definitionsDict[key]

Basically, now I want to write some tests for this function. There are three approaches that I am considering, and I am really unsure which would be the proper (read: most widely accepted) way of doing things.

  1. Write a test that mocks the load_definitions function, ensuring that when cls._definitionsDict is None, load_definitions is called with no args. (I'm assuming this is a "strict unit test")
  2. Write a test that does not mock the load_definitions function, but simply ensures that output is as expected given a certain input. (I'm assuming this is a "strict functional test")
  3. Do both in order to test that code flow works properly and the function works as intended. This seems extremely redundant to me.

I would love your thoughts and opinions on this. I suppose, in a sense, this question is asking about where to draw the line with mocks. What should really be mocked? Is a unit test testing code flow, or simply the input/output of a function?

As an additional note, option number 1 seems to be something true to the idea of "unit" testing; however, any change in the source code would also require the test to be updated. Is this the goal of a unit test?

Aucun commentaire:

Enregistrer un commentaire