-- Leo's gemini proxy

-- Connecting to gemini.conman.org:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

What is a “unit test?”

Despite the emphasis on testing at The Enterprise, no one there was able to answer the simple question I would often ask, “what is a unit test?”


On thinking about it since I left [1], I don't think there's an answer to that question. I'm thinking it really depends upon the language being used, and it's a similar concept to Design Patterns [2], a collection of patterns seen in Smalltalk development and later forced onto other languages, applicability be damned.


Since most of the coding I do is in C, a “unit” would most likely be a function, or maybe a collection of functions known colloquially as “a library.” The various components I worked on, like “Project: Lumbergh [3]” or “Project: Sippy-Cup [4]” aren't libraries, and most functions in those projects are single use that exist just for organizational sake, so of course the “unit” ended up being the entire program.


But I'm also looking at some of my own projects, like mod_blog [5]. There's a fair number of stand-alone functions in here I could possibly “unit test” if I were inclined. The first one is this function (found here [6]):



int max_monthday(int year,int month)
{
  static int const days[] = { 31,0,31,30,31,30,31,31,30,31,30,31 } ;

  assert(year  > 1969);
  assert(month >    0);
  assert(month <   13);

  if (month == 2)
  {
    /*----------------------------------------------------------------------
    ; in case you didn't know, leap years are those years that are divisible
    ; by 4, except if it's divisible by 100, then it's not, unless it's
    ; divisible by 400, then it is.  1800 and 1900 were NOT leap years, but
    ; 2000 is.
    ;----------------------------------------------------------------------*/

    if ((year % 400) == 0) return 29;
    if ((year % 100) == 0) return 28;
    if ((year %   4) == 0) return 29;
    return 28;
  }
  else
    return days[month - 1];
}


I'm sorry, there's no way I'm going to even waste time writing unit tests for a function this simple. I didn't bother when I first wrote it in Debtember of 1999, and there's no point in writing one now. Even if the leap year rules change in 1,980 years [7], I probably still won't write unit tests for this function (probably because I'll be dead by then, but that's besides the point).


But that's not to say there aren't other functions that couldn't be “unit tested.” The next one I have in mind [8] is simple, but I would love to see a unit test purist tell me how they would write a unit test for it.


Update on Friday, Debtember 23^rd, 2022


I “unit tested” the code [9].


[1] /boston/2022/10/05.1

[2] http://www.amazon.com/exec/obidos/ASIN/0201633612/conmanlaborat-20

[3] /boston/2018/09/11.2

[4] /boston/2014/03/05.1

[5] https://github.com/spc476/mod_blog

[6] https://github.com/spc476/mod_blog/blob/049e9dcf0985d4e089ac3593f39c987f077f0bcb/src/timeutil.c#L29

[7] http://computer-programming-forum.com/48-cobol/1ee56472420eb83e.htm

[8] https://github.com/spc476/mod_blog/blob/049e9dcf0985d4e089ac3593f39c987f077f0bcb/src/hooks.c#L39

[9] /boston/2022/12/21.1


Discussions about this entry


Re: What is a unit test

Re: What is a "unit test"?

Re: What is a "unit test?"

Re: What is a “unit test?”


Gemini Mention this post

Contact the author


-- Response ended

-- Page fetched on Mon May 20 16:02:22 2024