Skip to content

Commit 73f8fd1

Browse files
Use Verify library for approval tests (#2330)
* Used Verify library for approval tests * Removed unnecessary item group * Approval -> Verify renaming * Avoid duplication of directory name for VerifyTests * Disabled diff tool for better console experience * Renamed files after classes * Updated documentation --------- Co-authored-by: Alina Smirnova <[email protected]>
1 parent bed071f commit 73f8fd1

File tree

38 files changed

+62
-96
lines changed

38 files changed

+62
-96
lines changed

docs/articles/contributing/running-tests.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ dotnet test -c Release -f net5.0 BenchmarkDotNet.sln
1414

1515
You should be able to run all of tests from your IDE as well.
1616

17-
## Approval Tests
17+
## Verify Tests
1818

19-
For some unit tests (e.g. for exporter tests) BenchmarkDotNet uses [approval tests'](https://approvaltests.com/) implementation for .NET: [ApprovalTests.Net](https://github.com/approvals/ApprovalTests.Net).
20-
* The expected value for each test is stored in a `*.approved.txt` file located near the test source file in the repository. ApprovalTests.NET generates approved file's names automatically according test name and its parameters. This files must be added under the source control.
21-
* It also creates a `*.received` file for each failed test. You can use different reporters for convenient file comparison. By default we use XUnit2Reporter, so you can find test run results on the test runner console as usual. You can add [UseReporter(typeof(KDiffReporter))] on test class and then ApprovalTests will open KDiff for each failed test. This way you can easily understand what's the difference between approved and received values and choose the correct one.
19+
For some unit tests (e.g. for exporter tests) BenchmarkDotNet uses [Verify](https://github.com/VerifyTests/Verify).
20+
* The expected value for each test is stored in a `*.verified.txt` file located near the test source file in the repository. Verify generates verified file's names automatically according test name and its parameters. This files must be added under the source control.
21+
* It also creates a `*.received` file for each failed test. You can use diff tools for convenient file comparison. By default you can find test run results on the test runner console as usual. You can comment out the line ```result.DisableDiff()``` in ```VerifySettingsFactory.Create``` method and then Verify will open KDiff for each failed test. This way you can easily understand what's the difference between verified and received values and choose the correct one.

tests/BenchmarkDotNet.Tests/Attributes/ParamsAllValuesApprovalTests.cs renamed to tests/BenchmarkDotNet.Tests/Attributes/ParamsAllValuesVerifyTests.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,27 @@
22
using System.Diagnostics.CodeAnalysis;
33
using System.Globalization;
44
using System.Linq;
5-
using System.Runtime.CompilerServices;
65
using System.Threading;
7-
using ApprovalTests;
8-
using ApprovalTests.Namers;
9-
using ApprovalTests.Reporters;
6+
using System.Threading.Tasks;
107
using BenchmarkDotNet.Exporters;
118
using BenchmarkDotNet.Loggers;
129
using BenchmarkDotNet.Tests.Mocks;
1310
using BenchmarkDotNet.Attributes;
14-
using BenchmarkDotNet.Tests.XUnit;
11+
using BenchmarkDotNet.Tests.Builders;
1512
using BenchmarkDotNet.Validators;
1613
using JetBrains.Annotations;
14+
using VerifyXunit;
1715
using Xunit;
1816

1917
namespace BenchmarkDotNet.Tests.Attributes
2018
{
21-
// In case of failed approval tests, use the following reporter:
22-
// [UseReporter(typeof(KDiffReporter))]
23-
[UseReporter(typeof(PatchedXUnit2Reporter))]
24-
[UseApprovalSubdirectory("ApprovedFiles")]
25-
[Collection("ApprovalTests")]
26-
public class ParamsAllValuesApprovalTests : IDisposable
19+
[Collection("VerifyTests")]
20+
[UsesVerify]
21+
public class ParamsAllValuesVerifyTests : IDisposable
2722
{
2823
private readonly CultureInfo initCulture;
2924

30-
public ParamsAllValuesApprovalTests() => initCulture = Thread.CurrentThread.CurrentCulture;
25+
public ParamsAllValuesVerifyTests() => initCulture = Thread.CurrentThread.CurrentCulture;
3126

3227
[UsedImplicitly]
3328
public static TheoryData<Type> GetBenchmarkTypes()
@@ -40,10 +35,8 @@ public static TheoryData<Type> GetBenchmarkTypes()
4035

4136
[Theory]
4237
[MemberData(nameof(GetBenchmarkTypes))]
43-
[MethodImpl(MethodImplOptions.NoInlining)]
44-
public void BenchmarkShouldProduceSummary(Type benchmarkType)
38+
public Task BenchmarkShouldProduceSummary(Type benchmarkType)
4539
{
46-
NamerFactory.AdditionalInformation = benchmarkType.Name;
4740
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
4841

4942
var logger = new AccumulationLogger();
@@ -60,7 +53,9 @@ public void BenchmarkShouldProduceSummary(Type benchmarkType)
6053
foreach (var error in errors)
6154
logger.WriteLineError("* " + error.Message);
6255

63-
Approvals.Verify(logger.GetLog());
56+
var settings = VerifySettingsFactory.Create();
57+
settings.UseTextForParameters(benchmarkType.Name);
58+
return Verifier.Verify(logger.GetLog(), settings);
6459
}
6560

6661
public void Dispose() => Thread.CurrentThread.CurrentCulture = initCulture;

tests/BenchmarkDotNet.Tests/BenchmarkDotNet.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
</ItemGroup>
1717
<ItemGroup>
1818
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
19+
<PackageReference Include="Verify.Xunit" Version="20.3.2" />
1920
<PackageReference Include="xunit" Version="2.4.2" />
2021
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
2122
<PrivateAssets>all</PrivateAssets>
2223
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2324
</PackageReference>
24-
<PackageReference Include="ApprovalTests" Version="3.1.0" />
2525
</ItemGroup>
2626
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' ">
2727
<ProjectReference Include="..\..\src\BenchmarkDotNet.Diagnostics.Windows\BenchmarkDotNet.Diagnostics.Windows.csproj" />

0 commit comments

Comments
 (0)