Skip to content

Commit

Permalink
Add default constructors for ContainerFixture and ContainerTest
Browse files Browse the repository at this point in the history
This makes Testcontainers logs optional in a better way than passing null for the IMessageSink or ITestOutputHelper.
  • Loading branch information
0xced committed Nov 23, 2024
1 parent 5448078 commit 80b1f1d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
23 changes: 19 additions & 4 deletions src/Testcontainers.Xunit/ContainerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@ namespace Testcontainers.Xunit;
/// <summary>
/// Fixture for sharing a container instance across multiple tests in a single class.
/// See <a href="https://xunit.net/docs/shared-context">Shared Context between Tests</a> from xUnit.net documentation for more information about fixtures.
/// A logger is automatically configured to write diagnostic messages to xUnit's <see cref="IMessageSink" />.
/// </summary>
/// <typeparam name="TBuilderEntity">The builder entity.</typeparam>
/// <typeparam name="TContainerEntity">The container entity.</typeparam>
[PublicAPI]
public class ContainerFixture<TBuilderEntity, TContainerEntity>(IMessageSink messageSink)
: ContainerLifetime<TBuilderEntity, TContainerEntity>(new MessageSinkLogger(messageSink))
public class ContainerFixture<TBuilderEntity, TContainerEntity> : ContainerLifetime<TBuilderEntity, TContainerEntity>
where TBuilderEntity : IContainerBuilder<TBuilderEntity, TContainerEntity>, new()
where TContainerEntity : IContainer;
where TContainerEntity : IContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="ContainerFixture{TBuilderEntity,TContainerEntity}" /> class.
/// </summary>
/// <remarks>Use the constructor taking an <see cref="IMessageSink"/> for logging Testcontainers messages.</remarks>
public ContainerFixture() : base(NullLogger.Instance)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ContainerFixture{TBuilderEntity,TContainerEntity}" /> class.
/// </summary>
/// <param name="messageSink">The message sink where the logs, prefixed with <c>[testcontainers.org]</c>, will be written to.</param>
public ContainerFixture(IMessageSink messageSink) : base(new MessageSinkLogger(messageSink))
{
}
}
28 changes: 24 additions & 4 deletions src/Testcontainers.Xunit/ContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,35 @@ namespace Testcontainers.Xunit;

/// <summary>
/// Base class for tests needing a container per test method.
/// A logger is automatically configured to write messages to xUnit's <see cref="ITestOutputHelper" />.
/// </summary>
/// <typeparam name="TBuilderEntity">The builder entity.</typeparam>
/// <typeparam name="TContainerEntity">The container entity.</typeparam>
[PublicAPI]
public abstract class ContainerTest<TBuilderEntity, TContainerEntity>(ITestOutputHelper testOutputHelper, Func<TBuilderEntity, TBuilderEntity> configure = null)
: ContainerLifetime<TBuilderEntity, TContainerEntity>(new TestOutputLogger(testOutputHelper))
public abstract class ContainerTest<TBuilderEntity, TContainerEntity> : ContainerLifetime<TBuilderEntity, TContainerEntity>
where TBuilderEntity : IContainerBuilder<TBuilderEntity, TContainerEntity>, new()
where TContainerEntity : IContainer
{
protected override TBuilderEntity Configure(TBuilderEntity builder) => configure != null ? configure(builder) : builder;
private readonly Func<TBuilderEntity, TBuilderEntity> _configure;

/// <summary>
/// Initializes a new instance of the <see cref="ContainerTest{TBuilderEntity,TContainerEntity}" /> class.
/// </summary>
/// <param name="configure">An optional callback to configure the container.</param>
/// <remarks>Use the constructor taking an <see cref="ITestOutputHelper"/> for logging Testcontainers messages.</remarks>
protected ContainerTest(Func<TBuilderEntity, TBuilderEntity> configure = null) : base(NullLogger.Instance)
{
_configure = configure;
}

/// <summary>
/// Initializes a new instance of the <see cref="ContainerTest{TBuilderEntity,TContainerEntity}" /> class.
/// </summary>
/// <param name="testOutputHelper">The test output helper where the logs, prefixed with <c>[testcontainers.org]</c> and a timestamp, will be written to.</param>
/// <param name="configure">An optional callback to configure the container.</param>
protected ContainerTest(ITestOutputHelper testOutputHelper, Func<TBuilderEntity, TBuilderEntity> configure = null) : base(new TestOutputLogger(testOutputHelper))
{
_configure = configure;
}

protected override TBuilderEntity Configure(TBuilderEntity builder) => _configure != null ? _configure(builder) : builder;
}
1 change: 1 addition & 0 deletions src/Testcontainers.Xunit/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
global using DotNet.Testcontainers.Containers;
global using JetBrains.Annotations;
global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.Logging.Abstractions;
global using Xunit;
global using Xunit.Sdk;
#if XUNIT_V3
Expand Down

0 comments on commit 80b1f1d

Please sign in to comment.