How to set stream name in TestNG

I run my tests in parallel. TestNG, by default, sets the thread name to TestNG This makes it difficult to view log files and distinguish between messages.

TestNG javadocs does not help.

Can I set the stream name or stream identifier?

+4
source share
2 answers

I set the stream name in my tests. For example, see how I did it IN THIS PROJECT . Basically, all of my Log4j messages (as well as testNG reporter messages) show the thread ID in them.

Here is a basic example that makes it easy to show the stream identifier in the standard release. You can also put a stream in the html standard using the Reporter class. Unfortunately, you cannot put threadid in the test name in an HTML report unless you wrote a custom reporter (which I did in my link to my project above):

 import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class ParallelMethodTest { @BeforeMethod public void beforeMethod() { long id = Thread.currentThread().getId(); System.out.println("Before test-method. Thread id is: " + id); } @Test public void testMethodsOne() { long id = Thread.currentThread().getId(); System.out.println("Simple test-method One. Thread id is: " + id); } @Test public void testMethodsTwo() { long id = Thread.currentThread().getId(); System.out.println("Simple test-method Two. Thread id is: " + id); } @AfterMethod public void afterMethod() { long id = Thread.currentThread().getId(); System.out.println("After test-method. Thread id is: " + id); } } 
+3
source

You can use the setName () function to provide a custom name for your stream.

The following is an example in JAVA to set the name to Thread:

 Thread.currentThread().setName("Thread 1"); 

Above the line, your current thread name will be set to "Thread 1".

+1
source

Source: https://habr.com/ru/post/1480058/


All Articles