TestAttachments.NUnit
3.0.0-Preview-20230511-1511
See the version list below for details.
dotnet add package TestAttachments.NUnit --version 3.0.0-Preview-20230511-1511
NuGet\Install-Package TestAttachments.NUnit -Version 3.0.0-Preview-20230511-1511
<PackageReference Include="TestAttachments.NUnit" Version="3.0.0-Preview-20230511-1511" />
paket add TestAttachments.NUnit --version 3.0.0-Preview-20230511-1511
#r "nuget: TestAttachments.NUnit, 3.0.0-Preview-20230511-1511"
// Install TestAttachments.NUnit as a Cake Addin #addin nuget:?package=TestAttachments.NUnit&version=3.0.0-Preview-20230511-1511&prerelease // Install TestAttachments.NUnit as a Cake Tool #tool nuget:?package=TestAttachments.NUnit&version=3.0.0-Preview-20230511-1511&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 |
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);
}
}
}
Related issues
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. 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. 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 is compatible. |
.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. |
-
.NETStandard 2.0
- NUnit (>= 3.13.3)
- securifybv.ShellLink (>= 0.1.0)
- System.Memory (>= 4.5.5)
- System.Text.Json (>= 7.0.2)
-
.NETStandard 2.1
- NUnit (>= 3.13.3)
- securifybv.ShellLink (>= 0.1.0)
- System.Text.Json (>= 7.0.2)
-
net6.0
- NUnit (>= 3.13.3)
- securifybv.ShellLink (>= 0.1.0)
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-20241018-1255 | 106 | 10/18/2024 |
4.0.0-Preview-20240102-0955 | 2,378 | 1/2/2024 |
4.0.0-Preview-20231128-1207 | 821 | 11/28/2023 |
3.0.0-Preview-20230511-1511 | 359 | 5/11/2023 |