गुर्दा का समाधान अच्छा है।
@RunWith(FilteredRunner.class)
public class MyTest {
:
import org.junit.runner.manipulation.Filter;
import org.junit.runner.Description;
public final class AntCLFilter extends Filter {
private static final String TEST_CASES = "tests";
private static final String ANT_PROPERTY = "${tests}";
private static final String DELIMITER = "\\,";
private String[] testCaseNames;
public AntCLFilter() {
super();
if (hasTestCases()) testCaseNames = getTestCaseNames();
}
public String describe() {
return "Filters out all tests not explicitly named in a comma-delimited list in the system property 'tests'.";
}
public boolean shouldRun(Description d) {
String displayName = d.getDisplayName();
// cut off the method name:
String testName = displayName.substring(0, displayName.indexOf('('));
if (testCaseNames == null) return true;
for (int i = 0; i < testCaseNames.length; i++)
if (testName.equals(testCaseNames[i]))
return true;
return false;
}
/**
* Check to see if the test cases property is set. Ignores Ant's
* default setting for the property (or null to be on the safe side).
**/
public static boolean hasTestCases() {
return
System.getProperty(TEST_CASES) == null ||
System.getProperty(TEST_CASES).equals(ANT_PROPERTY) ?
false : true;
}
/**
* Create a List of String names of test cases specified in the
* JVM property in comma-separated format.
*
* @return a List of String test case names
*
* @throws NullPointerException if the TEST_CASES property
* isn't set
**/
private static String[] getTestCaseNames() {
if (System.getProperty(TEST_CASES) == null) {
throw new NullPointerException("Test case property is not set");
}
String testCases = System.getProperty(TEST_CASES);
String[] cases = testCases.split(DELIMITER);
return cases;
}
}
import org.junit.internal.runners.*;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.NoTestsRemainException;
public class FilteredRunner extends TestClassRunner {
public FilteredRunner(Class<?> clazz) throws InitializationError {
super(clazz);
Filter f = new AntCLFilter();
try {
f.apply(this);
} catch (NoTestsRemainException ex) {
throw new RuntimeException(ex);
}
}
}
तो मैं के साथ अपने परीक्षण वर्ग एनोटेट: यहाँ है कि मैं क्या (यह ल्यूक Francl का नुस्खा है, जो मैं पहले लिंक का एक मिश्रण है, और कुछ अन्य सामान मैं नेट पर देखा था) कर समाप्त हो गया है
और मेरे चींटी buildfile में निम्नलिखित डाल:
<target name="runtest"
description="Runs the test you specify on the command line with -Dtest="
depends="compile, ensure-test-name">
<junit printsummary="withOutAndErr" fork="yes">
<sysproperty key="tests" value="${tests}" />
<classpath refid="classpath" />
<formatter type="plain" usefile="false" />
<batchtest>
<fileset dir="${src}">
<include name="**/${test}.java" />
</fileset>
</batchtest>
</junit>
</target>
कुंजी लाइन वहाँ sysproperty टैग किया जा रहा है।
और के रूप में वांछित अब मैं
ant runtest -Dtest=MyTest -Dtests=testFoo,testBar
चला सकते हैं। यह जुनीट 4.1 के साथ काम करता है --- 4.4 में, जुनीट 4 क्लासरुनर से सबक्लास, और 4.5 और बाद में, ब्लॉकजुनिट 4 क्लासरुनर से सबक्लास।
ठीक है, कि और अधिक सुंदर है मेरे समाधान से :) – guerda
मैं कुछ दिनों के लिए इस (या कम से कम समान) समस्या से जूझ रहा हूं और एक चीज है जो मुझे परेशान करती रहती है। क्या होगा यदि आप अपने फ़िल्टर किए गए कुनर को Powermock के साथ उपयोग करना चाहते हैं, जिसके लिए इसकी अपनी @RunWith (PowermockRunner.class) एनोटेशन की भी आवश्यकता है? –
महान उत्तर, लेकिन अब पुराना है। मुझे लगता है कि BlockJUnit4ClassRunner को TestClassRunner – Illidanek