karatelabs/karate · warning
Failed to write JUnit XML for
Error message
Failed to write JUnit XML for {}: {} What it means
JunitXmlWriter.writeFeature() converts a FeatureResult to XML and writes it as <packageQualifiedName>.xml in the output directory. Any exception in conversion or writing is logged with the feature's display name; the test run continues without that XML report.
Solutions
- Ensure outputDir exists and is writable before calling writeFeature
- Check the derived fileName (packageQualifiedName) for illegal characters or collisions
- Enable debug logging ('JUnit XML written' vs the warning) to isolate the failing feature
- Inspect the FeatureResult content for data that breaks conversion and upgrade Karate if it is a serializer bug
Defensive patterns
Strategy: try-catch
Validate before calling
if (!Files.isWritable(outputDir))
throw new IllegalStateException("output dir not writable: " + outputDir);
String name = result.getFeature().getResource().getPackageQualifiedName() + ".xml";
if (!name.matches("[A-Za-z0-9._-]+")) logger.warn("suspicious xml file name: {}", name); Try / catch
try {
Files.writeString(outputDir.resolve(fileName), featureToXml(result));
} catch (Exception e) {
logger.warn("Failed to write JUnit XML for {}: {}", result.getDisplayName(), e.getMessage(), e);
} Prevention
- Prefer the listener-driven writeSerialized path; reserve writeFeature for custom pipelines
- Validate packageQualifiedName produces unique, legal file names
- Upgrade Karate if conversion fails reproducibly on specific result data
When it happens
Trigger: Exception from featureToXml(result) or Files.writeString(xmlPath, xml) — unwritable/missing output directory, or a FeatureResult whose content breaks XML generation.
Common situations: Legacy callers writing JUnit XML outside the listener flow; outputDir cleaned concurrently; features with names producing colliding or invalid XML file names; serialization bugs on unusual result data.
Understand the failure class
Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.
Related errors
- Failed to write JUnit XML for
- Failed to copy static resources
- Failed to write HTML report
- Failed to externalize embeds
- Failed to copy ext assets for
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/1292b7fafcdc6e1d.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/output/JunitXmlWriter.java:117
Files.writeString(outputDir.resolve(fileName), content);
} catch (Exception e) {
logger.warn("Failed to write xml for {}: {}", fileName, e.getMessage());
}
}
public static void writeFeature(FeatureResult result, Path outputDir) {
try {
if (!Files.exists(outputDir)) {
Files.createDirectories(outputDir);
}
String fileName = result.getFeature().getResource().getPackageQualifiedName() + ".xml";
Path xmlPath = outputDir.resolve(fileName);
String xml = featureToXml(result);
Files.writeString(xmlPath, xml);
logger.debug("JUnit XML written: {}", xmlPath);
} catch (Exception e) {
logger.warn("Failed to write JUnit XML for {}: {}", result.getDisplayName(), e.getMessage());
}
}
/**
* Convert a single feature result to JUnit XML string.
*
* @param result the feature result
* @return XML string
*/
public static String featureToXml(FeatureResult result) {
DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
formatter.applyPattern("0.######");
String packageQualifiedName = result.getFeature().getResource().getPackageQualifiedName();
StringBuilder xml = new StringBuilder();
// Root testsuite element (single feature)View on GitHub (pinned to a22eb90246)