Skip to content
forked from qoter/EasyTest

A set of tools that helps write tests based on directories

Notifications You must be signed in to change notification settings

artemworld/EasyTest

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EasyTest

Lightweight tools that helps write tests based on directories

Statuses

Build status Nuget version

Usage

Content loading

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);

Content verifying

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));

Snapshot testing

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.

Exception message with clickable commands

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.

Code examples

See example ContentLoader and ContentVerifier in code

See example Snapshoter in code

About

A set of tools that helps write tests based on directories

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%