c# - Unit Testing - best practice -
i use visual studio 2010 professional mstest framework perform unit tests. have nasty production code test. first issue problematic code in constructor. show examaple :
class classtotest { public someenum upperborder; public someenum lowerborder; public int var1; private readonly someenum2 _ethnicgroup; private readonly double _age; public int datastart; public int dataend; public double[] darkreddarkyellow; public double[] darkyellowgreen; public double[] greenlightyellow; public double[] lightyellowlightred; public classtotest(someenum upperborder, someenum lowerborder, int var1, someenum2 ethnicgroup, int age) { upperborder = upperborder; lowerborder = lowerborder; bscanindex = bscanindex; _ethnicgroup = ethnicgroup; _age = age; datastart = 0; dataend = 0; darkreddarkyellow = null; darkyellowgreen = null; greenlightyellow = null; lightyellowlightred = null; } }
my question :
- write 1 test assert statement each variable? or write couple of tests , in each test check 1 variable @ once? example :
[testmethod()] public void classtotest_constructor_upperborder_ptest() { //act var ob = new classtotest(someenum.bor1, someenum.bor2,10,someenum2.asian,10); //assert assert.isnotnull(object); assert.areequal(ob.upperborder,someenum.bor1); }
- should check if constructor assign parameters private field? or if there property returns private field performs action triggers event, log action etc.
i cannot find information it. advice precious.
i have written 1 test many asserts. know people argue against it, think testing 1 method , validating relevant postconditions method ok in 1 test. otherwise you'll have tons of test methods.
private fields not tested unit tests. unit test should preferrably test externally visible behaviour , state.
i think rule strive full code coverage possible unit tests. if there error in constructor , assignments field, should caught in other more high-level tests if have proper coverage. reason see write tests private parts of class if hard trigger scenarios, such error handling routines otherwise. when dealing threads there can reasons acquire private locks before executing test, emulate specific scheduling scenarios.
Comments
Post a Comment