add support for nunit attachments (#1192)

This commit is contained in:
Simon Cropp 2024-04-13 10:41:15 +10:00 коммит произвёл GitHub
Родитель b86eb10237
Коммит b76e3c55f8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
4 изменённых файлов: 90 добавлений и 1 удалений

1
.gitignore поставляемый
Просмотреть файл

@ -18,3 +18,4 @@ src/Verify.Tests/Tests.TextNegative.verified.tmp
*.cache
src/Verify.Tests/NewLineTests.StringWithDifferingNewline.verified.txt
src/Verify.MSTest.Tests/Tests.AutoVerifyHasAttachment.verified.txt
src/Verify.NUnit.Tests/Tests.AutoVerifyHasAttachment.verified.txt

Просмотреть файл

@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters</NoWarn>
<Version>24.0.0</Version>
<Version>24.1.0</Version>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>

Просмотреть файл

@ -88,4 +88,80 @@ public class Tests
VerifyZip(pathToArchive);
#endregion
static List<TestAttachment> GetAttachments() =>
TestExecutionContext.CurrentContext.CurrentResult.TestAttachments.ToList();
[Test]
public void ChangeHasAttachment()
{
var settings = new VerifySettings();
settings.DisableDiff();
ThrowsAsync<VerifyException>(
() => Verify("Bar", settings));
var list = GetAttachments();
AreEqual(1, list.Count);
var file = Path.GetFileName(list[0].FilePath);
AreEqual($"Tests.ChangeHasAttachment.{Namer.TargetFrameworkNameAndVersion}.received.txt", file);
}
[Test]
public async Task AutoVerifyHasAttachment()
{
var path = CurrentFile.Relative("Tests.AutoVerifyHasAttachment.verified.txt");
var fullPath = Path.GetFullPath(path);
File.Delete(fullPath);
File.WriteAllText(fullPath,"Foo");
var settings = new VerifySettings();
settings.DisableDiff();
settings.AutoVerify();
await Verify("Bar", settings);
var list = GetAttachments();
AreEqual(1, list.Count);
var file = Path.GetFileName(list[0].FilePath);
AreEqual($"Tests.AutoVerifyHasAttachment.{Namer.TargetFrameworkNameAndVersion}.received.txt", file);
}
[Test]
public void NewHasAttachment()
{
var settings = new VerifySettings();
settings.DisableDiff();
ThrowsAsync<VerifyException>(
() => Verify("Bar", settings));
var list = GetAttachments();
AreEqual(1, list.Count);
var file = Path.GetFileName(list[0].FilePath);
AreEqual($"Tests.NewHasAttachment.{Namer.TargetFrameworkNameAndVersion}.received.txt", file);
}
[Test]
public void MultipleChangedHasAttachment()
{
var settings = new VerifySettings();
settings.DisableDiff();
ThrowsAsync<VerifyException>(
() => Verify("Bar", [new Target("txt", "Value")], settings));
var list = GetAttachments();
AreEqual(2, list.Count);
var file0 = Path.GetFileName(list[0].FilePath);
var file1 = Path.GetFileName(list[1].FilePath);
AreEqual($"Tests.MultipleChangedHasAttachment.{Namer.TargetFrameworkNameAndVersion}#00.received.txt", file0);
AreEqual($"Tests.MultipleChangedHasAttachment.{Namer.TargetFrameworkNameAndVersion}#01.received.txt", file1);
}
[Test]
public void MultipleNewHasAttachment()
{
var settings = new VerifySettings();
settings.DisableDiff();
ThrowsAsync<VerifyException>(
() => Verify("Bar",[new Target("txt", "Value")], settings));
var list = GetAttachments();
AreEqual(2, list.Count);
var file0 = Path.GetFileName(list[0].FilePath);
var file1 = Path.GetFileName(list[1].FilePath);
AreEqual($"Tests.MultipleNewHasAttachment.{Namer.TargetFrameworkNameAndVersion}#00.received.txt", file0);
AreEqual($"Tests.MultipleNewHasAttachment.{Namer.TargetFrameworkNameAndVersion}#01.received.txt", file1);
}
}

Просмотреть файл

@ -2,6 +2,18 @@
public static partial class Verifier
{
static Task AddFile(FilePair path)
{
TestContext.AddTestAttachment(path.ReceivedPath);
return Task.CompletedTask;
}
static Verifier()
{
VerifierSettings.OnFirstVerify((pair, _, _) => AddFile(pair));
VerifierSettings.OnVerifyMismatch((pair, _, _) => AddFile(pair));
}
static InnerVerifier BuildVerifier(string sourceFile, VerifySettings settings, bool useUniqueDirectory)
{
Guard.AgainstBadSourceFile(sourceFile);