-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #272 from thePantz/feature/xunit-fixture-logging
xUnit fixture logging
- Loading branch information
Showing
10 changed files
with
277 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
Boa.Constrictor.Xunit.UnitTests/Logging/Loggers/ConcreteMessageSink.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Boa.Constrictor.Xunit.UnitTests; | ||
|
||
using global::Xunit.Abstractions; | ||
using global::Xunit.Sdk; | ||
|
||
/// <summary> | ||
/// A test double that enables reading the output of a message sink | ||
/// </summary> | ||
public class ConcreteMessageSink : IMessageSink | ||
{ | ||
public bool OnMessage(IMessageSinkMessage message) | ||
{ | ||
var diagnosticMessage = (DiagnosticMessage)message; | ||
LastMessage = diagnosticMessage.Message; | ||
return true; | ||
} | ||
|
||
public string LastMessage { get; set; } | ||
} |
98 changes: 98 additions & 0 deletions
98
Boa.Constrictor.Xunit.UnitTests/Logging/Loggers/MessageSinkLoggerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
namespace Boa.Constrictor.Xunit.UnitTests; | ||
|
||
using Boa.Constrictor.Screenplay; | ||
using FluentAssertions; | ||
using global::Xunit; | ||
|
||
public class MessageSinkLoggerTest | ||
{ | ||
#region Variables | ||
|
||
const string TimePattern = @"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}"; | ||
|
||
ConcreteMessageSink MessageSink; | ||
|
||
MessageSinkLogger Logger; | ||
|
||
#endregion | ||
|
||
#region Setup | ||
|
||
public MessageSinkLoggerTest() | ||
{ | ||
MessageSink = new ConcreteMessageSink(); | ||
Logger = new MessageSinkLogger(MessageSink); | ||
} | ||
|
||
#endregion | ||
|
||
#region Tests | ||
|
||
[Fact] | ||
public void Close() | ||
{ | ||
Logger.Info("hello"); | ||
Logger.Info("moto"); | ||
Logger.Invoking(y => y.Close()).Should().NotThrow(); | ||
} | ||
|
||
[Fact] | ||
public void LogArtifact() | ||
{ | ||
const string type = "Screenshot"; | ||
const string path = "path/to/screen.png"; | ||
|
||
Logger.LogArtifact(type, path); | ||
|
||
MessageSink.LastMessage.Should().MatchRegex(TimePattern).And.EndWith($"[INFO] {type}: {path}"); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Trace")] | ||
[InlineData("Debug")] | ||
[InlineData("Info")] | ||
[InlineData("Warning")] | ||
[InlineData("Error")] | ||
[InlineData("Fatal")] | ||
public void LogByLevel(string level) | ||
{ | ||
const string message = "Message text!"; | ||
|
||
Logger.GetType().GetMethod(level).Invoke(Logger, new object[] { message }); | ||
|
||
MessageSink.LastMessage.Should().MatchRegex(TimePattern).And.EndWith($"[{level.ToUpper()}] {message}"); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Info")] | ||
[InlineData("Warning")] | ||
[InlineData("Error")] | ||
[InlineData("Fatal")] | ||
public void LowestSeverityLogged(string level) | ||
{ | ||
const string message = "Message text!"; | ||
Logger.LowestSeverity = LogSeverity.Info; | ||
|
||
Logger.GetType().GetMethod(level).Invoke(Logger, new object[] { message }); | ||
|
||
MessageSink.LastMessage.Trim().Should().MatchRegex(TimePattern).And.EndWith($"[{level.ToUpper()}] {message}"); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Trace")] | ||
[InlineData("Debug")] | ||
[InlineData("Info")] | ||
[InlineData("Warning")] | ||
[InlineData("Error")] | ||
public void LowestSeverityBlocked(string level) | ||
{ | ||
const string message = "Message text!"; | ||
Logger.LowestSeverity = LogSeverity.Fatal; | ||
|
||
Logger.GetType().GetMethod(level).Invoke(Logger, new object[] { message }); | ||
|
||
MessageSink.LastMessage.Should().BeNull(); | ||
} | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json", | ||
"diagnosticMessages": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
Boa.Constrictor.Xunit/Logging/Loggers/MessageSinkLogger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
namespace Boa.Constrictor.Xunit | ||
{ | ||
using Boa.Constrictor.Screenplay; | ||
using global::Xunit.Abstractions; | ||
using global::Xunit.Sdk; | ||
|
||
/// <summary> | ||
/// Prints messages to xUnit's IMessageSink. | ||
/// </summary> | ||
public class MessageSinkLogger : AbstractLogger | ||
{ | ||
#region Constructors | ||
|
||
/// <summary> | ||
/// Constructor. | ||
/// </summary> | ||
/// <param name="messageSink">the logger object used by xUnit's extensibility classes.</param> | ||
/// <param name="lowestSeverity">The lowest severity message to log.</param> | ||
public MessageSinkLogger(IMessageSink messageSink, LogSeverity lowestSeverity = LogSeverity.Trace) | ||
:base(lowestSeverity) | ||
{ | ||
MessageSink = messageSink; | ||
} | ||
|
||
#endregion | ||
|
||
#region Properties | ||
|
||
/// <summary> | ||
/// A logger object used by xUnit's extensibility classes. | ||
/// </summary> | ||
public IMessageSink MessageSink { get; set; } | ||
|
||
#endregion | ||
|
||
#region Log Method Implementations | ||
|
||
/// <summary> | ||
/// Closes the logging stream | ||
/// (No-op for IMessageSink) | ||
/// </summary> | ||
public override void Close() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Logs a basic message to the console after checking the lowest severity. | ||
/// </summary> | ||
/// <param name="message">The message text.</param> | ||
/// <param name="severity">The severity level (defaults to info).</param> | ||
protected override void LogRaw(string message, LogSeverity severity = LogSeverity.Info) | ||
{ | ||
if (severity >= LowestSeverity) | ||
{ | ||
var diagnosticMessage = new DiagnosticMessage(MessageFormat.StandardTimestamp(message, severity)); | ||
MessageSink.OnMessage(diagnosticMessage); | ||
} | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.