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
if(!srcFolder.exists())
{
System.out.println("Directory does not exist at mentioned location.");
//just exit
System.exit(0);
}
else
{

try
{
Copy_Folder(srcFolder,destFolder);
}
catch(IOException e){
e.printStackTrace();
//error, just exit
System.exit(0);
}
}
}
_
public static void copy_Folder(File src, File dest)
throws IOException{

if(src.isDirectory()){
//if directory not exists, create it
if(!dest.exists()){
dest.mkdir();
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
copyFolder(srcFile,destFile);
}

}else
{
//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);
}

in.close();
out.close();
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.

@BeforeTest
public void setup(ITestContext ctx) {
TestRunner runner = (TestRunner) ctx;
runner.setOutputDirectory("/Path/To/Store/Output");
}

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.

Its Prashant Chaudhary, I have 10+ years of experience in software test automation, performance & process automation with various commercial and open source tools. This blog shares my experience on the topic of Automation. The content published on my blog is solely mine, it does not express the views of any of my employers (current or previous).
No Comments

Leave a Reply

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

Selenium Basic
1
Step by step selenium – step 3, what is findelement method

Abstract- So far, we have learned how to launch a web browser, entering values in a text box and clicking button. In this post, we will learn about findelement method. Test automation is all about get and set, which means get from web and set into web. Talking more specifically- …

Test Automation
1
What is ITestListner interface and its Use in Selenium

There are several interfaces available in TestNG that allow you to modify TestNG’s behavior or to give test scripts additional strength. These interfaces are broadly called “TestNG Listeners”. Here are a few listeners which are commonly used: 1- IAnnotationTransformer 2- IAnnotationTransformer2 3- IHookable 4- IInvokedMethodListener 5- IMethodInterceptor 6- IReporter 7- …

Test Automation
How to Capture Screenshot in Selenium Webdriver-3 different ways

Abstract– Capturing screenshot is an essential feature of a test automation framework, They help in defect analyses once after test execution is over. In this blog post we will see how to capture screenshot, there are different ways to capture screenshots. Which includes- 1- Capture full Page 2- Capturng full …

Shares