Lightweight tools that helps write tests based on directories
Define TestContent
class
public class MyTestContent : TestContent
{
[FileContent("document.xml")]
public XDocument Xml { get; private set; }
[FileContent("expected.txt")]
public string Expected { get; private set; }
}
Use ContentLoader
with necessary deserializers
using var content = ContentLoader
.For<MyTestContent>()
.WithDeserializer(XDocument.Load)
.WithDeserializer(s => s.ReadString())
.LoadFromDirectory("/path/to/directory/with/files");
Now you can use content for asserts
var actual = MakeMagic(content.Xml);
Assert.AreEqual(content.Expected, actual);
You can save expected
and actual
in files with ContentVerifier
(like Approval Tests)
ContentVerifier
.UseDirectory(testDirectory)
.SaveActualAs("actual.txt", s => s.WriteString(actual))
.ReadExpectedAs("expected.txt", s => s.ReadString())
.Verify(expected => Assert.Equal(actual, expected));
Just create directory __snapshots__
in root of your project, then call MatchSnapshot
inside your test method:
Snapshoter.MatchSnapshot(actualString);
Snapshoter save snapshots as __snapshots__/<caller method file name>/<caller method name>.snap
.
We use [CallerMemberName] and [CallerFilePath] attributes to resove path to snapshots directory and it's nessery to call MatchSnapshot
strictly inside your test method.
When a test failed we throwing exception with special message that helps investigate or fix tests.
For exmaple Snapshoter throws exception with messages:
Snapshot mismatch:
Expected: some expected text\n\r\t\0
Actual: somb actual text\n\r\t\0
↑ [3]
Available commands:
<view diff> $(rider) diff "/path/to/mismatch.snap" "/path/to/snapshot.snap"
<accept diff> $(term) move /Y "/path/to/mismatch.snap" "/path/to/snapshot.snap"
<accept ALL diffs> $(term) move /Y "/path/to/mismatch\*" "/path/to/snapshots" & rmdir /S /Q "/path/to/mismatch"
To make Available commands
work straight from console you need to use JetBrains Rider IDE with ClickableConsole plugin.
But also you can use this commands via copy-paste to console.