如何使TestNG报告显示软断言失败的行

时间:2016-05-14 18:25:37

标签: java selenium selenium-webdriver testng testng-eclipse

我正在使用Selenium,Java和TestNG来编写测试。有时我在单元测试中使用许多软断言,当它们失败时,TestNG报告器不会显示它们发生的代码行。有没有办法让它显示出来?实际上,当我点击Failure Exception上的报告时,我需要s_assert.assertAll();,但我需要接受特定的行,例如:s_assert.assertEquals(Alert_text, "Hi.. is alert message!", "Alert Is InCorrect");

3 个答案:

答案 0 :(得分:2)

自定义Soft Assertion的下面实现(我将其命名为Verifier)应该可以满足您的要求。

import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
import org.testng.asserts.IAssert;
import org.testng.collections.Maps;

import java.util.Arrays;
import java.util.Map;

public class SoftAssertExample {
    private Verifier verifier = new Verifier();

    @Test
    public void testMethod() {
        verifier.assertEquals(false, true);
        verifier.assertTrue(true);
        verifier.assertAll();
    }

    /**
     * A simple soft assertion mechanism that also captures the stacktrace to help pin point the source
     * of failure.
     */
    public static class Verifier extends Assertion {
        private final Map<AssertionError, IAssert<?>> m_errors = Maps.newLinkedHashMap();

        @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() {
            if (! m_errors.isEmpty()) {
                StringBuilder sb = new StringBuilder("The following asserts failed:");
                boolean first = true;
                for (Map.Entry<AssertionError, IAssert<?>> ae : m_errors.entrySet()) {
                    if (first) {
                        first = false;
                    } else {
                        sb.append(",");
                    }
                    sb.append("\n\t");
                    sb.append(ae.getKey().getMessage());
                    sb.append("\nStack Trace :");
                    sb.append(Arrays.toString(ae.getKey().getStackTrace()).replaceAll(",", "\n"));
                }
                throw new AssertionError(sb.toString());
            }
        }
    }
}

答案 1 :(得分:1)

这是一个延迟的答案,但是由于我遇到了同样的问题,我认为我会分享我的解决方案。

以下内容打印出软断言行失败的位置。它显示了正在使用的类的方法内部的线路故障,如下所示::

1) 
Error: Custom error message goes here
[Class -> path.to.class.shows.here.ExampleClassName]
[Method -> exampleMethod]
[Line -> 342]

此解决方案的示例代码:

  public void assertIsTrueSoftly(String errorMsg, boolean condition) {
    String trace = null;

    if (!condition) {
      Throwable throwable = new Throwable();
      trace =
              String.format(
                      "[Class -> %s]%n[Method -> %s]%n[Line -> %d]",
                      throwable.getStackTrace()[1].getClassName(),
                      throwable.getStackTrace()[1].getMethodName(),
                      throwable.getStackTrace()[1].getLineNumber());
    }

    soft.assertThat(condition).withFailMessage(format("%nError: %s%n%s", errorMsg, trace)).isTrue();
  }

答案 2 :(得分:0)

示例代码: s_assert.assertEquals(Alert_text,“Hi .. is alert message!”,“Alert is InCorrect1”); s_assert.assertEquals(Alert_text,“Hi .. is alert message!”,“Alert is InCorrect2”); s_assert.assertEquals(Alert_text,“Hi .. is alert message!”,“Alert is InCorrect3”); s_assert.assertEquals(Alert_text,“Hi .. is alert message!”,“Alert is InCorrect4”); s_assert.assertall();

执行失败后,它指向(s_assert.assertall();)行,它显示消息软件断言失败的平均时间.... 警报是InCorrect2 警报是InCorrect3

请检查输出。