There is a concept known as 'Setup' and 'Teardown' when unit testing.
You can define methods in your test class which have special significance to the test framework (ie. MS Test, NUnit) which is executing your tests. You can use following attributes with MS Test class, which specify code to be run
-
before each test is run [TestInitialize]
-
after a test has finished running TestCleanup]
- before
any tests are run [ClassInitialize]
- after all tests have been executed [ClassCleanup]
I don't use CastleWindsor myself, but your code would look something along following lines ...
namespace MyTestProject
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class MyTestClass
{
private static IWindsorContainer _Container;
[ClassInitialize()]
public static void ClassInit(TestContext context)
{
var container = new WindsorContainer();
container.Register(
Component.For<IObjectToTest>().ImplementedBy<ObjectToTest>()
);
_Container = container;
}
[TestMethod()]
public void MyTestMethod()
{
IObjectToTest obj = _Container.Resolve<IObjectToTest>();
var result = obj.MyMethod();
Assert.IsNotNull(obj);
}
[ClassCleanup()]
public static void ClassCleanup()
{
_Container.Dispose();
}
}
}
Hope that helps get you started!