{"record":{"id":"d621ba4f3c3e2c28","repo":"ThreeMammals/Ocelot","slug":"dotnet-test-failed-with-exit-code-exitcode","errorCode":null,"errorMessage":"dotnet test failed with exit code {exitCode}","messagePattern":"dotnet test failed with exit code (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"build.cake","lineNumber":583,"sourceCode":"\t\t\t*/\r\n\t\t\t// Use StartProcess instead of DotNetTest for better control\r\n\t\t\tvar exitCode = StartProcess(\"dotnet\", new ProcessSettings\r\n\t\t\t{\r\n\t\t\t\tArguments = $\"test \\\"{unitTestAssemblies}\\\" \" +\r\n\t\t\t\t\t\t\t$\"--configuration {compileConfig} \" +\r\n\t\t\t\t\t\t\t$\"--framework {tfm} \" +\r\n\t\t\t\t\t\t\t$\"--no-restore --no-build \" +\r\n\t\t\t\t\t\t\t$\"--verbosity {verbosity} \" +\r\n\t\t\t\t\t\t\t$\"--results-directory \\\"{artifactsForUnitTestsDir}\\\" \" +\r\n\t\t\t\t\t\t\t$\"--coverlet \" +\r\n\t\t\t\t\t\t\t$\"--coverlet-include \\\"[Ocelot*]*\\\" \" +\r\n\t\t\t\t\t\t\t$\"--coverlet-exclude \\\"[Ocelot.Testing]*\\\"\",\r\n\t\t\t\tWorkingDirectory = \".\"\r\n\t\t\t});\r\n\t\t\t// Only fail on actual test failures, not on thread exit issues\r\n\t\t\tif (exitCode != 0 && exitCode != 7)\r\n\t\t\t{\r\n\t\t\t\tthrow new Exception($\"dotnet test failed with exit code {exitCode}\");\r\n\t\t\t}\r\n\t\t\telse if (exitCode == 7)\r\n\t\t\t{\r\n\t\t\t\tWarning(\"Tests passed but background threads didn't exit cleanly (exit code 7). Ignoring.\");\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tInformation(\"ArtifactsForUnitTestsDir = \" + artifactsForUnitTestsDir);\r\n\t\t// Find all files matching pattern \"coverage.cobertura.*.xml\"\r\n\t\tvar coverageFiles = GetFiles(artifactsForUnitTestsDir.ToString() + \"/coverage.cobertura.*.xml\");\r\n\t\tif (!coverageFiles.Any())\r\n\t\t\tthrow new Exception($\"No coverage.cobertura.*.xml files found in {artifactsForUnitTestsDir}\");\r\n\t\t// Get the first matching file (or order by creation date if needed)\r\n\t\tvar coverageSummaryFile = coverageFiles.First();\r\n\t\tInformation(\"CoverageSummaryFile = \" + coverageSummaryFile);\r\n\t\tGenerateReport(coverageSummaryFile);\r\n\t\tInformation(\"##############################\");\r\n\t\tInformation(\"# Code coverage\");\r","sourceCodeStart":565,"sourceCodeEnd":601,"githubUrl":"https://github.com/ThreeMammals/Ocelot/blob/d1f22d930430410bfd0233f5e9e4f92980cf7df0/build.cake#L565-L601","documentation":"Thrown after 'dotnet test' (via coverlet collector) returns a non-zero exit code that is not the whitelisted code 7. Code 7 is deliberately treated as 'tests passed but background threads didn't exit cleanly' and only warns. Any other non-zero code means real test failures or a host crash.","triggerScenarios":"dotnet test exiting with codes other than 0 or 7: at least one test failed (exit 1), test host crashed, unhandled exception in a test, or collection abort.","commonSituations":"A flaky or genuinely failing unit test after a code change; test host process crash on .NET SDK mismatch; deadlock in tests causing abort.","solutions":["Re-run the tests locally with the same arguments and inspect the failing test output.","Fix the failing test or the regression it exposes (exit code 1 means test failure).","Check trx/console logs for 'Test host process crashed' — fix SDK/runtime version mismatch if so.","If the crash is thread-exit related but surfaces as a different code, add that code to the tolerated set only after verifying tests actually passed."],"exampleFix":"// before\nif (exitCode != 0 && exitCode != 7)\n// after (only if verified non-failing)\nif (exitCode != 0 && exitCode != 7 && exitCode != 8)","handlingStrategy":"try-catch","validationCode":"// verify locally before CI\ndotnet test ./test/Ocelot.UnitTests --collect:\"XPlat Code Coverage\"; echo $LASTEXITCODE","typeGuard":null,"tryCatchPattern":"try { RunUnitTests(); }\ncatch (Exception ex) when (ex.Message.Contains(\"exit code\")) { Warning($\"Test runner failed: {ex.Message}\"); throw; }","preventionTips":["Run the same dotnet test command locally before pushing","Keep test host SDK versions aligned with global.json","Treat only verified thread-exit codes (like 7) as tolerable","Check test logs for 'Test host process crashed'"],"tags":["dotnet-test","unit-tests","exit-code","ci"],"backgroundTag":"command-exit-code-failure","analyzedSha":"d1f22d930430410bfd0233f5e9e4f92980cf7df0","analyzedAt":"2026-09-12T13:50:06.067Z","contentChangedAt":"2026-09-12T13:50:06.067Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}