में सभी परीक्षण मामलों के लिए एक जुनीट @ नियम लागू करने के लिए मैं परीक्षण सूट चलाने के लिए जुनीट 4.10 का उपयोग कर रहा हूं, और मैंने How to Re-run failed JUnit tests immediately? पोस्ट में मैथ्यू फ़ारवेल के अद्भुत नोट्स के बाद "पुनः प्रयास विफल परीक्षण" नियम लागू किया है। मैं निम्नलिखित कोड के साथ एक वर्ग "RetryTestRule" बनाया:एक सूट
public class RetryTestRule implements TestRule {
private final int retryCount;
public RetryTestRule(int retryCount) {
this.retryCount = retryCount;
}
@Override
public Statement apply(Statement base, Description description) {
return statement(base, description);
}
private Statement statement(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Throwable caughtThrowable = null;
// retry logic
for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
return;
} catch (Throwable t) {
caughtThrowable = t;
System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed");
}
}
System.err.println(description.getDisplayName() + ": Giving up after " + retryCount
+ " failures");
throw caughtThrowable;
}
};
}
}
जब एक परीक्षण का मामला पूरी तरह से काम करता है यह अंदर एक नियम के रूप में इस का उपयोग करते हुए, लेकिन यह एक के हर परीक्षण मामले में @Rule संकेतन का उपयोग करने के लिए इष्टतम नहीं लगता है
@RunWith(Suite.class)
@SuiteClasses({
UserRegistrationTest.class,
WebLoginTest.class
})
public class UserSuite {
@ClassRule
public static RetryTestRule retry = new RetryTestRule(2);
}
समस्या यह अपेक्षा के अनुरूप काम नहीं करता है: सुइट परिभाषा में एक भी अंकन के बजाय सूट, इसलिए थोड़ा की जाँच के बाद मैं अपने सुइट कक्षा में नए @ClassRule अंकन की कोशिश की असफल परीक्षण पुन: प्रयास नहीं किया जा रहा है । क्या किसी ने कोशिश की है और समाधान जानता है? मदद बहुत सराहना की है!
एक डुप्लिकेट हो सकता है: http://stackoverflow.com/questions/7639353/how-to-define-junit-method-rule-in-a-suite – pholser
आपको यह बिल्कुल चिंता नहीं है कि आपकी इकाई परीक्षण यादृच्छिक रूप से विफल? – Tobb