Class ContainerFixture
Used to run a Docker container on the current machine as a test fixture while tests are being performed and then deletes the container when the fixture is disposed.
Implements
Inherited Members
Namespace: Neon.Xunit
Assembly: Neon.Xunit.dll
Syntax
public class ContainerFixture : TestFixture, ITestFixture
Remarks
note
IMPORTANT: The base Neon TestFixture implementation DOES NOT
support parallel test execution. You need to explicitly disable parallel execution in
all test assemblies that rely on thesex test fixtures by adding a C# file called
AssemblyInfo.cs
with:
[assembly: CollectionBehavior(DisableTestParallelization = true, MaxParallelThreads = 1)]
and then define your test classes like:
public class MyTests : IClassFixture<ontainerFixture>, IDisposable
{
[Collection(TestCollection.NonParallel)]
[CollectionDefinition(TestCollection.NonParallel, DisableParallelization = true)]
[Fact]
public void Test()
{
}
}
Constructors
ContainerFixture()
Constructor.
Declaration
public ContainerFixture()
Properties
ContainerId
Returns the running container's short ID or null
if the container
has not been started.
Declaration
public string ContainerId { get; }
Property Value
Type | Description |
---|---|
string |
ContainerName
Returns the running container's name or null
if the container
has not been started.
Declaration
public string ContainerName { get; }
Property Value
Type | Description |
---|---|
string |
DefaultHostInterface
Specifies the IP address of host interface where container ports will be published. This defaults to 0.0.0.0 which binds ports to all network interfaces.
Declaration
public static string DefaultHostInterface { get; set; }
Property Value
Type | Description |
---|---|
string |
Remarks
You may need to customize this to avoid port conflicts with other running applications. When all tests are running on a single host, you should consider setting this to one of the 16 million loopback addresses in the 127.0.0.0/8 subnet (e.g. 127.0.0.1, 127.0.0.2, etc). You'll need to set this before starting any fixture containers.
note
Fixtures implemented by NEONFORGE that are derived from ContainerFixture all implement this behavior. If you implement your own derived fixtures, you should consider implementing this as well for consistency.
Methods
Dispose(bool)
Releases all associated resources.
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing | Pass |
Overrides
~ContainerFixture()
Finalizer.
Declaration
protected ~ContainerFixture()
GetHostInterface(string, bool)
Used by derived fixtures to retrieve the host network interface address for the docker -p port publish option or the address to use for establishing a Cadence connections. interfaces.
Declaration
protected static string GetHostInterface(string hostInterface, bool forConnection = false)
Parameters
Type | Name | Description |
---|---|---|
string | hostInterface | The desired host interface IPv4 address or |
bool | forConnection | Indicates that the address a client should use to establish a connection should be returned vs. the address the container will listen on. |
Returns
Type | Description |
---|---|
string | The target network interface address. |
Remarks
This method returns DefaultHostInterface when hostInterface
is null
or empty otherwise it will ensure that the parameter is valid
and before returning it.
Reset()
INTERNAL USE ONLY: Resets the fixture state.
Declaration
public override void Reset()
Overrides
Restart()
Restarts the container. This is a handy way to deploy a fresh container with the same properties while running unit tests.
Declaration
public void Restart()
Start(string, string, string[], IEnumerable<string>, IEnumerable<string>, bool, bool, ContainerLimits)
Starts the container.
note
You'll need to call StartAsComposed(string, string, string[], IEnumerable<string>, IEnumerable<string>, bool, bool, ContainerLimits) instead when this fixture is being added to a ComposedFixture.
Declaration
public TestFixtureStatus Start(string name, string image, string[] dockerArgs = null, IEnumerable<string> containerArgs = null, IEnumerable<string> env = null, bool noRemove = false, bool keepOpen = false, ContainerLimits limits = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | Specifies the container name. |
string | image | Specifies the container Docker image. |
string[] | dockerArgs | Optional arguments to be passed to the docker run ... command. |
IEnumerable<string> | containerArgs | Optional arguments to be passed to the container. |
IEnumerable<string> | env | Optional environment variables to be passed to the Couchbase container, formatted as NAME=VALUE or just NAME. |
bool | noRemove | Optionally indicates that the --rm option should not be included when creating the container. |
bool | keepOpen | Optionally indicates that the container should continue to run after the fixture is disposed. |
ContainerLimits | limits | Optionally specifies the Docker container resource limits. |
Returns
Type | Description |
---|---|
TestFixtureStatus | Started if the fixture wasn't previously started and this method call started it or AlreadyRunning if the fixture was already running. |
Remarks
note
You must specify a valid container name
so that the fixure
can remove any existing container with the same name before starting the new container.
This is very useful during test debugging when the test might be interrupted during
debugging before ensuring that the container is stopped.
Exceptions
Type | Condition |
---|---|
InvalidOperationException | Thrown if this is not called from within the Action method passed Start(Action) |
StartAsComposed(string, string, string[], IEnumerable<string>, IEnumerable<string>, bool, bool, ContainerLimits)
Used to start the fixture within a ComposedFixture.
Declaration
public void StartAsComposed(string name, string image, string[] dockerArgs = null, IEnumerable<string> containerArgs = null, IEnumerable<string> env = null, bool noRemove = false, bool keepOpen = false, ContainerLimits limits = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | Specifies the container name. |
string | image | Specifies the container Docker image. |
string[] | dockerArgs | Optional arguments to be passed to the docker run ... command. |
IEnumerable<string> | containerArgs | Optional arguments to be passed to the container. |
IEnumerable<string> | env | Optional environment variables to be passed to the Couchbase container, formatted as NAME=VALUE or just NAME. |
bool | noRemove | Optionally indicates that the --rm option should not be included when creating the container. |
bool | keepOpen | Optionally indicates that the container should continue to run after the fixture is disposed. |
ContainerLimits | limits | Optionally specifies Docker container resource limits. |
Remarks
note
You must specify a valid container name
so that the fixure
can remove any existing container with the same name before starting the new container.
This is very useful during test debugging when the test might be interrupted during
debugging before ensuring that the container is stopped.
Exceptions
Type | Condition |
---|---|
InvalidOperationException | Thrown if this is not called from within the Action method passed Start(Action) |