Unit-testing – Is it considered ‘bad practice’ to check file contents/encoding in unit tests

unit testing

A bit of context: Earlier today I had to update some SQL code that another colleague of mine provided, and since it’s a pretty large script, it’s stored as a separate file (which is then read and executed at runtime). While doing this I accidentally reintroduced two bugs we had a few months back, namely:

  • For whatever reason the ASCII file was encoded in UTF-16 (the colleague emailed me the file, which might have caused it).
  • The script was missing initial SET statements (required due to some driver things on production, but not on a clean install locally).

After debugging this for about an hour (again) I decided to write some unit tests to ensure this would never happen again (and include a quick way to fix it in the assertion message to provide an easy fix for future developers).

However when I pushed this code another colleague (who is also our team lead) walks up to me and told me I shouldn't make these things again because:

"These things don't belong in unit tests"

"Unit tests should only be used to check the flow of your code"

I’m pretty conflicted now since I still think what I’m doing isn’t wrong, as this bug wouldn’t be reintroduced in the future, however this colleague works as a senior and at the end of the day gets to decide what we spend our time on. What should I do? Am I wrong for doing it this way? Is it considered bad practice?

Best Answer

Most likely the tests you wrote are closer to integration or regression tests than unit tests. While the line can be very fuzzy and sometimes devolves into pedantry over what is or is not a unit test, I would go back to your colleague and ask where the tests you wrote should be since they do add value ensuring correctness of the code.

I would not focus to much on what is or isn't a unit test and realize that even if its an integration test, there could still be value in the test.

Related Topic