|
| 1 | +--- |
| 2 | +uid: snapshottest-details |
| 3 | +title: SnapshotTest Details |
| 4 | +--- |
| 5 | + |
| 6 | +# `<SnapshotTest>` Details |
| 7 | + |
| 8 | +bUnit's support for snapshot testing comes with the <xref:Bunit.SnapshotTest> component. In snapshot testing, you declare your input (e.g. one or more component under test) and the expected output, and the library will automatically tell you if they do not match. |
| 9 | + |
| 10 | +> [!NOTE] |
| 11 | +> One notable snapshot testing feature is missing now; the ability to auto-generate the expected output initially, when it is not specified. If you want to contribute to this, take a look at [issue #3 on GitHub](https://github.com/egil/bunit/issues/3). |
| 12 | +
|
| 13 | +> [!WARNING] |
| 14 | +> Razor tests, where <xref:Bunit.SnapshotTest> components are used, are currently only compatible with using xUnit as the general purpose testing framework. |
| 15 | +
|
| 16 | +## Parameters |
| 17 | + |
| 18 | +All the parameters the <xref:Bunit.SnapshotTest> component support is shown in the listing below: |
| 19 | + |
| 20 | +[!code-html[](../../samples/tests/razor/AllSnapshotTestParameters.razor)] |
| 21 | + |
| 22 | +Let us go over each of these: |
| 23 | + |
| 24 | +1. **<xref:Bunit.RazorTesting.FixtureBase`1.Setup>** and **<xref:Bunit.RazorTesting.FixtureBase`1.SetupAsync>:** |
| 25 | + The `Setup` and `SetupAsync` methods can be used to register any services that should be injected into the components declared inside the `<TestInput>` and `<ExpectedOutput>`, and you can use both `Setup` and `SetupAsync`, if needed. If both are provided, `Setup` is called first. |
| 26 | +2. **<xref:Bunit.RazorTesting.RazorTestBase.Description>:** |
| 27 | + If a description is provided, it will be displayed by the test runner when the test runs, and in Visual Studio's Test Explorer. If no description is provided, "SnapshotTest #NUM" is used, where NUM is the position the test has in the file it is declared in. |
| 28 | +3. **<xref:Bunit.RazorTesting.RazorTestBase.Skip>:** |
| 29 | + If the skip parameter is provided, the test is skipped, and the text entered in the skip parameter is passed to the test runner as the reason to skip the test. |
| 30 | +4. **<xref:Bunit.RazorTesting.RazorTestBase.Timeout>:** |
| 31 | + If provided, the test runner will terminate the test after the specified amount of time, if it has not completed already. |
| 32 | +5. **<xref:Bunit.SnapshotTest.TestInput> child component:** |
| 33 | + Inside the `<TestInput>` child component is where you put all Razor and HTML markup, that constitute the test input or component under test. |
| 34 | +6. **<xref:Bunit.SnapshotTest.ExpectedOutput> child component:** |
| 35 | + Inside the `<ExpectedOutput>` child component is where you put all Razor and HTML markup that represents what the rendered result of `<TestInput>` of should be. |
| 36 | + |
| 37 | +## What Happens When The Test Runs? |
| 38 | + |
| 39 | +When a <xref:Bunit.SnapshotTest> runs, this happens: |
| 40 | + |
| 41 | +1. It will first call the setup methods. |
| 42 | +2. Then it will render the `<TestInput>` and `<ExpectedOutput>` child components. |
| 43 | +3. Finally, it will compare the rendered markup from the `<TestInput>` and `<ExpectedOutput>` child components, using the semantic HTML comparer built into bUnit. |
| 44 | + |
| 45 | +The semantic comparison in bUnit allows you to customize the it through _"comparison modifiers"_ in the `<ExpectedOutput>` markup. For example, if you want to tell the semantic comparer to ignore the case of the text content inside an element, you can add the `diff:ignoreCase` attribute to the element inside `<ExpectedOutput>`. |
| 46 | + |
| 47 | +To learn more about semantic comparison modifiers, go to the <xref:semantic-html-comparison> page. |
0 commit comments