diff --git a/Ductus.FluentDocker.MsTest/FluentDockerTestBase.cs b/Ductus.FluentDocker.MsTest/FluentDockerTestBase.cs index 968539c..66f81ad 100644 --- a/Ductus.FluentDocker.MsTest/FluentDockerTestBase.cs +++ b/Ductus.FluentDocker.MsTest/FluentDockerTestBase.cs @@ -14,13 +14,20 @@ public abstract class FluentDockerTestBase [TestInitialize] public void Initialize() { - Container = Build().Build(); + var containerBuilder = Build(); + + this.OnBeforeContainerBuild(containerBuilder); + + Container = containerBuilder.Build(); + try { + this.OnBeforeContainerStart(); Container.Start(); } - catch + catch (Exception ex) { + this.OnBeforeDispose(Container, ex); Container.Dispose(); throw; } @@ -37,6 +44,7 @@ public void TeardownContainer() Container = null; try { + this.OnBeforeDispose(c, null); c?.Dispose(); } catch @@ -52,6 +60,35 @@ protected virtual void OnContainerTearDown() { } + /// + /// Invoked just before the container is built. + /// + /// The that is about to be built. + protected virtual void OnBeforeContainerBuild(ContainerBuilder containerBuilder) + { + } + + /// + /// Invoked just after the container is built and before starting it. + /// + protected virtual void OnBeforeContainerStart() + { + } + + /// + /// Invoked just before the container is ed. + /// + /// The that is about to be disposed. + /// The that caused the container to be disposed (when starting up). + /// + /// This method is invoked either when the container fails to start. In such situation the + /// is not null. It is also called when the test is cleaning up and thus the + /// is null. The is always set since the teardown + /// will clear the field. Note that the may still be null! + /// + protected virtual void OnBeforeDispose(IContainerService container, Exception throwable) + { + } /// /// Invoked after a container has been created and started. /// diff --git a/Ductus.FluentDocker.XUnit/FluentDockerTestBase.cs b/Ductus.FluentDocker.XUnit/FluentDockerTestBase.cs index 543585e..5f7da3b 100644 --- a/Ductus.FluentDocker.XUnit/FluentDockerTestBase.cs +++ b/Ductus.FluentDocker.XUnit/FluentDockerTestBase.cs @@ -12,13 +12,20 @@ public abstract class FluentDockerTestBase : IDisposable public FluentDockerTestBase() { - Container = Build().Build(); + var containerBuilder = Build(); + + this.OnBeforeContainerBuild(containerBuilder); + + Container = containerBuilder.Build(); + try { + this.OnBeforeContainerStart(); Container.Start(); } - catch + catch (Exception ex) { + this.OnBeforeDispose(Container, ex); Container.Dispose(); throw; } @@ -34,6 +41,7 @@ public void Dispose() Container = null; try { + this.OnBeforeDispose(c, null); c?.Dispose(); } catch @@ -49,6 +57,35 @@ protected virtual void OnContainerTearDown() { } + /// + /// Invoked just before the container is built. + /// + /// The that is about to be built. + protected virtual void OnBeforeContainerBuild(ContainerBuilder containerBuilder) + { + } + + /// + /// Invoked just after the container is built and before starting it. + /// + protected virtual void OnBeforeContainerStart() + { + } + + /// + /// Invoked just before the container is ed. + /// + /// The that is about to be disposed. + /// The that caused the container to be disposed (when starting up). + /// + /// This method is invoked either when the container fails to start. In such situation the + /// is not null. It is also called when the test is cleaning up and thus the + /// is null. The is always set since the teardown + /// will clear the field. Note that the may still be null! + /// + protected virtual void OnBeforeDispose(IContainerService container, Exception throwable) + { + } /// /// Invoked after a container has been created and started. /// diff --git a/global.json b/global.json index e7d5479..13b8b14 100644 --- a/global.json +++ b/global.json @@ -1,7 +1,7 @@ { "$schema": "https://json.schemastore.org/global", "sdk": { - "version": "6.0.100", + "version": "5.0.200", "allowPrerelease": false, "rollForward": "latestMinor" }