Posts

Showing posts with the label Unit Testing

NUnit Console Runner and NLog extension logging for your tests

Image
Have you experienced the frustration of tests running perfectly well on your local machine and then failing in CI? This all too familiar "It worked on my machine" comes to mind. You set off to investigate and after a while you realize you are too far into the rabbit hole between code and logs in Kibana and this investigation is getting you nowhere. Wouldn't it be nice if you could hijack the logger and print the logs in the test runner's console? TL;DR NLog can be extended easily and NUnit  Console Runner does not play well with simple console logs.   Photo by  Kaboompics .com  from  Pexels Enter Console logging In .Net in general it is not advisable to log to console in a multi-threaded environment, because the console is not thread safe. While logging simple strings may not cause any problems logging strings with interpolated variables may very well be sub optimal to say the least. Should you use any such debugging technique you are better off with a thread safe f

Unit tests and private functions: A real life dilemma

Always Test! There is no argument whatsoever that unit tests are essential to every software nowadays. Beyond their initial use they serve as regression tests during code changes. They are also used in build environments and CI/CD to ensure merges do not negatively affect your code. And sometimes they are considered as an alternative to QA on server side code. TL;DR "It depends" This is where I currently live, server side code, and unit tests are an essential part of every feature in every sprint.  I will not go into the textbook "what are unit tests" Q and A. I assume dear reader that you know it by heart. However in case you don't know this Wikipedia entry should get you started. Opinions on unit tests in OO languages In general unit tests are used to verify the correct behavior of a module, hence in C# only the public methods of a class should be tested. Some consider testing protected or private methods unacceptable. And then there is this quote fro