1"use strict";(globalThis.webpackChunktunit_docs_site||=[]).push([[379],{94239(e,n,t){t.r(n),t.d(n,{assets:()=>c,contentTitle:()=>r,default:()=>h,frontMatter:()=>o,metadata:()=>i,toc:()=>l});const i=JSON.parse('{"id":"extending/exception-handling","title":"Exception Handling","description":"When a test fails, TUnit throws an exception. Most of the time you don\'t need to think about this \u2014 the framework handles it and reports the failure. But there are a couple of exception types you might want to throw yourself.","source":"@site/docs/extending/exception-handling.md","sourceDirName":"extending","slug":"/extending/exception-handling","permalink":"/docs/extending/exception-handling","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{},"sidebar":"docs","previous":{"title":"Logging","permalink":"/docs/extending/logging"},"next":{"title":"Dynamically Created Tests","permalink":"/docs/extending/dynamic-tests"}}');var a=t(74848),s=t(28453);const o={},r="Exception Handling",c={},l=[{value:"Skipping a test at runtime",id:"skipping-a-test-at-runtime",level:2},{value:"Marking a test as inconclusive",id:"marking-a-test-as-inconclusive",level:2},{value:"Checking for failure in teardown",id:"checking-for-failure-in-teardown",level:2}];function d(e){const n={code:"code",h1:"h1",h2:"h2",header:"header",p:"p",pre:"pre",...(0,s.R)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(n.header,{children:(0,a.jsx)(n.h1,{id:"exception-handling",children:"Exception Handling"})}),"\n",(0,a.jsx)(n.p,{children:"When a test fails, TUnit throws an exception. Most of the time you don't need to think about this \u2014 the framework handles it and reports the failure. But there are a couple of exception types you might want to throw yourself."}),"\n",(0,a.jsx)(n.h2,{id:"skipping-a-test-at-runtime",children:"Skipping a test at runtime"}),"\n",(0,a.jsxs)(n.p,{children:["If a test can't run because of some runtime condition, throw ",(0,a.jsx)(n.code,{children:"SkipTestException"}),". The test will be reported as skipped rather than failed."]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-csharp",children:'[Test]\npublic async Task RequiresExternalService()\n{\n if (!await IsServiceAvailable())\n {\n throw new SkipTestException("Service is not available");\n }\n\n // Test logic\n}\n'})}),"\n",(0,a.jsx)(n.h2,{id:"marking-a-test-as-inconclusive",children:"Marking a test as inconclusive"}),"\n",(0,a.jsxs)(n.p,{children:["If a test can't determine a pass/fail result, throw ",(0,a.jsx)(n.code,{children:"InconclusiveTestException"}),"."]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-csharp",children:'[Test]\npublic async Task CheckFeatureFlag()\n{\n var flag = await GetFeatureFlag("new-checkout");\n\n if (flag is null)\n {\n throw new InconclusiveTestException("Feature flag not configured");\n }\n\n // Test logic\n}\n'})}),"\n",(0,a.jsx)(n.h2,{id:"checking-for-failure-in-teardown",children:"Checking for failure in teardown"}),"\n",(0,a.jsxs)(n.p,{children:["In an ",(0,a.jsx)(n.code,{children:"[After(Test)]"})," hook, you can check whether the test failed via ",(0,a.jsx)(n.code,{children:"TestContext"}),":"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-csharp",children:"[After(Test)]\npublic async Task TakeScreenshotOnFailure(TestContext context)\n{\n if (context.Execution.Result?.Exception is not null)\n {\n await SaveScreenshot(context.Metadata.TestDetails.TestName);\n }\n}\n"})})]})}function h(e={}){const{wrapper:n}={...(0,s.R)(),...e.components};return n?(0,a.jsx)(n,{...e,children:(0,a.jsx)(d,{...e})}):d(e)}},28453(e,n,t){t.d(n,{R:()=>o,x:()=>r});var i=t(96540);const a={},s=i.createContext(a);function o(e){const n=i.useContext(s);return i.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function r(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(a):e.components||a:o(e.components),i.createElement(s.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.