mardi 8 octobre 2019

How to unittest specific implementation

I am reading a lot about (unit)testing and I try to implement as much as possible in my day-to-day workflow, but somehow I have the feeling I am doing something wrong.

Let's say I have a function which takes a path and based on some elements of this path it creates a name for a new log file. The path could be C:/my_project/dir_1/message01 and it should convert this to dir_1_log_01.txt. The name of the function is convertPathToLogfileName.

If I would want to write a unittest for this function it could look like:

def test_convertPathToLogfileName():   
    path = "C:/my_project/dir_1/message01"

    expected = "dir_1_log_01.txt"
    actual = convertPathToLogFileName(path)

    assertEqual(expected, actual)

Now I could write a bunch of tests like these to check for all different kinds of inputs if the output is what I expect it to be.

But what if at one point I decide that the naming convention for the logfile I chose is not what I want anymore: I would change the function to make it implement my new requirement and all the tests would fail.

This is just a simple example, but I feel that often this is the case. While I'm programming I come up with a new way of doing something and then my test fail.

Is there something I am missing here, am I testing the wrong thing? And if so, how would you approach this situation? Or is this just the way it is and should I accept this?

Aucun commentaire:

Enregistrer un commentaire