2017-08-22 7 views
0

ここでソフトアサーションを使用してテストを実行しようとしているため、アサーションが失敗した後もテスト内のすべてのスクリプトを実行する必要があります。結果を検証してテストを実行するListenerクラスを作成しました。しかし、実行後、このテストは失敗するはずで、検証されてEclipseで表示されます。「スイートの実行結果」タブではなく、「コンソール」タブにもレポートテストでもアサーションエラーが発生します。コード内に何か問題がある場合はお知らせください。"コンソール"と "実行スイートの結果"で異なる結果を与えるアサーションの処理

参考までに添付のスクリーンショットをご覧ください。

- ベローは、アサーションの処理がtry-catchを使って行われたテストクラスです。

import org.testng.Assert; 
 
import org.testng.annotations.Test; 
 

 
import ErrorCollectors.ErrorCollector; 
 

 

 
public class TestValidateTitles { 
 
\t 
 
\t 
 
\t @Test 
 
\t public void ValidteTitle(){ 
 
\t \t try{ 
 
\t \t System.out.println("Beginning"); 
 
\t \t 
 
\t \t String actual_title = "Gmail.com"; 
 
\t \t String expected_title = "Yahoo.com"; 
 
\t \t 
 
\t \t Assert.assertEquals(expected_title, actual_title); 
 
\t \t }catch(Throwable t){ 
 
\t \t \t 
 
\t \t \t System.out.println("Error Occurred"); 
 
\t \t \t ErrorCollector.addVerificationFailure(t); 
 
\t \t \t 
 
\t \t } 
 
\t \t System.out.println("Ending"); 
 
\t \t 
 
\t } 
 

 
}

- 以下

以下

package ErrorCollectors; 
 

 
import java.util.ArrayList; 
 
import java.util.HashMap; 
 
import java.util.List; 
 
import java.util.Map; 
 

 

 
import org.testng.ITestResult; 
 
import org.testng.Reporter; 
 

 
public class ErrorCollector { 
 
\t 
 
\t private static Map<ITestResult, List<Throwable>> verificationFailuresMap = new HashMap<ITestResult, List<Throwable>>(); 
 

 

 
\t public static List<Throwable> getVerificationFailures() { 
 
\t \t List<Throwable> verificationFailures = verificationFailuresMap.get(Reporter.getCurrentTestResult()); 
 
\t \t return verificationFailures == null ? new ArrayList<Throwable>() : verificationFailures; 
 
\t } 
 
\t 
 
\t public static void addVerificationFailure(Throwable e) { 
 
\t \t List<Throwable> verificationFailures = getVerificationFailures(); 
 
\t \t verificationFailuresMap.put(Reporter.getCurrentTestResult(), verificationFailures); 
 
\t \t verificationFailures.add(e); 
 
\t } 
 
\t 
 
}

を検証するリスナクラスを定義するコードである---エラーを収集するクラスでありますテストの失敗または合格.............

以下は

package ErrorCollectors; 
 

 
import java.util.List; 
 

 
import org.testng.IInvokedMethod; 
 
import org.testng.IInvokedMethodListener; 
 
import org.testng.ITestResult; 
 
import org.testng.Reporter; 
 
import org.testng.internal.Utils; 
 

 
public class TestListenerAdapter implements IInvokedMethodListener { 
 

 
\t public void afterInvocation(IInvokedMethod method, ITestResult result) { 
 
\t \t 
 

 
\t \t 
 
\t \t Reporter.setCurrentTestResult(result); 
 
\t \t 
 
\t \t if (method.isTestMethod()) { 
 

 
\t \t \t List<Throwable> verificationFailures = ErrorCollector.getVerificationFailures(); 
 

 
\t \t \t //if there are verification failures... 
 
\t \t \t if (verificationFailures.size() > 0) { 
 
\t \t \t \t 
 
\t \t \t \t //set the test to failed 
 
\t \t \t \t result.setStatus(ITestResult.FAILURE); 
 

 
\t \t \t \t //if there is an assertion failure add it to verificationFailures 
 
\t \t \t \t if (result.getThrowable() != null) { 
 
\t \t \t \t \t verificationFailures.add(result.getThrowable()); 
 
\t \t \t \t } 
 
\t \t \t \t 
 
\t \t \t \t int size = verificationFailures.size(); 
 
\t \t \t \t //if there's only one failure just set that 
 
\t \t \t \t if (size == 1) { 
 
\t \t \t \t \t result.setThrowable(verificationFailures.get(0)); 
 
\t \t \t \t } else { 
 
\t \t \t \t \t //create a failure message with all failures and stack traces (except last failure) 
 
\t \t \t \t \t StringBuffer failureMessage = new StringBuffer("Multiple failures (").append(size).append("):\n\n"); 
 
\t \t \t \t \t for (int i = 0; i < size-1; i++) { 
 
\t \t \t \t \t \t failureMessage.append("Failure ").append(i+1).append(" of ").append(size).append(":\n"); 
 
\t \t \t \t \t \t Throwable t = verificationFailures.get(i); 
 
\t \t \t \t \t \t String fullStackTrace = Utils.stackTrace(t, false)[1]; 
 
\t \t \t \t \t \t failureMessage.append(fullStackTrace).append("\n\n"); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t 
 
\t \t \t \t \t //final failure 
 
\t \t \t \t \t Throwable last = verificationFailures.get(size-1); 
 
\t \t \t \t \t failureMessage.append("Failure ").append(size).append(" of ").append(size).append(":\n"); 
 
\t \t \t \t \t failureMessage.append(last.toString()); 
 
\t \t \t \t \t 
 
\t \t \t \t \t //set merged throwable 
 
\t \t \t \t \t Throwable merged = new Throwable(failureMessage.toString()); 
 
\t \t \t \t \t merged.setStackTrace(last.getStackTrace()); 
 
\t \t \t \t \t 
 
\t \t \t \t \t result.setThrowable(merged); 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t } 
 
\t 
 
\t } 
 
\t 
 
\t public void beforeInvocation(IInvokedMethod arg0, ITestResult arg1) {} 
 
\t 
 
}

私のtestng.xmlファイルです。

<?xml version="1.0" encoding="UTF-8"?> 
 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
 
<suite name="Suite"> 
 
<listeners> 
 
    <listener class-name="ErrorCollectors.TestListenerAdapter"/> 
 
</listeners> 
 
    <test name="Test"> 
 
    <classes> 
 
     <class name="com.collections.java.TestValidateTitles"/> 
 
    </classes> 
 
    </test> 
 
</suite>

enter image description here enter image description here

答えて

0

catchキャッチAssertionErrorAssert.assertEqualsによってスローされています。そのため、エラーメッセージが表示されますが、テストは終了せず、正常終了します。

1

私は車輪を再発明する理由は何だったのか分かりませんが、TestNGにはすでにSoftAssertの実装があります。これに代わる方法として、 AssertJライブラリには、SoftAssertionsも含まれています。

特に問題はアサーションエラー処理の方法が間違っていることです。元SoftAssert

あなたの代わりにテストでtry/catchブロックを置くことで、afterInvocationassertAll結果をキャッチして、手動で障害が発生した場合に必要なテスト結果を割り当てることができます。

関連する問題