Overview– TestNG is widely used and most preferred in test automation, It contains several features that are beneficial for Unit testing, regression, and Integration testing.

In this article we will see out report generated by Test-NG can be copied in every run.
Test-NG generate its execution report in test-output folder, this folder contains various logs such as-

1-Emailable report
2-index.hlml(contains various section like-Failed, Passed, Chronological view order(of test execution) etc
3-Test-NG failed xml etc
4-Jnformation of custom Asserts and screenshot link(if configured).

However, if you run multiple test runs the output folder files are overwritten and reports of previous test runs are lost, to handle this we can take backup or copy of test-output folder using below 2 methods.

Method 1-

In this method copy of output folder is being taken to another location by calling it before/after suite execution.

Step 1- Write below code

public void Take_TestOutput_Backup() throws IOException

String Foldername = new SimpleDateFormat("HH_mm_ss").format(new Date());
File srcFolder = new File("./test-output");
File destFolder = new File("D:\\"+"Execution_Archive_"+Foldername);

//make sure source exists
System.out.println("Directory does not exist at mentioned location.");
//just exit

catch(IOException e){
//error, just exit
public static void copy_Folder(File src, File dest)
throws IOException{

//if directory not exists, create it
System.out.println("Directory copied from "
+ src + " to " + dest);
//list all the directory contents
String files[] = src.list();

for (String file : files)
//construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
//recursive copy

//if file, then copy it.
//Use bytes stream to support all file types
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);

byte[] buffer = new byte[1024];

int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);

System.out.println("File copied from " + src + " to " + dest);

Step 2

In this step call Take_TestOutput_Backup() method either at the start of the test run or at the end of the run, the best- recommended way is to call is in @beforesuite annotation or @aftersuite annotation.

Copy_Folder() method is being called inside Take_Report_Backup() such that all folders and their containing files can be copied at the designated location, in above code the location is hardcoded same can be updated as per local machine logical drive name.

If in case same is being implemented in @aftersuite annotation then eclipse should be configured for autorefresh as test-output sometimes it required a refresh for getting latest execution summary.

Every time Take_TestOutput_Backup method is being called it creates a folder with timestamping(execution time) and copy all data from test-output folder their.

Method 2-

Another way is to use below code and set output folder path before every test run. Best suggestion is to use time-stamped folder as output folder, similar to what we have used in method 1.

public void setup(ITestContext ctx) {
TestRunner runner = (TestRunner) ctx;

Summary– Test execution report are most critical for every test run therefore if a user has to manually copy reports then manual errors can happen and that can result in to rework or sometimes data(execution result overwrites) loss, To handle such situation above code is effective.