开发者

Unit testing - what's up with useless tests?

I've seen people write unit tests for things that should simply be taken for granted. E.g.:

class Employee
{ 
   public int Id { get; set; }开发者_如何转开发 
   public string Name { get; set; } 
   //.....
}

Unit test:

Assert.AreEqual(new Employee().Id, 0);

Seems like a massive waste of time and resources, yet some people do write tests like this. I've even seen it in some samples from Microsoft.

Am I missing something?


they should be targeted at behaviour, so this test that the default is 0 is pretty pointless unless there is a reason you need it to behave that way, for example a stack starting empty.


A big benefit of unit testing is the ability to quickly detect regressions. If a developer comes in years down the track and modifies the Id getter function to return something other than Id or modifies the default constructor to set the Id to something other than zero, that test will fail. That's a lot easier to debug than some obscure bug occuring weeks down the track because there was code assuming the initial value of a new Employee's Id was 0.


Unittests are never a waste. What if another programmer decides to edit the getter code for Id? You would never find that out easily unless the unit test is executed and it shows you what's wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜