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?
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
And then two derived classes from the
FooBase. These class will only have theSetUpand nothing else. i.e.:So if I now run my tests, I get the tests defined in the parent class
FooBaserun 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.