Use annotations to set test parameters on Java -
i'll start saying 1 of things don't understand java annotations.
i come python background tend think of annotations modifiers on methods potentially (like decorators on python).
now, have problem that, in terms of syntax, nice if solved using annotations, don't know if can done.
basically, have different versions of api: v1, v2, v3, v4. , have junit4 tests each of versions.
apiv1test.java
@test public void featurex() { connection conn = new apiconnection("v1"); response = conn.request("x", "foo")'; assertequals("bar", response); } @test public void featurey() { connection conn = new apiconnection("v1"); response = conn.request("y", "foo_y")'; assertequals("bar_y", response); }
apiv2test.java
@test public void featurex() { connection conn = new apiconnection("v2"); response = conn.request("x", "foo")'; assertequals("bar", response); } @test public void featurez() { connection conn = new apiconnection("v2"); response = conn.request("z", "foo_y")'; assertequals("bar_z", response); }
the thing different versions have different features; v1 may have features x , y, v2 may have features x , z. , on, each feature available in subset of apis.
so, leads lot of code repetition. have 1 file different features , have mechanism saying "run test these versions", like:
@test @versions("v1", "v2") public void featurex(string version) { connection conn = new apiconnection(version); response = conn.request("x", "foo")'; assertequals("bar", response); } @test @versions("v1") public void featurey(string version) { connection conn = new apiconnection(version); response = conn.request("y", "foo_y")'; assertequals("bar_y", response); } @test @versions("v2") public void featurez(string version) { connection conn = new apiconnection(version); response = conn.request("z", "foo_z")'; assertequals("bar_z", response); }
is possible?
since junit 4, can that. categories
.
// in version1.java. public interface version1 { /* category marker */ } // in version2.java. public interface version2 { /* category marker */ } // part of apiv1test.java @test @category({version1.class, version2.class}) public void featurex( string version) { connection conn = new apiconnection(version); response = conn.request("x", "foo")'; assertequals("bar", response); } // part of apiv2test.java @test @category(version2.class) public void featurez(string version) { connection conn = new apiconnection(version); response = conn.request("z", "foo_z")'; assertequals("bar_z", response); } //part of v1testsuite.java, chosen through ant target or maven execution. @runwith(categories.class) @includecategory(version1.class) @suiteclasses(apiv1test.class, apiv2test.class) // * public class v1testsuite {}
you can split test methods few or many classes want. edit line marked star. then, run v1testsuite
hand, or use appropriate ways have run ant target or maven execution test step.
ant
<target name="text-version1" depends="compile-tests"> <junit printsummary="yes" haltonfailure="yes"> <classpath> <pathelement location="${build.tests}"/> <pathelement path="${java.class.path}"/> </classpath> <formatter type="plain"/> <test name="my.test.v1testsuite"/> </junit> </target>
Comments
Post a Comment