|
| 1 | +<!-- |
| 2 | +GENERATED FILE - DO NOT EDIT |
| 3 | +This file was generated by [MarkdownSnippets](https://github.com/SimonCropp/MarkdownSnippets). |
| 4 | +Source File: /docs/mdsource/dangling-files.source.md |
| 5 | +To change this file edit the source file and then run MarkdownSnippets. |
| 6 | +--> |
| 7 | + |
| 8 | +# Dangling snapshot files |
| 9 | + |
| 10 | +A dangling snapshot file are when `.verified.` file exist with no corresponding test. This can occur when a test, with corresponding test snapshot file(s), is renamed or deleted. The snapshot file are now redundant and noise on the file system and in the solution explorer |
| 11 | + |
| 12 | + |
| 13 | +## How dangling snapshot checks works |
| 14 | + |
| 15 | + * When each test is executed, any snapshots produced are recorded. |
| 16 | + * After all tests are executed, all recorded snapshots are checked against the snapshots that exist on disk |
| 17 | + * An exception is thrown if any files: |
| 18 | + * exist on disk and do not have a corresponding recorded test |
| 19 | + * have casing that does not match the test case |
| 20 | + |
| 21 | + |
| 22 | +## Experimental |
| 23 | + |
| 24 | +`DanglingSnapshots` is an experimental feature (marked with `[Experimental("VerifyDanglingSnapshots")]`) and is subject to change in minor version. |
| 25 | + |
| 26 | +To use it requires the `VerifyDanglingSnapshots` warning to to be disabled using a `pragma warning disable`: |
| 27 | + |
| 28 | +``` |
| 29 | +#pragma warning disable VerifyDanglingSnapshots |
| 30 | +``` |
| 31 | + |
| 32 | + |
| 33 | +## Only in CI |
| 34 | + |
| 35 | +When running tests locally, it is not possible to detect if all tests are being run, or only a subset. As such `DanglingSnapshots.Run()` will only perform checks when executed on a build server. |
| 36 | + |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +In the tear-down of a test run (after all test have run), call `DanglingSnapshots.Run()`. |
| 41 | + |
| 42 | + |
| 43 | +### MSTest |
| 44 | + |
| 45 | +Use the `[AssemblyCleanup]` feature: |
| 46 | + |
| 47 | +<!-- snippet: DanglingSnapshotsMSTestUsage/DanglingSnapshots.cs --> |
| 48 | +<a id='snippet-DanglingSnapshotsMSTestUsage/DanglingSnapshots.cs'></a> |
| 49 | +```cs |
| 50 | +#pragma warning disable VerifyDanglingSnapshots |
| 51 | +[TestClass] |
| 52 | +public static class Cleanup |
| 53 | +{ |
| 54 | + [AssemblyCleanup] |
| 55 | + public static void Run() => |
| 56 | + DanglingSnapshots.Run(); |
| 57 | +} |
| 58 | +``` |
| 59 | +<sup><a href='/src/DanglingSnapshotsMSTestUsage/DanglingSnapshots.cs#L1-L8' title='Snippet source file'>snippet source</a> | <a href='#snippet-DanglingSnapshotsMSTestUsage/DanglingSnapshots.cs' title='Start of snippet'>anchor</a></sup> |
| 60 | +<!-- endSnippet --> |
| 61 | + |
| 62 | + |
| 63 | +### NUnit |
| 64 | + |
| 65 | +use the `[OneTimeTearDown]` feature: |
| 66 | + |
| 67 | +<!-- snippet: DanglingSnapshotsNUnitUsage/DanglingSnapshots.cs --> |
| 68 | +<a id='snippet-DanglingSnapshotsNUnitUsage/DanglingSnapshots.cs'></a> |
| 69 | +```cs |
| 70 | +#pragma warning disable VerifyDanglingSnapshots |
| 71 | + |
| 72 | +[SetUpFixture] |
| 73 | +public static class SetUp |
| 74 | +{ |
| 75 | + [OneTimeTearDown] |
| 76 | + public static void Run() => |
| 77 | + DanglingSnapshots.Run(); |
| 78 | +} |
| 79 | +``` |
| 80 | +<sup><a href='/src/DanglingSnapshotsNUnitUsage/DanglingSnapshots.cs#L1-L9' title='Snippet source file'>snippet source</a> | <a href='#snippet-DanglingSnapshotsNUnitUsage/DanglingSnapshots.cs' title='Start of snippet'>anchor</a></sup> |
| 81 | +<!-- endSnippet --> |
| 82 | + |
| 83 | + |
| 84 | +### XUnit |
| 85 | + |
| 86 | +Define an XUnit collection: |
| 87 | + |
| 88 | +<!-- snippet: DanglingSnapshotsXUnitUsage/DanglingSnapshots.cs --> |
| 89 | +<a id='snippet-DanglingSnapshotsXUnitUsage/DanglingSnapshots.cs'></a> |
| 90 | +```cs |
| 91 | +#pragma warning disable VerifyDanglingSnapshots |
| 92 | + |
| 93 | +[CollectionDefinition(nameof(SharedFixtureCollection))] |
| 94 | +public class SharedFixtureCollection : |
| 95 | + ICollectionFixture<SharedFixture>; |
| 96 | + |
| 97 | +public sealed class SharedFixture : |
| 98 | + IDisposable |
| 99 | +{ |
| 100 | + public void Dispose() => |
| 101 | + DanglingSnapshots.Run(); |
| 102 | +} |
| 103 | +``` |
| 104 | +<sup><a href='/src/DanglingSnapshotsXunitUsage/DanglingSnapshots.cs#L1-L12' title='Snippet source file'>snippet source</a> | <a href='#snippet-DanglingSnapshotsXUnitUsage/DanglingSnapshots.cs' title='Start of snippet'>anchor</a></sup> |
| 105 | +<!-- endSnippet --> |
| 106 | + |
| 107 | +Apply that collection to all tests: |
| 108 | + |
| 109 | +<!-- snippet: XunitDanglingCollection --> |
| 110 | +<a id='snippet-XunitDanglingCollection'></a> |
| 111 | +```cs |
| 112 | +[Collection(nameof(SharedFixtureCollection))] |
| 113 | +public class Tests |
| 114 | +{ |
| 115 | + [Fact] |
| 116 | + public Task Simple() => |
| 117 | + Verify("Foo"); |
| 118 | +``` |
| 119 | +<sup><a href='/src/DanglingSnapshotsXunitUsage/Tests.cs#L1-L8' title='Snippet source file'>snippet source</a> | <a href='#snippet-XunitDanglingCollection' title='Start of snippet'>anchor</a></sup> |
| 120 | +<!-- endSnippet --> |
| 121 | + |
| 122 | + |
| 123 | +### XUnitV3 |
| 124 | + |
| 125 | +Define an XUnit collection: |
| 126 | + |
| 127 | +<!-- snippet: DanglingSnapshotsXUnitV3Usage/DanglingSnapshots.cs --> |
| 128 | +<a id='snippet-DanglingSnapshotsXUnitV3Usage/DanglingSnapshots.cs'></a> |
| 129 | +```cs |
| 130 | +#pragma warning disable VerifyDanglingSnapshots |
| 131 | + |
| 132 | +[CollectionDefinition(nameof(SharedFixtureCollection))] |
| 133 | +public class SharedFixtureCollection : |
| 134 | + ICollectionFixture<SharedFixture>; |
| 135 | + |
| 136 | +public sealed class SharedFixture : |
| 137 | + IDisposable |
| 138 | +{ |
| 139 | + public void Dispose() => |
| 140 | + DanglingSnapshots.Run(); |
| 141 | +} |
| 142 | +``` |
| 143 | +<sup><a href='/src/DanglingSnapshotsXunitV3Usage/DanglingSnapshots.cs#L1-L12' title='Snippet source file'>snippet source</a> | <a href='#snippet-DanglingSnapshotsXUnitV3Usage/DanglingSnapshots.cs' title='Start of snippet'>anchor</a></sup> |
| 144 | +<!-- endSnippet --> |
| 145 | + |
| 146 | +Apply that collection to all tests: |
| 147 | + |
| 148 | +<!-- snippet: XunitV3DanglingCollection --> |
| 149 | +<a id='snippet-XunitV3DanglingCollection'></a> |
| 150 | +```cs |
| 151 | +[Collection(nameof(SharedFixtureCollection))] |
| 152 | +public class Tests |
| 153 | +{ |
| 154 | + [Fact] |
| 155 | + public Task Simple() => |
| 156 | + Verify("Foo"); |
| 157 | +``` |
| 158 | +<sup><a href='/src/DanglingSnapshotsXunitV3Usage/Tests.cs#L1-L8' title='Snippet source file'>snippet source</a> | <a href='#snippet-XunitV3DanglingCollection' title='Start of snippet'>anchor</a></sup> |
| 159 | +<!-- endSnippet --> |
0 commit comments