Is it possible to reuse code for integration and unit tests?
c#, integration-testing, language-agnostic, tdd, unit-testing
Solution
Even though others had good points in their answers, this is what I ended up doing
I created a base class for unit and integration tests
[TestFixture]
public class FooBase
{
[Test]
public void GetBarIsNotNullTest()
{
var foo = IoC.Current.Resolve<IFoo>();
Bar actual = foo.GetBar();
Assert.IsNotNull(actual);
}
//many other tests
}
And then two derived classes from the `FooBase`. These class will only have the `SetUp` and nothing else. i.e.:
[TestFixture]
public class UnitTestFoo : FooBase
{
[SetUp]
public void SetUp()
{
IoC.Current.Register<IFoo, FakeFoo>();
}
//nothing else here
}
[TestFixture]
public class IntegrationTestFoo : FooBase
{
[SetUp]
public void SetUp()
{
IoC.Current.Register<IFoo, RealFoo>();
}
//nothing else here
}
So if I now run my tests, I get the tests defined in the parent class `FooBase` run twice for unit tests class and integration tests class with their own real and fake objects. This works because of the inheritance of the test fixtures.
Problem
I work with a distributed system that has unit and integration tests. I am trying to save time and maintenance efforts by reusing code between integration and unit tests. For this I implemented an interface and 2 classes: fake and real. Fake class returns some stubbed data, and real class makes a few calls to other distributed services. Current structure of my projects ``` /BaseTest interface IFoo ------------------------------------- /UnitTest class FakeFoo : IFoo [TestFixture] class FooTest {...} //uses FakeFoo ------------------------------------- /IntegrationTest class RealFoo : IFoo [TestFixture] class FooTest {...} //uses RealFoo ``` I want to somehow reuse code for both tests, so if I have a test ``` [Test] public void GetBarIsNotNullTest() { var foo = IoC.Current.Resolve<IFoo>(); Bar actual = foo.GetBar(); Assert.IsNotNull(actual); } ``` I want this test to run with both implementations: `RealFoo` and `FakeFoo`. So far I thought about copy-pasting tests between /UnitTest and /IntegrationTest projects, but this doesn't sound right. System is written in C#, but I believe this question is language agnostic. Anyone has better ideas? Am I doing this wrong?