Construct unique safe filenames in TestNG
Posted: October 22nd, 2009 | Author: Dave | Filed under: Examples | Tags: debug, failure, testng | No Comments »In order to save files for investigating failures from within TestNG it’s important to have a safe filename that is unique to the test – otherwise you may overwrite important files. I have written the following simple method in Java that is called from a listener with an ITestResult parameter to construct a unique file name that should be safe on most file systems.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | private static String fileNameFrom(ITestResult result, String fileExtension) { List<String> parts = new ArrayList<String>(); parts.add(result.getTestClass().getName()); parts.add(result.getMethod().getMethodName()); //add parameters Object[] parameters = result.getParameters(); for (Object parameter: parameters) parts.add(parameter != null ? parameter.toString() : NULL_PARAMETER_VALUE); String fileName = StringUtils.join(parts.toArray(new String[parts.size()]), "."); fileName = fileName.replaceAll("[\\^\\\\.\\-:;#_]", "_"); return fileName + "." + fileExtension; } |
Leave a Reply