Java 调用 Ant
public static void exeBuildFile(String build, int level) throws Exception {
File buildFile = new File(build);
Project p = new Project();
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(level);
p.addBuildListener(consoleLogger);
try {
p.setCoreLoader(ExecuteAntFile.class.getClassLoader()
.getSystemClassLoader());
p.fireBuildStarted();
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
helper.configureProject(p, buildFile);
// helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
p.fireBuildFinished(null);
} catch (BuildException e) {
p.fireBuildFinished(e);
throw new Exception(e);
}
}
Junit dynamic invoke testcase for reduce test method
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.tools.ant.Project;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import com.fmr.pzn.ant.ExecuteAntFile;
@RunWith(Parameterized.class)
public class DEVTest {
private String name;
private String file;
public DEVTest(String name, String file) {
this.name = name;
this.file = file;
}
@Parameters
public static Collection<Object[]> data() {
// load the files as you want
Collection<Object[]> data = new ArrayList<Object[]>();
List<String> list = ExecuteAntFile.SearchFile(
ExecuteAntFile.getTestCasePath() + "/mytests-pzn/DEVINT",
"my.xml");
for (String str : list) {
File temp = new File(str);
data.add(new Object[] {
temp.getParentFile().getParentFile().getName(),
temp.getAbsolutePath() });
}
return data;
}
@Test
public void testFunction() throws Exception {
String andFilePath = file;
System.out.println(andFilePath);
ExecuteAntFile.exeBuildFile(andFilePath, Project.MSG_INFO);
}
}
ant invoke junit and junitreport
<target name="DEVTest" depends="compile">
<echo message="===========start to run DEV junit test===========" />
<junit printsummary="true" showoutput="true" filtertrace="true" fork="true">
<classpath refid="classpath" />
<formatter type="xml" />
<sysproperty key="my.env" value="${my.env}" />
<sysproperty key="my.testname" value="${my.testname}" />
<sysproperty key="my.project.root" value="${my.project.root}" />
<test name="com.fmr.pzn.test.DEVTest" todir="${work-dir}/${my.env}/${sync.datetime}/result" />
</junit>
<echo message="===========end to run DEV junit test ===========" />
<junitreport todir="${work-dir}/${my.env}/${sync.datetime}/report">
<fileset dir="${work-dir}/${my.env}/${sync.datetime}/result">
<include name="TEST-*.xml" />
</fileset>
<report todir="${work-dir}/${my.env}/${sync.datetime}/report" format="frames">
<param name="key1" expression="value1" />
<param name="key2" expression="value2" />
</report>
</junitreport>
</target>
posted on 2012-01-12 20:38
ゞ沉默是金ゞ 阅读(224)
评论(1) 编辑 收藏