PageSourceSearch

https://pester.dev/assets/js/fab59332.65d914fe.js

js pester.dev collected 2026-10-03 19:36:10 UTC 5,383 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkpester_docs=self.webpackChunkpester_docs||[]).push([["1413"],{6170(e,t,s){s.r(t),s.d(t,{metadata:()=>n,default:()=>h,frontMatter:()=>i,contentTitle:()=>a,toc:()=>d,assets:()=>l});var n=JSON.parse('{"id":"assertions/should-command","title":"Should","description":"Introduction to the Should command that\'s used to define assertions that fail or pass the test","source":"@site/versioned_docs/version-v5/assertions/should-command.mdx","sourceDirName":"assertions","slug":"/assertions/should-command","permalink":"/docs/v5/assertions/should-command","draft":false,"unlisted":false,"editUrl":"https://github.com/pester/docs/edit/main/versioned_docs/version-v5/assertions/should-command.mdx","tags":[],"version":"v5","frontMatter":{"title":"Should","description":"Introduction to the Should command that\'s used to define assertions that fail or pass the test"},"sidebar":"docs","previous":{"title":"Troubleshooting","permalink":"/docs/v5/usage/troubleshooting"},"next":{"title":"Assertion Reference","permalink":"/docs/v5/assertions/"}}'),o=s(1987),r=s(7008);let i={title:"Should",description:"Introduction to the Should command that's used to define assertions that fail or pass the test"},a,l={},d=[{value:"Collect all Should failures (soft assertions)",id:"collect-all-should-failures-soft-assertions",level:3}];function c(e){let t={a:"a",code:"code",h3:"h3",p:"p",pre:"pre",strong:"strong",...(0,r.R)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsxs)(t.p,{children:[(0,o.jsx)(t.code,{children:"Should"})," is used to do an assertion that fails or passes the test."]}),"\n",(0,o.jsx)(t.p,{children:"An example of assertion is comparing two strings in case insensitive manner:"}),"\n",(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{className:"language-powershell",children:'"Pester is bad." | Should -Be "Pester is awesome!"\n'})}),"\n",(0,o.jsx)(t.p,{children:"Which throws an exception with this text to fail the test:"}),"\n",(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{children:"Expected strings to be the same, but they were different.\nExpected length: 18\nActual length:   14\nStrings differ at index 10.\nExpected: 'Pester is awesome!'\nBut was:  'Pester is bad.'\n           ----------^\n"})}),"\n",(0,o.jsxs)(t.p,{children:["Other assertion types can be found in ",(0,o.jsx)(t.a,{href:".",children:"Assertion Reference"}),"."]}),"\n",(0,o.jsx)(t.h3,{id:"collect-all-should-failures-soft-assertions",children:"Collect all Should failures (soft assertions)"}),"\n",(0,o.jsxs)(t.p,{children:[(0,o.jsx)(t.code,{children:"Should"})," can now be configured to continue on failure. This will report the error to Pester, but won't fail the test immediately. Instead, all the Should failures are collected and reported at the end of the test. This allows you to put multiple assertions into one It and still get complete information on failure. This is known as a ",(0,o.jsx)(t.strong,{children:"soft assertion"}),"."]}),"\n",(0,o.jsxs)(t.p,{children:["This new Should behavior is opt-in and can be enabled via ",(0,o.jsx)(t.code,{children:"Should.ErrorAction = 'Continue'"})," on the configuration object or via ",(0,o.jsx)(t.code,{children:"$PesterPreference"}),". Setting ",(0,o.jsx)(t.code,{children:"-ErrorAction Stop"})," on a single assertion makes that one fail the test immediately even when soft assertions are enabled, which is useful to guard a precondition."]}),"\n",(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{className:"language-powershell",children:' BeforeAll {\n    function Get-User {\n        @{\n            Name = "Jakub"\n            Age = 31\n        }\n    }\n}\n\nDescribe "describe" {\n    It "user" {\n        $user = Get-User\n\n        $user | Should -Not -BeNullOrEmpty -ErrorAction Stop\n        $user.Name | Should -Be "Tomas"\n        $user.Age | Should -Be 27\n\n    }\n}\n\n'})}),"\n",(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{children:"Starting test discovery in 1 files.\nFound 1 tests. 51ms\nTest discovery finished. 83ms\nDescribing describe\n  [-] user 124ms (109ms|15ms)\n   [0] Expected strings to be the same, but they were different.\n   String lengths are both 5.\n   Strings differ at index 0.\n   Expected: 'Tomas'\n   But was:  'Jakub'\n   at $user.Name | Should -Be \"Tomas\"\n   [1] Expected 27, but got 31.\n   at $user.Age | Should -Be 27\nTests completed in 286ms\nTests Passed: 0, Failed: 1, Skipped: 0, Total: 1, NotRun: 0\n"})}),"\n",(0,o.jsxs)(t.p,{children:["This allows you to check complex objects easily without writing It for each of the properties that you want to test. You can also use ",(0,o.jsx)(t.code,{children:"-ErrorAction Stop"})," to force a failure when a pre-condition is not met. In our case if ",(0,o.jsx)(t.code,{children:"$user"})," was null, there would be no point in testing the object further and we would fail the test immediately."]})]})}function h(e={}){let{wrapper:t}={...(0,r.R)(),...e.components};return t?(0,o.jsx)(t,{...e,children:(0,o.jsx)(c,{...e})}):c(e)}},7008(e,t,s){s.d(t,{R:()=>i,x:()=>a});var n=s(1763);let o={},r=n.createContext(o);function i(e){let t=n.useContext(r);return n.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function a(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:i(e.components),n.createElement(r.Provider,{value:t},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.