Skip to content

Commit 8cfdbfc

Browse files
committed
Use Constructor / Dispose instead of [Before(Test)] / [After(Test)]
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
1 parent bb7187f commit 8cfdbfc

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

tests/Log4NetTextFormatterTest.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
namespace Serilog.Formatting.Log4Net.Tests;
1515

16-
public sealed class Log4NetTextFormatterTest
16+
public sealed class Log4NetTextFormatterTest : IDisposable
1717
{
18-
private TextWriter? _selfLogWriter;
19-
private string? SelfLogValue => _selfLogWriter?.ToString();
18+
private readonly TextWriter _selfLogWriter;
19+
private string? SelfLogValue => _selfLogWriter.ToString();
2020

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

43-
[Before(Test)]
44-
public void EnableSelfLog()
43+
public Log4NetTextFormatterTest()
4544
{
4645
_selfLogWriter = new StringWriter();
4746
Debugging.SelfLog.Enable(_selfLogWriter);
4847
}
4948

50-
[After(Test)]
51-
public void DisableSelfLog()
49+
public void Dispose()
5250
{
5351
Debugging.SelfLog.Disable();
54-
_selfLogWriter?.Dispose();
55-
_selfLogWriter = null;
52+
_selfLogWriter.Dispose();
5653
}
5754

5855
[Test]

0 commit comments

Comments
 (0)