我得到了你的問題,這是我在我所有的測試用例都盡現 -
在您的測試情況下,如果你想檢查是否2個字符串相等 -
你可能會使用這個代碼 -
Assert.assertTrue(value1.equals(value2));
如果這些值不相等,則會生成AssertionError。
相反,你可以改變你這樣的代碼 -
String testCaseStatus = "";
if(value1.equals(value2)){
testCaseStatus = "success";
}
else{
testCaseStatus = "fail";
}
現在這個結果在您的Excel工作表通過將testCaseStatus
你的代碼,添加一行到您的Excel您已使用Apache POI實現存儲。如果您實施try catch塊,您還可以處理「錯誤」的條件。
例如,您可以捕獲一些例外情況並將錯誤狀態添加到您的Excel工作表中。
答案編輯部分 -
我只是想出瞭如何使用的TestResult類 - 這是一些示例代碼我張貼 -
這就是所謂ExampleTest測試用例類 -
public class ExampleTest implements junit.framework.Test {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void test(TestResult result) {
try{
Assert.assertEquals("hari", "");
}catch(AssertionFailedError e){
result.addFailure(this, e);
}catch(AssertionError e){
result.addError(this, e);
}
}
@Override
public int countTestCases() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void run(TestResult result) {
test(result);
}
}
我請從該上面的測試情況下代碼 -
public class Test {
public static void main(String[] args) {
TestResult result = new TestResult();
TestSuite suite = new TestSuite();
suite.addTest(new ExampleTest());
suite.run(result);
System.out.println(result.errorCount());
}
}
則只需將它們添加到這套房調用許多測試用例,然後使用的TestResult類failures()和錯誤()方法得到的整個結果。
您可以從here瞭解更多。
您是否使用任何斷言條件來驗證您的測試?張貼一些代碼,以便社區可以建議任何更改以幫助您。不要將整個代碼僅發佈相關代碼。對於此問題,發佈代碼,通過該代碼確定您的測試是否失敗或通過。 – 2012-07-24 16:54:51
@Hari:嗯,他們是簡單的Selenium JUnit代碼,如driver.get(url);發送密鑰等是的,我一般使用斷言,但我認爲它只會返回true/false。我在談論的JUnit報告有3個值 - 成功/失敗/錯誤。我想抓住所有三個爲我的excel報告。有沒有辦法做到這一點? – 2012-07-25 05:03:47
就像這樣 - http://www.junit.org/junit/javadoc/3.8.1/junit/framework/TestCase.html可以收集結果。但我無法正確使用它。 – 2012-07-25 05:15:45