断言处理给出了不同的结果" console"和"运行套件的结果"

时间:2017-08-22 10:08:14

标签: java selenium selenium-webdriver junit testng

我正在尝试使用软断言来运行测试,以便在断言失败后,测试中的所有脚本都应该运行。我创建了一个Listener类来验证结果并执行测试。但是在执行之后,这个测试应该会失败,并且会在Eclipse"运行Suite"的结果中进行验证和显示。选项卡但不在"控制台"选项卡以及报告测试中传递断言错误。如果代码中有任何问题,请告诉我..

请参阅随附的屏幕截图供您参考。

- 下面是使用try-catch进行断言处理的测试类。



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

import ErrorCollectors.ErrorCollector;


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

}




- 下面是收集错误的类---



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 {
	
	private static Map<ITestResult, List<Throwable>> verificationFailuresMap = new HashMap<ITestResult, List<Throwable>>();


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

下面是定义监听器类以验证任何测试失败或通过的代码.............

&#13;
&#13;
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 {

	public void afterInvocation(IInvokedMethod method, ITestResult result) {
		

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

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

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

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

下面是我的testng.xml文件。

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

我不确定重新发明轮子的原因是什么,但TestNG已经实现了SoftAssert。作为替代方案,您可以查看例如AssertJ库,也有SoftAssertions

您的特定问题与断言错误处理的错误方式有关。

使用原始SoftAssert,您可以在assertAll内捕获afterInvocation结果,而不是在测试中放置try / catch块,然后在发生故障时手动分配所需的测试结果

答案 1 :(得分:0)

catch抓住AssertionError引发的Assert.assertEquals。这就是打印错误消息但测试未终止并继续成功结束的原因。