c# - Structuring TextFixtures in nUnit -
upon unit testing of class, there need test same methods different combination of data in class. can achieved writing several textfixtures , repeating same test methods each one, below.
[textfixture] public class whenbuildinglongfoo { private foo foo; [setup] public void creating() { foo = new foo(){height = new fooheight(200)}; } [test] public void returnsvalidheight() { assert.areequal(200, foo.height); } } [textfixture] public class whenbuildingshortfoo { private foo foo; [setup] public void creating() { foo = new foo(){height=newfooheight(2); } [test] public void returnsvalidheight() { assert.areequal(2, foo.height); } }
- what best way structure such fixtures, method returnsvalidheight written once?
i came creating public static class utilities in common namespace, has method; please advise better?
something create abstract class such method , derive fixtures it?
- how reuse setup among fixtures, varying values?
use testcase
attribute run same test different input values. that'll reduce lot of duplicate code. setup
method isn't necessary in case either.
[textfixture] public class footests { [test] [testcase(200)] [testcase(2)] public void returnsvalidheight(int expectedheight) { var foo = new foo { height = new fooheight(expectedheight) }; assert.areequal(expectedheight, foo.height); } }