| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Verification tool to enable simple approval of complex models and documents.
Support is available via a Tidelift Subscription.
Given the following method:
public static class ClassBeingTested
{
public static Person FindPerson()
{
return new Person
{
Id = new Guid("ebced679-45d3-4653-8791-3d969c4a986c"),
Title = Title.Mr,
GivenNames = "John",
FamilyName = "Smith",
Spouse = "Jill",
Children = new List<string>
{
"Sam",
"Mary"
},
Address = new Address
{
Street = "4 Puddle Lane",
Country = "USA"
}
};
}
}Compare a traditional assertion based test to a verification test.
[Fact]
public void TraditionalTest()
{
var person = ClassBeingTested.FindPerson();
Assert.Equal(new Guid("ebced679-45d3-4653-8791-3d969c4a986c"), person.Id);
Assert.Equal(Title.Mr, person.Title);
Assert.Equal("John", person.GivenNames);
Assert.Equal("Smith", person.FamilyName);
Assert.Equal("Jill", person.Spouse);
Assert.Equal(2, person.Children.Count);
Assert.Equal("Sam", person.Children[0]);
Assert.Equal("Mary", person.Children[1]);
Assert.Equal("4 Puddle Lane", person.Address.Street);
Assert.Equal("USA", person.Address.Country);
}[Fact]
public Task Simple()
{
var person = ClassBeingTested.FindPerson();
return Verify(person);
}Given a class to be tested:
public static class ClassBeingTested
{
public static Person FindPerson()
{
return new Person
{
Id = new Guid("ebced679-45d3-4653-8791-3d969c4a986c"),
Title = Title.Mr,
GivenNames = "John",
FamilyName = "Smith",
Spouse = "Jill",
Children = new List<string>
{
"Sam",
"Mary"
},
Address = new Address
{
Street = "4 Puddle Lane",
Country = "USA"
}
};
}
}Support for xUnit
public class SampleTest :
VerifyBase
{
[Fact]
public Task Simple()
{
var person = ClassBeingTested.FindPerson();
return Verify(person);
}
public SampleTest(ITestOutputHelper output) :
base(output)
{
}
}Support for NUnit
[TestFixture]
public class SampleTest
{
[Test]
public Task Simple()
{
var person = ClassBeingTested.FindPerson();
return Verifier.Verify(person);
}
}Support for MSTest
[TestClass]
public class SampleTest :
VerifyBase
{
[TestMethod]
public Task Simple()
{
var person = ClassBeingTested.FindPerson();
return Verify(person);
}
}Support for rendering a Blazor Component to a verified file via bunit.
Given the following Component:
<div>
<h1>@Title</h1>
<button>MyButton</button>
</div>
@code {
[Parameter]
public string Title { get; set; } = "My Test Component";
}This test:
[Fact]
public Task Component()
{
var component = RenderComponent<TestComponent>();
return Verify(component);
}Will produce:
The component rendered as html ...Component.verified.html:
<div>
<h1>My Test Component</h1>
<button>MyButton</button>
</div>And the current model rendered as txt ...Component.info.verified.txt:
{
Instance: {
Title: 'My Test Component'
}
}When the test is initially run will fail with:
First verification. SampleTest.Simple.verified.txt not found. Verification command has been copied to the clipboard.
The clipboard will contain the following:
move /Y "C:\Code\Sample\SampleTest.Simple.received.txt" "C:\Code\Sample\SampleTest.Simple.verified.txt"
If a Diff Tool is detected it will display the diff:
To verify the result:
This will result in the SampleTest.Simple.verified.txt being created:
{
GivenNames: 'John',
FamilyName: 'Smith',
Spouse: 'Jill',
Address: {
Street: '4 Puddle Lane',
Country: 'USA'
},
Children: [
'Sam',
'Mary'
],
Id: Guid_1
}If the implementation of ClassBeingTested changes:
public static class ClassBeingTested
{
public static Person FindPerson()
{
return new Person
{
Id = new Guid("ebced679-45d3-4653-8791-3d969c4a986c"),
Title = Title.Mr,
// Middle name added
GivenNames = "John James",
FamilyName = "Smith",
Spouse = "Jill",
Children = new List<string>
{
"Sam",
"Mary"
},
Address = new Address
{
// Address changed
Street = "64 Barnett Street",
Country = "USA"
}
};
}
}And the test is re run it will fail with
Verification command has been copied to the clipboard.
Assert.Equal() Failure
↓ (pos 21)
Expected: ···\n GivenNames: 'John',\n FamilyName: 'Smith',\n Spouse: 'Jill···
Actual: ···\n GivenNames: 'John James',\n FamilyName: 'Smith',\n Spouse:···
↑ (pos 21)
The clipboard will again contain the following:
move /Y "C:\Code\Sample\SampleTest.Simple.received.txt" "C:\Code\Sample\SampleTest.Simple.verified.txt"
And the Diff Tool is will display the diff:
The same approach can be used to verify the results and the change to SampleTest.Simple.verified.txt is committed to source control along with the change to ClassBeingTested.
The clipboard behavior can be disable using the following:
var settings = new VerifySettings();
settings.DisableClipboard();SharedVerifySettings.DisableClipboard();If clipboard is disabled for all tests, it can be re-enabled at the test level:
var settings = new VerifySettings();
settings.EnableClipboard();Set a Verify.DisableClipboard environment variable to true. This overrides the above settings.
In some scenarios it makes sense to auto-accept any changes as part of a given test run. For example:
This can be done using AutoVerify():
var settings = new VerifySettings();
settings.AutoVerify();Note that auto accepted changes in .verified. files remain visible in source control tooling.
OnFirstVerify is called when there is no verified file.
OnVerifyMismatch is called when a received file does not match the existing verified file.
public async Task OnHandlersSample()
{
var settings = new VerifySettings();
settings.OnFirstVerify(
receivedFile =>
{
Debug.WriteLine(receivedFile);
return Task.CompletedTask;
});
settings.OnVerifyMismatch(
(receivedFile, verifiedFile, message) =>
{
Debug.WriteLine(receivedFile);
Debug.WriteLine(verifiedFile);
Debug.WriteLine(message);
return Task.CompletedTask;
});
await Verify("value", settings);
}Note that the output is technically not valid json. Single quotes are used and names are not quoted. The reason for this is to make the resulting output easier to read and understand.
Projects/tools that may be a better alternative to Verify
To report a security vulnerability, use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
Helmet designed by Leonidas Ikonomou from The Noun Project.
| Back | FazBrowse Home | New Git URL |