c# - How to structure my unit test? -
i new unit test , wondering how start testing. application working on, not have unit test. winform application , interested test data layer of application.
here example.
public interface icalculatesomething { someoutout1 calculatesomething1(someinput1 input1); someoutout2 calculateosomething2(someinput2 input2); } public class calculatesomething : icalculatesomething { someoutout1 icalculatesomething.calculatesomething1(someinput1 input1) { someoutout1.prop1 = calculatefrominput1(input1.prop1, input1.prop2); someoutout1.prop3 = calculatefrominput2(input1.prop3, input1.prop4); return someoutout1; } someoutout2 icalculatesomething.calculateosomething2(someinput2 input2) { someoutout2.prop1 = calculatefrominput1(input2.prop1, input2.prop2); someoutout2.prop3 = calculatefrominput2(input2.prop3, input2.prop4); return someoutout2; } }
i test these 2 methods in calculatesomething. methods implementation long , complicated. how should structure test?
i don't see reason not using straight-forward unit test implementation. i'd start basic test method:
[testmethod] public void calculatesomething1_fooinput { var input = new someinput1("foo"); var expected = new someoutput1(...); var calc = new calculatesomething(...); var actual = calc.calculatesomething1(input); assert.areequal(expected.prop1, actual.prop1); assert.areequal(expected.prop2, actual.prop2); assert.areequal(expected.prop3, actual.prop3); }
and then, add calculatesomething1_barinput
, calculatesomething2_fooinput
, factor out common code helper methods:
[testmethod] public void calculatesomething1_fooinput { var input = new someinput1("foo"); var expected = new someoutput1(...); var actual = createtestcalculatesomething().calculatesomething1(input); assertsomeoutput1equality(expected, actual); }
Comments
Post a Comment