Skip to content

TestSources makes easy to handle, organize and use test files from within a .NET test.

License

Notifications You must be signed in to change notification settings

SwissLife-OSS/testsources

Repository files navigation

TestSources

Nuget GitHub Release Build Status Coverage Status Quality

TestSources is a test file management tool for .NET Core and .NET Framework

TestSources makes easy to handle, organize and use test files in a .NET test. It greatly simplifies the usual work of having to set up a file management tool/code, implement validations and enable proper access to files and its contents... Those have to be set up manually on every test project that manages files of any kind, consuming time and repeating code. THAT IS EVIL. But no more, here is TestSources to help.

Getting Started

To get started, install the TestSources nuget package:

In your favorite test tool, XUnit, NUnit or MSTest

dotnet add package TestSources

Setting up TestSources

In the root of the project, add a New Folder, named "__testsources__". It should be in lowercase, with two underscore characters "_" before and after. Inside our tessources folder, create a text file named "aTextFile.txt" and inside add "Some text", or any text of your liking.

Using TestSources to get a file

To get this test file and use it within your unit (or integration) tests, follow the next step:

1. Get the file

Insert a TestSources GetFile statement TestSource.GetFile("aTextFile.txt"); into your unit test.

Example:

/// <summary>
/// Tests if the test file contains some text.
/// </summary>
[Fact]
public void TestThatTheTextFileContainsText()
{
    // arrange
    string fileName = "aTextFile.txt";

    // act
    string textInsideTheFile =
        TestSource.GetFile(fileName)
        .AsString();

    // Assert
    Assert.NotEmpty(textInsideTheFile);
}

2. Run the unit test to Assert that the file is not empty

The TestSource.GetFile() statement will obtain the file with the specified name located on the "__testsources__" folder. Next, the metachained AsString() statement will obtain its contents and deliver them ready to use in a string form, with UTF-8 encoding. (it is configurable too)

It is herarchical (files and folders)

You can create folders inside the "__testsources__" folder, without any limit except the one set up by your operating system.

Same goes with files, you can add them at any level and use your imagination to organize your test files until it pleases you.

The statement TestSource.GetFile(filename, true) will get any file located under the testsources root directory, given that it exists and you have typed it properly.

In addition you can get a reference to a folder with a similar statement:

TestSource.GetFolder(foldername) will find the folder named foldername in the root of the testsources root directory, whereas TestSource.GetFile(filename, true) will find the named folder in any of its subfolders.

Managing files: ITestSourceFile

The files fulfill the ITestSourceFile Interface and this enables us to check its parent, get the file name, the full name including the path and then some extensions such as:

  • OpenRead() - Opens a file for reading returning a file stream.
  • AsString() - Reads the current file and returns its content as an string with a default UTF8 encoding, which can be overridden.
  • AsByteArray() - Reads the current file and returns a byte array of its content.
  • AsFileStream() - Reads the current file and returns a FileStream to it.
  • AsMemoryStream() - Reads the current file and returns its contents as a MemoryStream.
  • AsStream() - Reads the current file and returns its contents as a Stream.
  • GetHash() - Returns the hash of a file, given a Cryptographic hash algorithm.
  • AsType<T>() - Returns the content of a file as a concrete type, deserializing its JSON content.
  • AsJson() - Reads the current file and returns its content as a JSON string with a default UTF8 encoding, which can be overridden.

Managing folders: ITestSourceDir

The folders or directories fulfill the ITestSourceDir Interface and this enables us to check its parent, get the folder name, the full name including the path and then some extensions such as:

  • GetFiles() - Returns a list of the files contained on this folder. Of course, fulfilling the ITestSourceFile interface.
  • GetFolders() - Returns a list of the folders contained on this folder. Of course, fulfilling the ITestSourceDir interface. (if no files or folders, the collection is empty)

Using testsources in CI-Builds

They simply work, nothing specially to set up there.

Community

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information, see the Swiss Life OSS Code of Conduct.