diff --git a/GitHubActionsTestLogger.Tests/AnnotationFormatSpecs.cs b/GitHubActionsTestLogger.Tests/AnnotationFormatSpecs.cs deleted file mode 100644 index dc43f61..0000000 --- a/GitHubActionsTestLogger.Tests/AnnotationFormatSpecs.cs +++ /dev/null @@ -1,264 +0,0 @@ -using System.IO; -using FluentAssertions; -using GitHubActionsTestLogger.Tests.Utils; -using GitHubActionsTestLogger.Tests.Utils.Extensions; -using Microsoft.VisualStudio.TestPlatform.ObjectModel; -using Xunit; -using Xunit.Abstractions; - -namespace GitHubActionsTestLogger.Tests; - -public class AnnotationFormatSpecs -{ - private readonly ITestOutputHelper _testOutput; - - public AnnotationFormatSpecs(ITestOutputHelper testOutput) => - _testOutput = testOutput; - - [Fact] - public void Custom_format_can_reference_test_name() - { - // Arrange - using var commandWriter = new StringWriter(); - - var context = new TestLoggerContext( - new GitHubWorkflow( - commandWriter, - TextWriter.Null - ), - new TestLoggerOptions - { - AnnotationTitleFormat = "<$test>", - AnnotationMessageFormat = "[$test]" - } - ); - - // Act - context.SimulateTestRun( - new TestResultBuilder() - .SetDisplayName("Test1") - .SetOutcome(TestOutcome.Failed) - .Build() - ); - - // Assert - var output = commandWriter.ToString().Trim(); - - output.Should().Contain(""); - output.Should().Contain("[Test1]"); - - _testOutput.WriteLine(output); - } - - [Fact] - public void Custom_format_can_reference_test_traits() - { - // Arrange - using var commandWriter = new StringWriter(); - - var context = new TestLoggerContext( - new GitHubWorkflow( - commandWriter, - TextWriter.Null - ), - new TestLoggerOptions - { - AnnotationTitleFormat = "<$traits.Category -> $test>", - AnnotationMessageFormat = "[$traits.Category -> $test]" - } - ); - - // Act - context.SimulateTestRun( - new TestResultBuilder() - .SetDisplayName("Test1") - .SetTrait("Category", "UI Test") - .SetTrait("Document", "SS01") - .SetOutcome(TestOutcome.Failed) - .Build() - ); - - // Assert - var output = commandWriter.ToString().Trim(); - - output.Should().Contain(" Test1>"); - output.Should().Contain("[UI Test -> Test1]"); - - _testOutput.WriteLine(output); - } - - [Fact] - public void Custom_format_can_reference_test_error_message() - { - // Arrange - using var commandWriter = new StringWriter(); - - var context = new TestLoggerContext( - new GitHubWorkflow( - commandWriter, - TextWriter.Null - ), - new TestLoggerOptions - { - AnnotationTitleFormat = "<$test: $error>", - AnnotationMessageFormat = "[$test: $error]" - } - ); - - // Act - context.SimulateTestRun( - new TestResultBuilder() - .SetDisplayName("Test1") - .SetOutcome(TestOutcome.Failed) - .SetErrorMessage("ErrorMessage") - .Build() - ); - - // Assert - var output = commandWriter.ToString().Trim(); - - output.Should().Contain(""); - output.Should().Contain("[Test1: ErrorMessage]"); - - _testOutput.WriteLine(output); - } - - [Fact] - public void Custom_format_can_reference_test_error_stacktrace() - { - // Arrange - using var commandWriter = new StringWriter(); - - var context = new TestLoggerContext( - new GitHubWorkflow( - commandWriter, - TextWriter.Null - ), - new TestLoggerOptions - { - AnnotationTitleFormat = "<$test: $trace>", - AnnotationMessageFormat = "[$test: $trace]" - } - ); - - // Act - context.SimulateTestRun( - new TestResultBuilder() - .SetDisplayName("Test1") - .SetOutcome(TestOutcome.Failed) - .SetErrorStackTrace("ErrorStackTrace") - .Build() - ); - - // Assert - var output = commandWriter.ToString().Trim(); - - output.Should().Contain(""); - output.Should().Contain("[Test1: ErrorStackTrace]"); - - _testOutput.WriteLine(output); - } - - [Fact] - public void Custom_format_can_reference_test_target_framework_name() - { - // Arrange - using var commandWriter = new StringWriter(); - - var context = new TestLoggerContext( - new GitHubWorkflow( - commandWriter, - TextWriter.Null - ), - new TestLoggerOptions - { - AnnotationTitleFormat = "<$test ($framework)>", - AnnotationMessageFormat = "[$test ($framework)]" - } - ); - - // Act - context.SimulateTestRun( - "FakeTests.dll", - "FakeTargetFramework", - new TestResultBuilder() - .SetDisplayName("Test1") - .SetOutcome(TestOutcome.Failed) - .SetErrorStackTrace("ErrorStackTrace") - .Build() - ); - - // Assert - var output = commandWriter.ToString().Trim(); - - output.Should().Contain(""); - output.Should().Contain("[Test1 (FakeTargetFramework)]"); - - _testOutput.WriteLine(output); - } - - [Fact] - public void Custom_format_can_contain_newlines() - { - // Arrange - using var commandWriter = new StringWriter(); - - var context = new TestLoggerContext( - new GitHubWorkflow( - commandWriter, - TextWriter.Null - ), - new TestLoggerOptions - { - AnnotationMessageFormat = "foo\\nbar" - } - ); - - // Act - context.SimulateTestRun( - new TestResultBuilder() - .SetDisplayName("Test1") - .SetOutcome(TestOutcome.Failed) - .Build() - ); - - // Assert - var output = commandWriter.ToString().Trim(); - - output.Should().Contain("foo%0Abar"); - - _testOutput.WriteLine(output); - } - - [Fact] - public void Default_format_references_test_name_and_error_message() - { - // Arrange - using var commandWriter = new StringWriter(); - - var context = new TestLoggerContext( - new GitHubWorkflow( - commandWriter, - TextWriter.Null - ), - TestLoggerOptions.Default - ); - - // Act - context.SimulateTestRun( - new TestResultBuilder() - .SetDisplayName("Test1") - .SetOutcome(TestOutcome.Failed) - .SetErrorMessage("ErrorMessage") - .Build() - ); - - // Assert - var output = commandWriter.ToString().Trim(); - - output.Should().Contain("Test1"); - output.Should().Contain("ErrorMessage"); - - _testOutput.WriteLine(output); - } -} \ No newline at end of file diff --git a/GitHubActionsTestLogger.Tests/AnnotationSpecs.cs b/GitHubActionsTestLogger.Tests/AnnotationSpecs.cs index cde0bfa..8f36bdc 100644 --- a/GitHubActionsTestLogger.Tests/AnnotationSpecs.cs +++ b/GitHubActionsTestLogger.Tests/AnnotationSpecs.cs @@ -17,7 +17,7 @@ public AnnotationSpecs(ITestOutputHelper testOutput) => _testOutput = testOutput; [Fact] - public void Passed_tests_do_not_get_reported() + public void I_can_use_the_logger_to_produce_annotations_for_failed_tests() { // Arrange using var commandWriter = new StringWriter(); @@ -34,34 +34,15 @@ public void Passed_tests_do_not_get_reported() context.SimulateTestRun( new TestResultBuilder() .SetDisplayName("Test1") + .SetOutcome(TestOutcome.Failed) + .SetErrorMessage("ErrorMessage") + .Build(), + new TestResultBuilder() + .SetDisplayName("Test2") .SetOutcome(TestOutcome.Passed) - .Build() - ); - - // Assert - var output = commandWriter.ToString().Trim(); - - output.Should().BeEmpty(); - } - - [Fact] - public void Skipped_tests_do_not_get_reported() - { - // Arrange - using var commandWriter = new StringWriter(); - - var context = new TestLoggerContext( - new GitHubWorkflow( - commandWriter, - TextWriter.Null - ), - TestLoggerOptions.Default - ); - - // Act - context.SimulateTestRun( + .Build(), new TestResultBuilder() - .SetDisplayName("Test1") + .SetDisplayName("Test3") .SetOutcome(TestOutcome.Skipped) .Build() ); @@ -69,44 +50,18 @@ public void Skipped_tests_do_not_get_reported() // Assert var output = commandWriter.ToString().Trim(); - output.Should().BeEmpty(); - } - - [Fact] - public void Failed_tests_get_reported() - { - // Arrange - using var commandWriter = new StringWriter(); - - var context = new TestLoggerContext( - new GitHubWorkflow( - commandWriter, - TextWriter.Null - ), - TestLoggerOptions.Default - ); - - // Act - context.SimulateTestRun( - new TestResultBuilder() - .SetDisplayName("Test1") - .SetOutcome(TestOutcome.Failed) - .SetErrorMessage("ErrorMessage") - .Build() - ); - - // Assert - var output = commandWriter.ToString().Trim(); - output.Should().StartWith("::error "); output.Should().Contain("Test1"); output.Should().Contain("ErrorMessage"); + output.Should().NotContain("Test2"); + output.Should().NotContain("Test3"); + _testOutput.WriteLine(output); } [Fact] - public void Failed_tests_get_reported_with_source_information_if_exception_was_thrown() + public void I_can_use_the_logger_to_produce_annotations_that_include_source_information() { // .NET test platform never sends source information, so we can only // rely on exception stack traces to get it. @@ -159,7 +114,7 @@ at CliWrap.Tests.CancellationSpecs.I_can_execute_a_command_with_buffering_and_ca } [Fact] - public void Failed_tests_get_reported_with_source_information_if_exception_was_thrown_in_an_async_method() + public void I_can_use_the_logger_to_produce_annotations_that_include_source_information_for_async_tests() { // .NET test platform never sends source information, so we can only // rely on exception stack traces to get it. @@ -216,7 +171,7 @@ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotifi } [Fact] - public void Failed_tests_get_reported_with_approximate_source_information_if_exception_was_not_thrown() + public void I_can_use_the_logger_to_produce_annotations_that_include_approximate_source_information_as_fallback() { // Arrange using var commandWriter = new StringWriter(); @@ -249,4 +204,219 @@ public void Failed_tests_get_reported_with_approximate_source_information_if_exc _testOutput.WriteLine(output); } + + [Fact] + public void I_can_use_the_logger_to_produce_annotations_that_include_the_test_name() + { + // Arrange + using var commandWriter = new StringWriter(); + + var context = new TestLoggerContext( + new GitHubWorkflow( + commandWriter, + TextWriter.Null + ), + new TestLoggerOptions + { + AnnotationTitleFormat = "<$test>", + AnnotationMessageFormat = "[$test]" + } + ); + + // Act + context.SimulateTestRun( + new TestResultBuilder() + .SetDisplayName("Test1") + .SetOutcome(TestOutcome.Failed) + .Build() + ); + + // Assert + var output = commandWriter.ToString().Trim(); + + output.Should().Contain(""); + output.Should().Contain("[Test1]"); + + _testOutput.WriteLine(output); + } + + [Fact] + public void I_can_use_the_logger_to_produce_annotations_that_include_test_traits() + { + // Arrange + using var commandWriter = new StringWriter(); + + var context = new TestLoggerContext( + new GitHubWorkflow( + commandWriter, + TextWriter.Null + ), + new TestLoggerOptions + { + AnnotationTitleFormat = "<$traits.Category -> $test>", + AnnotationMessageFormat = "[$traits.Category -> $test]" + } + ); + + // Act + context.SimulateTestRun( + new TestResultBuilder() + .SetDisplayName("Test1") + .SetTrait("Category", "UI Test") + .SetTrait("Document", "SS01") + .SetOutcome(TestOutcome.Failed) + .Build() + ); + + // Assert + var output = commandWriter.ToString().Trim(); + + output.Should().Contain(" Test1>"); + output.Should().Contain("[UI Test -> Test1]"); + + _testOutput.WriteLine(output); + } + + [Fact] + public void I_can_use_the_logger_to_produce_annotations_that_include_the_error_message() + { + // Arrange + using var commandWriter = new StringWriter(); + + var context = new TestLoggerContext( + new GitHubWorkflow( + commandWriter, + TextWriter.Null + ), + new TestLoggerOptions + { + AnnotationTitleFormat = "<$test: $error>", + AnnotationMessageFormat = "[$test: $error]" + } + ); + + // Act + context.SimulateTestRun( + new TestResultBuilder() + .SetDisplayName("Test1") + .SetOutcome(TestOutcome.Failed) + .SetErrorMessage("ErrorMessage") + .Build() + ); + + // Assert + var output = commandWriter.ToString().Trim(); + + output.Should().Contain(""); + output.Should().Contain("[Test1: ErrorMessage]"); + + _testOutput.WriteLine(output); + } + + [Fact] + public void I_can_use_the_logger_to_produce_annotations_that_include_the_error_stacktrace() + { + // Arrange + using var commandWriter = new StringWriter(); + + var context = new TestLoggerContext( + new GitHubWorkflow( + commandWriter, + TextWriter.Null + ), + new TestLoggerOptions + { + AnnotationTitleFormat = "<$test: $trace>", + AnnotationMessageFormat = "[$test: $trace]" + } + ); + + // Act + context.SimulateTestRun( + new TestResultBuilder() + .SetDisplayName("Test1") + .SetOutcome(TestOutcome.Failed) + .SetErrorStackTrace("ErrorStackTrace") + .Build() + ); + + // Assert + var output = commandWriter.ToString().Trim(); + + output.Should().Contain(""); + output.Should().Contain("[Test1: ErrorStackTrace]"); + + _testOutput.WriteLine(output); + } + + [Fact] + public void I_can_use_the_logger_to_produce_annotations_that_include_the_target_framework_version() + { + // Arrange + using var commandWriter = new StringWriter(); + + var context = new TestLoggerContext( + new GitHubWorkflow( + commandWriter, + TextWriter.Null + ), + new TestLoggerOptions + { + AnnotationTitleFormat = "<$test ($framework)>", + AnnotationMessageFormat = "[$test ($framework)]" + } + ); + + // Act + context.SimulateTestRun( + "FakeTests.dll", + "FakeTargetFramework", + new TestResultBuilder() + .SetDisplayName("Test1") + .SetOutcome(TestOutcome.Failed) + .SetErrorStackTrace("ErrorStackTrace") + .Build() + ); + + // Assert + var output = commandWriter.ToString().Trim(); + + output.Should().Contain(""); + output.Should().Contain("[Test1 (FakeTargetFramework)]"); + + _testOutput.WriteLine(output); + } + + [Fact] + public void I_can_use_the_logger_to_produce_annotations_that_include_line_breaks() + { + // Arrange + using var commandWriter = new StringWriter(); + + var context = new TestLoggerContext( + new GitHubWorkflow( + commandWriter, + TextWriter.Null + ), + new TestLoggerOptions + { + AnnotationMessageFormat = "foo\\nbar" + } + ); + + // Act + context.SimulateTestRun( + new TestResultBuilder() + .SetDisplayName("Test1") + .SetOutcome(TestOutcome.Failed) + .Build() + ); + + // Assert + var output = commandWriter.ToString().Trim(); + + output.Should().Contain("foo%0Abar"); + + _testOutput.WriteLine(output); + } } \ No newline at end of file diff --git a/GitHubActionsTestLogger.Tests/InitializationSpecs.cs b/GitHubActionsTestLogger.Tests/InitializationSpecs.cs index 2322ba0..fa80f00 100644 --- a/GitHubActionsTestLogger.Tests/InitializationSpecs.cs +++ b/GitHubActionsTestLogger.Tests/InitializationSpecs.cs @@ -9,7 +9,7 @@ namespace GitHubActionsTestLogger.Tests; public class InitializationSpecs { [Fact] - public void Logger_can_be_used_with_default_configuration() + public void I_can_use_the_logger_with_the_default_configuration() { // Arrange var logger = new TestLogger(); @@ -24,7 +24,7 @@ public void Logger_can_be_used_with_default_configuration() } [Fact] - public void Logger_can_be_used_with_custom_configuration() + public void I_can_use_the_logger_with_a_custom_configuration() { // Arrange var logger = new TestLogger(); diff --git a/GitHubActionsTestLogger.Tests/SummarySpecs.cs b/GitHubActionsTestLogger.Tests/SummarySpecs.cs index ed44101..4cbf9e8 100644 --- a/GitHubActionsTestLogger.Tests/SummarySpecs.cs +++ b/GitHubActionsTestLogger.Tests/SummarySpecs.cs @@ -16,7 +16,7 @@ public SummarySpecs(ITestOutputHelper testOutput) => _testOutput = testOutput; [Fact] - public void Test_summary_contains_test_suite_name() + public void I_can_use_the_logger_to_produce_a_summary_that_includes_the_test_suite_name() { // Arrange using var summaryWriter = new StringWriter(); @@ -41,7 +41,7 @@ public void Test_summary_contains_test_suite_name() } [Fact] - public void Test_summary_contains_names_of_passed_tests_if_enabled() + public void I_can_use_the_logger_to_produce_a_summary_that_includes_the_list_of_passed_tests() { // Arrange using var summaryWriter = new StringWriter(); @@ -94,7 +94,7 @@ public void Test_summary_contains_names_of_passed_tests_if_enabled() } [Fact] - public void Test_summary_contains_names_of_skipped_tests_if_enabled() + public void I_can_use_the_logger_to_produce_a_summary_that_includes_the_list_of_skipped_tests() { // Arrange using var summaryWriter = new StringWriter(); @@ -147,7 +147,7 @@ public void Test_summary_contains_names_of_skipped_tests_if_enabled() } [Fact] - public void Test_summary_contains_names_of_failed_tests() + public void I_can_use_the_logger_to_produce_a_summary_that_includes_the_list_of_failed_tests() { // Arrange using var summaryWriter = new StringWriter(); @@ -196,67 +196,15 @@ public void Test_summary_contains_names_of_failed_tests() var output = summaryWriter.ToString().Trim(); output.Should().Contain("Test1"); + output.Should().Contain("ErrorMessage1"); output.Should().Contain("Test2"); + output.Should().Contain("ErrorMessage2"); output.Should().Contain("Test3"); + output.Should().Contain("ErrorMessage3"); + output.Should().NotContain("Test4"); output.Should().NotContain("Test5"); _testOutput.WriteLine(output); } - - [Fact] - public void Test_summary_contains_error_messages_of_failed_tests() - { - // Arrange - using var summaryWriter = new StringWriter(); - - var context = new TestLoggerContext( - new GitHubWorkflow( - TextWriter.Null, - summaryWriter - ), - TestLoggerOptions.Default - ); - - // Act - context.SimulateTestRun( - new TestResultBuilder() - .SetDisplayName("Test1") - .SetFullyQualifiedName("TestProject.SomeTests.Test1") - .SetOutcome(TestOutcome.Failed) - .SetErrorMessage("ErrorMessage1") - .Build(), - new TestResultBuilder() - .SetDisplayName("Test2") - .SetFullyQualifiedName("TestProject.SomeTests.Test2") - .SetOutcome(TestOutcome.Failed) - .SetErrorMessage("ErrorMessage2") - .Build(), - new TestResultBuilder() - .SetDisplayName("Test3") - .SetFullyQualifiedName("TestProject.SomeTests.Test3") - .SetOutcome(TestOutcome.Failed) - .SetErrorMessage("ErrorMessage3") - .Build(), - new TestResultBuilder() - .SetDisplayName("Test4") - .SetFullyQualifiedName("TestProject.SomeTests.Test4") - .SetOutcome(TestOutcome.Passed) - .Build(), - new TestResultBuilder() - .SetDisplayName("Test5") - .SetFullyQualifiedName("TestProject.SomeTests.Test5") - .SetOutcome(TestOutcome.Skipped) - .Build() - ); - - // Assert - var output = summaryWriter.ToString().Trim(); - - output.Should().Contain("ErrorMessage1"); - output.Should().Contain("ErrorMessage2"); - output.Should().Contain("ErrorMessage3"); - - _testOutput.WriteLine(output); - } } \ No newline at end of file