Skip to content

Commit

Permalink
Use Constructor / Dispose instead of [Before(Test)] / [After(Test)]
Browse files Browse the repository at this point in the history
Note that this fails the two test with the [NotInParallel] attribute, i.e. FilterPropertyThrowing and ExceptionFormatterThrowing.

The [Test Set Ups][1] and [Test Clean Ups][2] documentation mentions respectively:

> Most setup for a test can be performed in the constructor (think setting up mocks, assigning fields.)

and

> TUnit supports having your test class implement `IDisposable` or `IAsyncDisposable`. These will be called after your test has finished executing.

So I'm not sure what's the difference between Custructor/Dispose and [Before(Test)] / [After(Test)] attributes.

[1]: https://thomhurst.github.io/TUnit/docs/tutorial-extras/setup
[2]: https://thomhurst.github.io/TUnit/docs/tutorial-extras/cleanup
  • Loading branch information
0xced committed Jan 16, 2025
1 parent 0bdc1da commit 35d5f2a
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions tests/Log4NetTextFormatterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

namespace Serilog.Formatting.Log4Net.Tests;

public sealed class Log4NetTextFormatterTest
public sealed class Log4NetTextFormatterTest : IDisposable
{
private TextWriter? _selfLogWriter;
private string? SelfLogValue => _selfLogWriter?.ToString();
private readonly TextWriter _selfLogWriter;
private string? SelfLogValue => _selfLogWriter.ToString();

/// <summary>
/// Create a <see cref="DictionaryValue"/> containing two entries, mapping scalar values 1 to "one" and "two" to 2.
Expand All @@ -40,19 +40,16 @@ private static LogEvent CreateLogEvent(LogEventLevel level = Events.LogEventLeve
);
}

[Before(Test)]
public void EnableSelfLog()
public Log4NetTextFormatterTest()
{
_selfLogWriter = new StringWriter();
Debugging.SelfLog.Enable(_selfLogWriter);
}

[After(Test)]
public void DisableSelfLog()
public void Dispose()
{
Debugging.SelfLog.Disable();
_selfLogWriter?.Dispose();
_selfLogWriter = null;
_selfLogWriter.Dispose();
}

[Test]
Expand Down

0 comments on commit 35d5f2a

Please sign in to comment.