Hallsoft.TestHelpers.Net 1.0.0

Requires NuGet 2.12 or higher.

dotnet add package Hallsoft.TestHelpers.Net --version 1.0.0
NuGet\Install-Package Hallsoft.TestHelpers.Net -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hallsoft.TestHelpers.Net" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hallsoft.TestHelpers.Net --version 1.0.0
#r "nuget: Hallsoft.TestHelpers.Net, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hallsoft.TestHelpers.Net as a Cake Addin
#addin nuget:?package=Hallsoft.TestHelpers.Net&version=1.0.0

// Install Hallsoft.TestHelpers.Net as a Cake Tool
#tool nuget:?package=Hallsoft.TestHelpers.Net&version=1.0.0

TestHelpers.Net

This library contains helper methods for opening test data files inside test projects.

I find myself constantly copying and pasting a few common helper methods from old projects to make this work across machines when using Live Unit Testing. Below is a short overview of the basic concepts of the library. For more complete usage, browse the various test projects contained in the "test" folder that show the complete use cases (test\TestHelpers.NetCoreTests.xUnit has the most complete set of set cases).

Usage

Instantiate a new instance of the TestHelper class and then open input files relative to the test project's directory using the OpenFile method.

using Hallsoft.TestHelpers;

public void TestMethod()
{
    const string Expected = "Hello World!";
    TestFileHelper testHelper = new TestFileHelper();

    string contents;
    //Opens the file "test.txt" in the "TestInput" sub folder of the current test project
    using (StreamReader reader = testHelper.OpenFile(@"TestInput\test.txt"))
    {
        contents = reader.ReadToEnd();
    }

    Assert.Equal(Expected, contents);
}

Creating streams from string input

Another operation I find myself wanting to do is to manipulate the content of test input files before passing that to the code I'm testing (e.g. to test error conditions of incorrectly formatted input). To to this, use the "TestInputHelper.CreateStreamReader()" method.

const string InputContent = "Hello world!";
string actualContents;
using(StreamReader reader = TestInputHelper.CreateStreamReader(InputContent))
{
    actualContents = reader.ReadToEnd();
}

Assert.Equal(InputContent, actualContents);

Working with Live Unit Testing

Visual Studio's Live Unit Testing runs the test project outside the normal project build structure. By default this library assumes the test project folder matches the output assembly name of the test project.

  • If that is true, opening files should just work with no action required.
  • If the assembly name is not the same as the project folder name, you will need to manually specify the name of the project folder.

To manually specify the name of the project file create an instance of the TestHelperConfiguration class and set the CurrentProjectFolderName property, then pass the instance into the TestHelper constructor.

TestFileHelperConfiguration config = new TestFileHelperConfiguration
{
    CurrentProjectFolderName = "ThisProjectFolderName"
};

TestFileHelper helper = new TestFileHelper(config);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 1,945 2/3/2019
1.0.0-rc4 452 2/3/2019
1.0.0-rc3 688 1/2/2019
1.0.0-rc2 744 1/2/2019
1.0.0-rc 667 1/2/2019

Bug fixes