java - JUnit - Looping test cases and comparing the results -
java - JUnit - Looping test cases and comparing the results -
i learning junit , have test method multiple times. have test method based on 2 parameters (environment , case#). working on 2 environments have check if same case# yields same results between different environments. test case:
public class appstarttest { /** * test method {@link archive.appstart#beginoper(java.lang.string)}. */ list<string> actualsections = new arraylist<string>(); list<string> environments = new arraylist<string>(); list<string> cases = new arraylist<string>(); @before public void preparetest() { environments.add("env1"); environments.add("env2"); cases.add("case1"); //cases.add("case2"); cases.add("case3"); cases.add("case32"); cases.add("case4"); cases.add("emp3"); } @test public void testbeginoper() { (string casestr : cases) { @suppresswarnings("unchecked") map<string, integer> maplist[] = new hashmap[2]; int = 0; (string env : environments) { system.out.println("starting " + env + "-" + casestr); appstart = new appstart(); maplist[i] = a.beginoper(env + "-" + casestr, true); printmap(maplist[i++]); } //using assert in method compareresults(maplist[0], maplist[1], casestr); } } }
the result yields single test case, requiring results as:
testbeginoper[0] testbeginoper[1] testbeginoper[2] .....
i tried using parameterized test cases, test method executed independently. have compare results between 2 environments , need method @test homecoming value (method should void) , assert. please advise.
the easiest solution can think of not have testbeginoper()
annotated @test
@ standard, non-test, value returning, parameter accepting function. have 1 @test
annotated function runs various versions of testbeginoper()
, collects results , compares them. this:
private map<string, integer> testbeginoper(string environment, string case) { // actual tests using given parameters "environment" , "case" } @test public void runtests() { list<map<string, integer>> results = new arraylist<>(); for(string environment : environments) { for(string case : cases) { results.add(testbeginoper(environment, case)); } } // compare results }
there alternatives of course, 1 beingness write own testrunner, far easiest solution.
java junit parameterized-unit-test
Comments
Post a Comment