TestAttachments.NUnit 4.0.0-Preview-20240102-0955

This is a prerelease version of TestAttachments.NUnit.
dotnet add package TestAttachments.NUnit --version 4.0.0-Preview-20240102-0955
NuGet\Install-Package TestAttachments.NUnit -Version 4.0.0-Preview-20240102-0955
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="TestAttachments.NUnit" Version="4.0.0-Preview-20240102-0955" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TestAttachments.NUnit --version 4.0.0-Preview-20240102-0955
#r "nuget: TestAttachments.NUnit, 4.0.0-Preview-20240102-0955"
#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 TestAttachments.NUnit as a Cake Addin
#addin nuget:?package=TestAttachments.NUnit&version=4.0.0-Preview-20240102-0955&prerelease

// Install TestAttachments.NUnit as a Cake Tool
#tool nuget:?package=TestAttachments.NUnit&version=4.0.0-Preview-20240102-0955&prerelease

Overview

When writing tests that depend heavily on resources and attachment files, it is usually convenient to have an easy way to retrieve the resources, and also to have a way to organize the attached files based on the test IDs.

NUnit provides the properties needed to build per test paths in the TestContext object, but the rest is up to the developer.

This library provides a last mile API, easing the process of working with resources and file attachments.

Usage

The library defines two attributes

  • ResourcePathFormat(string format)
  • AttachmentPathFormat(string format)

Which can be used in assemblies, classes and individual methods.

The format defines how the test output directory will be created for the given test, by using a number of macros:

Predefined absolute path macross:

macro value
WorkDirectory or * context.WorkDirectory
TestDirectory context.TestDirectory
TempDirectory System.IO.Path.GetTempPath()
CurrentDirectory Environment.CurrentDirectory
SolutionDirectory finds the top most solution (.sln) file and takes its directory

predefined relative path macross:

macro value
ID or ? context.Test.ID
Name context.Test.Name
FullName context.Test.FullName
ClassName context.Test.ClassName
MethodName context.Test.MethodName
CurrentRepeatCount context.CurrentRepeatCount
WorkerId context.WorkerId
Date DateTime.Now 'yyyMMdd'
Time DateTime.Now 'hhmmss'

By using these macros, we can define the attribute like this:

[AttachmentPathFormat("{WorkDirectory}/{ID}/{Date}")]

when running a test, it will replace the macros with the appropiate values taken from the current test context.

the default and most common use will be

[AttachmentPathFormat("?")]

which creates a directory for every test.

Example


using NUnit.Framework;

[assembly: ResourcePathFormat("{TestDirectory}/Resources")]
[assembly: AttachmentPathFormat("{WorkDirectory}/TestResults/{ID}")]

namespace TestNamespace
{
    [AttachmentPathFormat("{WorkDirectory}/{ID}")] // Alternatively: [AttachmentPathFormat("?")]
    public class TestClass
    {
        [Test]
        public void WriteTextAttachment()        
        {
            var text = ResourceInfo
                .From("hello.txt")
                .ReadAllText();

                text += " - good bye!";

            AttachmentInfo
                .From("result.txt")
                .WriteAllText(text);            
        }

        [Test]
        [AttachmentPathFormat("{WorkDirectory}/ExplicitMethodResult-{Date}-{Time}")]
        public void WriteExplicitTextAttachment()
        {
            var text = ResourceInfo
                .From("hello.txt")
                .ReadAllText();

            text += " - good bye!";

            AttachmentInfo
                .From("result.txt")
                .WriteAllText(text);     
        }
    }
}

Nunit Proposal

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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.  net6.0-windows7.0 is compatible.  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 Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 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 (1)

Showing the top 1 popular GitHub repositories that depend on TestAttachments.NUnit:

Repository Stars
vpenades/SharpGLTF
glTF reader and writer for .NET Standard
Version Downloads Last updated
4.0.0-Preview-20240102-0955 1,134 1/2/2024
4.0.0-Preview-20231128-1207 799 11/28/2023
3.0.0-Preview-20230511-1511 344 5/11/2023