How to prevent 'over-testing' in a test case? (C#/nUnit) -


i'm working on test cases @ moment, , i'm regularly finding i'm ending multiple asserts in each case. example (over-simplified , comments stripped brevity):

[test] public void testnamepropertycorrectlysetoninstantiation() {   myclass myinstance = new myclass("test name");   assert.areequal("test name", myinstance.name); } 

this looks acceptable in principle, point of test verify when the class instantiated given name, name property set correctly, fails if goes wrong on instantiation, before gets assertion.

i refactored this:

[test] public void testnamepropertycorrectlysetoninstantiation() {   myclass myinstance;   string namepropertyvalue;    assert.doesnotthrow(() => myinstance = new myclass("test name"));   assert.doesnotthrow(() => namepropertyvalue = myinstance.name);   assert.areequal("test name", namepropertyvalue); } 

but of course, i'm testing 3 things here; in test, i'm not interested in testing whether or not instance of myclass instantiated, or name property read successfully, these tested in case. how can test last assertion without asserting other 2 first, given it's not possible test if first 2 fail?

just have other tests check exception is thrown if initialize in invalid way. first form fine @ point, imo.

personally i'd avoid getting hung on dogma of "one assert per test". try test 1 logical path through code, fine granularity makes practical sense.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -