Git Product home page Git Product logo

easytestfile's Introduction

EasyTestFile

Nuget Status Nuget Status Nuget Status Nuget Status

EasyTestFile is a library that simplifies the creation and usage of testfiles in unittests. Testfiles (like text, json, xml, binary, jpg, etc. etc.) are named based on the class and method name, are created if not exist, and are embedded as resource making sure the execution of the test is deterministic and do not rely on untracked files etc.

At this moment, EasyTestFile can be used in combination with XUnit and NUnit.

Initial setup

Using EasyTestFile in XUnit requires an additional attribute.

[UsesEasyTestFile]
public class TestClass1
{
    // The attribute is required when using XUnit.
}

snippet source | anchor

No special attributes or configuration is required to use EasyTestFile in combination with NUnit.

API

[Fact]
public async Task LoadAsText()
{
    // Executing this test for the first time will create an empty testfile and throw an exception.
    // Executing this test for the second time, this statement will read the testfile
    // and returns the content as a string.
    string text = await EasyTestFile.LoadAsText();

    // and do whatever you want
}

snippet source | anchor

[Fact]
public async Task LoadAsStream()
{
    // You can also load the testfile content as a stream.
    Stream stream = await EasyTestFile.LoadAsStream();

}

snippet source | anchor

[Fact]
public async Task LoadAsTestFileWithJson()
{
    // You can also load the test file as a TestFile object.
    TestFile testFile = EasyTestFile.Load();

    // then you can load the content as a stream
    Stream stream = await testFile.AsStream();

    // or use extension methods like
    Person person = await testFile.AsObjectUsingNewtonsoft<Person>();

    // or like
    string text = await testFile.AsText();
}

snippet source | anchor

These three test methods produce the following testfiles according to the name convention {class name}.{method name}.testfile.{extension}

Solution Explorer TestFiles

Configuration

API configuration

Loading testfiles can be done with specific configuration using the EasyTestFileSettings.

var settings = new EasyTestFileSettings();

// specify assembly containing the testfiles (only applicable when embedded).
settings.UseAssembly(typeof(Person).Assembly);

// custom directory where testfiles are stored.
settings.UseDirectory("myTestFiles");

settings.UseFileName("filename");
// or
settings.UseMethodName("Configuration2");
settings.SetTestFileNameSuffix("suffix");

settings.UseExtension("jpg");
settings.DisableAutoCreateMissingTestFile();

// Load testfile as object with settings.
TestFile testFile = EasyTestFile.Load(settings);

// or directly as content with settings.
var text = await EasyTestFile.LoadAsText(settings);

snippet source | anchor

Compile time configuration

There is an optional option to control how testfiles are included in your artifacts. This can be controlled using the property EasyTestFileMode. The options are:

  • None TestFiles are not copied or embedded on compilation. EasyTestFile will load the files from the original source. This will speedup the compilation process but might be less reliable as files can be altered or deleted after compilation and before executing tests. Creating artifacts on buildservers in order to run the test in other environments might also be problematic as the testfiles are not included as artifact.
  • Embed The testfiles are embedded as resource in the dll file. This will produce larger binaries, takes a little bit more time to compile but makes the test deterministic as testfiles cannot be altered or deleted after compilation and before testing.
  • CopyAlways This will always copy the testfile to the artifact/build directory.
  • CopyPreserveNewest This will copy the testfile to the build directory when the file is newer.

When no (valid) value is provided, the Embed mode will be used.

Configuration is done like:

<PropertyGroup>
  <!-- Embed;CopyAlways;CopyPreserveNewest;None -->
  <EasyTestFileMode>CopyAlways</EasyTestFileMode>
</PropertyGroup>

snippet source | anchor

Credits

VerifyTest

Verify is a snapshot tool that simplifies the assertion of complex data models and documents. Some ideas and parts of the implementation in this project are based on the VerifyTest.

Icon

Photo designed by OCHA Visual from The Noun Project.

easytestfile's People

Contributors

actions-user avatar coenm avatar dependabot[bot] avatar

Stargazers

 avatar  avatar

Watchers

 avatar

easytestfile's Issues

xunit - Load from property fails

Only xunit.
Load data before test execution fails. For instance loading data from a property, within ctor, or within IAsyncLifetime.InitializeAsync()

Example usage doesn't work

I'm not sure what's going, but I have access to the attribute, but not the static class EasyTestFile.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.