c# - TDD nUnit multiple asserts for one method -
using tdd first time in life today. using nunit.
i have 1 method, can insert multiple different inputs , check if result works.
i read multiple asserts in 1 test not problem, , don't want write new test each input.
example multiple asserts:
[testfixture] public class testclass { public program test; [setup] public void init() { test = new program(); } [test] public void parse_simplevalues_calculated() { assert.areequal(25, test.parsecalculationstring("5*5")); assert.areequal(125, test.parsecalculationstring("5*5*5")); assert.areequal(10, test.parsecalculationstring("5+5")); assert.areequal(15, test.parsecalculationstring("5+5+5")); assert.areequal(50, test.parsecalculationstring("5*5+5*5")); assert.areequal(3, test.parsecalculationstring("5-1*2")); assert.areequal(7, test.parsecalculationstring("7+1-1")); } }
but when fails hard read assert failed, mean if have them lot, have go through , find right assert.
is there elegant way show input did set if assert fails, instead of result , expected result?
thank you.
i mean if have them lot, have go through all.
no don't - @ stack trace. if you're running tests within ide, find that's enough work out line failed.
that said, there is (significantly) better way - parameterized tests testcaseattribute
. example:
[test] [testcase("5*5", 25)] [testcase("5*5*5", 125)] [testcase("5+5", 10)] // etc public void parse_simplevalues_calculated(string input, int expectedoutput) { assert.areequal(expectedoutput, test.parsecalculationstring(input)); }
now unit test runner show each test case separately, , you'll able see 1 fails. additionally, run all of tests, if 1 fails - don't end fixing 1 find next 1 failed unexpectedly.
there's testcasesourceattribute
cases want specify collection of inputs separately - e.g. use across multiple tests.