SoftAssert的assertAll产生的断言结果,在TestNG的报告中重复记录

 原引用assertion的是SoftAssert的对象,结果如下:

解决办法:

自己重写Assertion

package pro.assertion;

import java.util.Map;

import org.testng.asserts.Assertion;
import org.testng.asserts.IAssert;
import org.testng.collections.Maps;
/**
 * @author 这个博主狠弱? 
 */
public class MyAssert extends Assertion {
	// LinkedHashMap to preserve the order
	private final Map<AssertionError, IAssert<?>> m_errors = Maps.newLinkedHashMap();
	private static final String DEFAULT_SOFT_ASSERT_MESSAGE = "The following asserts failed:";

	@Override
	protected void doAssert(IAssert<?> a) {
		onBeforeAssert(a);
		try {
			a.doAssert();
			onAssertSuccess(a);
		} catch (AssertionError ex) {
			onAssertFailure(a, ex);
			m_errors.put(ex, a);
		} finally {
			onAfterAssert(a);
		}
	}

	public void assertAll() {
		assertAll(null);
	}

	public void assertAll(String message) {
		if (!m_errors.isEmpty()) {
			System.out.println("msize:" + m_errors.size());
			StringBuilder sb = new StringBuilder(null == message ? DEFAULT_SOFT_ASSERT_MESSAGE : message);
			boolean first = true;
			for (AssertionError error : m_errors.keySet()) {
				if (first) {
					first = false;
				} else {
					sb.append(",");
				}
				sb.append("\n\t");
				sb.append(getErrorDetails(error));
			}
			m_errors.clear();  //只需要加入这一行就行了
			throw new AssertionError(sb.toString());
		}
	}
}

解决后报告效果

猜你喜欢

转载自blog.csdn.net/qq_40632009/article/details/82356905