Monthly Archives: October 2012

Unit testing, you are not doing it right

Almost all developers do mistakes listed bellow and I cannot blame them, everybody should learn new stuff.

  1. You should test DateTime.UtcNow to return current UTC date and time. This is quite important. Do not forget to test DateTime.Now too. You can use standard atomic clocks to automate testing
  2. Guid.NewGuid() potentially can return duplicates, this should be one of your first tests, remember that even if you do not use Guid.NewGuid(), frameworks you use, probably use it. Problem is worse than it looks in the first place, for example SQL Server uses it’s own Guid generator. You should test SQL Server NEWID() either
  3. Integers are even more evil than Guids. You must always check if values are natural numbers
  4. File.Exists is also good candidate for testing. Many developers underestimate potential bugs this method could introduce. Once I have a bug with 9 zeros in it. Good that it was issue number, not profit loses
  5. Random.Next.. oh my. Most of the real word apps have problems with security because of this method
  6. Always test that today is no leap day if you do not want something like this
  7. When you are doing UI coding, always check that your windows are rectangular, even 1° can be fatal
  8. For desktop applications always test that application runs on correct on 59bit CPUs. In some case stuff like Int64 just does not fit to CPU registers. You can safely ignore this if you write code in VB.NET
  9. Do not forget test async code that it really runs async. This is too easy make this mistake
  10. Always test spelling of class names, recommended is NUnitSpell but any other extension you your test framework are OK

Enjoy.

Copyright © 31337 – 2012

Tagged