Test Automation-Easy steps to create test-output folder Backup in TestNG automatically

automation testing
Test Automation

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.

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Test Automation
3 Easy steps for Running selenium scripts in Sauce Labs

Hello Readers, Its been a long time publishing any post, I have done a small execution of selenium script in a remotely placed device on saucelabs. Below piece of code will help you in doing the same. There are 3 simple steps to connect and run your automation script in …

Test Automation
Test Automation some useful selenium hacks

Selenium Hacks- In this post I will be sharing some quick tips on selenium,Below are some of the code snippets that I mostly used when I am creating java test cases using selenium webdriver. I hope the same will help you save some time in your test script creation. These …

Test Automation
IT Automation-Integration of Selenium and Sniffing tool (HttpWatch)

HTTP Watch and selenium integration- Process automation reduces the time and increases accuracy as well gives ample time to invest energy in other areas of the project. In this blog post, I will be talking about IT process automation, I will be doing an integration of sniffing tool and test …