java - Manually instantiating TestCase and add it to JUnit doesn't work -
i need reuse testcase, i've created private field "role" in test case:
public class atest extends testcase { private string role; public void setrole(string username) { role = username; } @test public void method1(){ system.out.println("role:"+role); }
now want create suite several testcases using same "atest" class:
public class alltests { public static test suite() { testsuite suite = new testsuite(alltests.class.getname()); //case 1: works fine suite.addtestsuite(atest.class); //case 2: doesn't work fname null atest atest = new atest(); atest.setrole("admin"); suite.addtest(atest); //case 3: works ugly atest atest2 = new atest(); atest2.setname(atest2.getclass().getmethods()[1].getname()); atest2.setrole("anotheruser"); suite.addtest(atest2); homecoming suite; } }
the case 1 obvious way add together test case doesn't enable set private field. case 2 want doesn't work since property fname of test null (i saw using debug perspective in eclipse). lastly case case 3 seems work don't want utilize approach.
is there more elegant , simple way reuse same test case ? thanks
you not need suite this. can utilize parameterized class this
@runwith(parameterized.class) public class atest { //with junit4, no need extend @parameters public static collection<object[]> data() { homecoming arrays.aslist(new object[][] { { "firstrole" }, { "secondrole" }, { "thirdrole" } }); } private final string role; public atest(string role) { this.role = role; } @test public void test() { system.out.println("role:"+role); } }
see junit wiki more info on parameterized tests.
java eclipse junit testcase test-suite
No comments:
Post a Comment