Verify Snapshot Testing

repository·main·Indexed 25 days ago

https://github.com/verifytests/verify

A snapshot testing tool that simplifies asserting complex data models by serializing test results into files and comparing them across runs. It supports multiple .NET runtimes (net462 through net10) and integrates with testing frameworks including NUnit, xUnit V3, MSTest, Fixie, Expecto, and TUnit. Features include JSON verification via VerifyJson, support for asynchronous values, and various snapshot management methods such as IDE plugins, DiffEngineTray, and CLI tools.

Tokens
43.8K
Snippets
221
Records
253
Agent score
75%

What's inside Verify

  1. Overview of Verify snapshot testing

    main

    Verify is a snapshot testing tool designed to simplify the assertion of complex data models and documents.

    How it works:

    1. During the assertion phase, Verify is called on a test result.
    2. It serializes the result and stores it in a file matching the test name.
    3. On subsequent executions, the new result is serialized and compared against the existing file.
    4. The test fails if the snapshots do not match, indicating either an unexpected change or that the reference snapshot needs updating.

    Note on Licensing: From August 2026, commercial organizations and government agencies using official binary releases may be subject to a subscription fee. The source code remains open and free, and individuals, non-revenue organizations, CI, forks, and local development are unaffected.

  2. Explore Verify plugins

    main

    Verify provides a wide range of plugins to extend its capabilities for specific technologies, file formats, and UI frameworks. Common categories include:

    • Web & UI: Verify.AspNetCore, Verify.Blazor, Verify.Bunit, Verify.Avalonia, Verify.WinForms, Verify.Xamarin, Verify.Xaml, and Verify.HeadlessBrowsers (supporting Playwright, Puppeteer Sharp, or Selenium).
    • Documents & Data: Verify.Aspose (pdf, docx, xlsx, pptx), Verify.ClosedXml (Excel), Verify.CsvHelper (CSV), Verify.OpenXml (Excel), Verify.PdfPig (pdf), Verify.QuestPDF (pdf), and Verify.Yaml.
    • Databases: Verify.Cosmos (Azure CosmosDB), Verify.MongoDB, Verify.RavenDb, and Verify.SqlServer.
    • JSON & Serialization: Verify.NewtonsoftJson, Verify.SystemJson, and Verify.Yaml.
    • Logging & Diagnostics: Verify.MicrosoftLogging, Verify.Serilog, Verify.OpenTelemetry, and Verify.Diagnostics.
    • Mocking & Testing Frameworks: Verify.Moq, Verify.NSubstitute, Verify.FakeItEasy, and Verify.MassTransit.
    • Images: Verify.ImageSharp, Verify.ImageMagick, Verify.ImageHash, and Verify.Phash.

    Check the official plugin list for the full enumeration of supported integrations.

  3. Configure Source Control for Verify files

    main

    To maintain a clean repository, follow these source control conventions:

    1. Exclude received files: Add *.received.* to your .gitignore. If using UseSplitModeForUniqueDirectory, also include *.received/.
    2. Include verified files: All *.verified.* files must be committed to source control.
    3. Git Attributes: Use .gitattributes to ensure text files use lf line endings and UTF-8 encoding, and to mark binary files to prevent merging issues.

    Example .gitattributes configuration:

    *.verified.txt text eol=lf working-tree-encoding=UTF-8
    *.verified.xml text eol=lf working-tree-encoding=UTF-8
    *.verified.json text eol=lf working-tree-encoding=UTF-8
    *.verified.bin binary
    *.verified.txt text eol=lf working-tree-encoding=UTF-8
    *.verified.xml text eol=lf working-tree-encoding=UTF-8
    *.verified.json text eol=lf working-tree-encoding=UTF-8
    *.verified.bin binary
  4. Enable Verify in Fixie test framework

    main

    Because Fixie is less opinionated, you must manually configure the test project lifecycle to enable Verify. You need to implement ITestProject and IExecution interfaces.

    Requirements:

    1. In ITestProject.Configure, assign the target assembly using VerifierSettings.AssignTargetAssembly(environment.Assembly).
    2. In IExecution.Run, wrap test executions using ExecutionState.Set(testClass, test, parameters) to ensure Verify can correctly identify the context of the failing test.
    public class TestProject :
        ITestProject,
        IExecution
    {
        public void Configure(TestConfiguration configuration, TestEnvironment environment)
        {
            VerifierSettings.AssignTargetAssembly(environment.Assembly);
            configuration.Conventions.Add<DefaultDiscovery, TestProject>();
        }
    
        public async Task Run(TestSuite testSuite)
        {
            foreach (var testClass in testSuite.TestClasses)
            {
                foreach (var test in testClass.Tests)
                {
                    if (test.HasParameters)
                    {
                        foreach (var parameters in test
                                     .GetAll<TestCase>()
                                     .Select(_ => _.Parameters))
                        {
                            using (ExecutionState.Set(testClass, test, parameters))
                            {
                                await test.Run(parameters);
                            }
                        }
                    }
                    else
                    {
                        using (ExecutionState.Set(testClass, test, null))
                        {
                            await test.Run();
                        }
                    }
                }
            }
        }
    }
  5. Publish .received files on Azure DevOps

    main

    To ensure that .received files are available for inspection when tests fail in an Azure DevOps pipeline, you must set a flag during a failed test runner step, stage the files using CopyFiles@2, and then publish them using PublishBuildArtifacts@1. This is necessary because PublishBuildArtifacts@1 does not support wildcards directly.

    Follow these steps in your YAML pipeline:

    1. Set a failure flag: Immediately after your test runner step, add a CmdLine@2 task that runs only if the previous step failed to set the publishverify variable.
    2. Stage the files: Use CopyFiles@2 to move all **/*.received.* files to the $(Build.ArtifactStagingDirectory)/Verify folder.
    3. Publish the artifacts: Use PublishBuildArtifacts@1 to publish the staged folder as an artifact named Verify.
    - task: CmdLine@2
      displayName: 'Set flag to publish Verify *.received.* files when test step fails'
      condition: failed()
      inputs:
        script: 'echo "##vso[task.setvariable variable=publishverify]Yes"'
    
    - task: CopyFiles@2
      condition: eq(variables['publishverify'], 'Yes')
      displayName: 'Copy Verify *.received.* files to Artifact Staging'
      inputs:
        contents: '**/*.received.*' 
        targetFolder: '$(Build.ArtifactStagingDirectory)/Verify'
        cleanTargetFolder: true
        overWrite: true
    
    - task: PublishBuildArtifacts@1
      displayName: 'Publish Verify *.received.* files as Artifacts'
      name: 'verifypublish'
      condition: eq(variables['publishverify'], 'Yes')
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)/Verify'
        ArtifactName: 'Verify'
        publishLocation: 'Container'
  6. Add NuGet packages for MSTest

    main

    To use Verify with MSTest, add the following NuGet packages to your test project:

    • Microsoft.NET.Test.Sdk (v18.8.1+)
    • MSTest.TestAdapter (v4.3.2+)
    • MSTest.TestFramework (v4.3.2+)
    • Verify.MSTest (v31.25.0+)
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.8.1" />
    <PackageReference Include="MSTest.TestAdapter" Version="4.3.2" />
    <PackageReference Include="MSTest.TestFramework" Version="4.3.2" />
    <PackageReference Include="Verify.MSTest" Version="31.25.0" />
  7. Add NuGet packages for Verify with xUnit v3

    main

    To use Verify with xUnit v3, add the following packages to your test project. Ensure <ImplicitUsings> is set to enable in your project file so you can use Verify() directly. If ImplicitUsings is not enabled, you must use Verifier.Verify() instead.

    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.8.1" />
    <PackageReference Include="Verify.XunitV3" Version="31.25.0" />
    <PackageReference Include="xunit.v3" Version="3.2.2" />