TransWikia.com

How to make TestNG print detail message about the failure

Stack Overflow Asked by Gelin Luo on January 15, 2021

My testNG failed with simple message:

[TestNG] [ERROR]
Cannot instantiate class com.xyz.AbcTest

However I don’t know why it cannot instantiate the class. How can I make TestNG print out the detail information or the error stack in this case?

2 Answers

Hi You can enable testng logger by using log4testng.properties file in the src/main/resources .

https://github.com/qmetry/qtm-QAF-MAVEN-example/blob/master/resources/log4testng.properties

Also use verbose="10" in the testng xml file.

Answered by Tanuj Vishnoi on January 15, 2021

Assuming you are using the maven-surefire-plugin for execution of the TestNG test . For enabling the full stack trace on the console we need to set trimStackTrace to false

trimStackTrace : Whether to trim the stack trace in the reports to just the lines within the test, or show the full trace. Default value is: true. User property is: trimStackTrace.*

   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
        <configuration>
            <trimStackTrace>false</trimStackTrace>
            <suiteXmlFiles>
                <suiteXmlFile>testng.xml</suiteXmlFile>
            </suiteXmlFiles>
        </configuration>

    </plugin>

With above setting for maven project you should be able to see full stack trace.

Below are remote console outputs with trimStackTrace :

For : trimStackTrace:true

[ERROR] com.interfaces.TestClass.testing[Edge, org.testng.TestRunner@28467629](2)  Time elapsed: 0.008 s  <<< FAILURE!
java.lang.NullPointerException
        at com.interfaces.TestClass.testing(TestClass.java:32)

[ERROR] com.interfaces.TestClass.testing[Chrome, org.testng.TestRunner@28467629](2)  Time elapsed: 0.009 s  <<< FAILURE!
java.lang.NullPointerException
        at com.interfaces.TestClass.testing(TestClass.java:32)

[INFO]

---------------------------------------------------

For trimStackTrace :false

[ERROR] Tests run: 2, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 0.477 s <<< FAILURE! - in TestSuite
[ERROR] com.interfaces.TestClass.testing[Edge, org.testng.TestRunner@2dc7f489](2)  Time elapsed: 0.014 s  <<< FAILURE!
java.lang.NullPointerException
        at com.interfaces.TestClass.testing(TestClass.java:32)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
        at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599)
        at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
        at org.testng.internal.TestMethodWithDataProviderMethodWorker.call(TestMethodWithDataProviderMethodWorker.java:77)
        at org.testng.internal.TestMethodWithDataProviderMethodWorker.call(TestMethodWithDataProviderMethodWorker.java:15)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)

Answered by Rahul L on January 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP