views:

60

answers:

2

I have this abstract test class

[TestFixture]
public abstract class BaseTests
{

       private IRepository _repository;

        public BaseTests(IRepository repository)
        {
            _repository = repository;
        }

        [Test]
        public virtual void CanGetResultsFromSearch()
        {
            var results = _repository.GetResults("test");
            Assert.IsTrue(results.Count() > 0);
        }
}

Is there any value in executing the Base Tests as part of Unit Testing and also Integration testings. e.g

[TestFixture]
public class UnitTest : BaseTests
{
    private static IRepository _repository = new FakeReposistory();

    public UnitTest() : base(_repository)
    {
    } 
}

[TestFixture]
public class IntegrationTest : BaseTests
{
    private static IRepository _repository = new SqlReposistory();

    public UnitTest() : base(_repository)
    {
    } 
}

Is there value in repeating the test twice? I am a bit unclear if this particular test is a concern of the integration test? i.e Should integration tests be testing public methods on a class? Or should it be more into testing the functionality of the system? Or Both?!

A: 

I'm one of the evil sort that think a unit can be expanded to include a database, so take this with the grain of salt you would with any other weirdo.

I see no point in repeating the tests or even running a unit test on the data layer with a completely mocked database in most cases (one exception being error conditions).

Do you mind if I ask what GetResults would look like for FakeReposistory? Or is that a simplification and it is really new SqlReposistory(mockedConnection)?

mlk
its just an example but in theory it could perform a search and return a List<T>
But still the way it looks above is that you are not running a unit test on the repository, but rather on a FakeReposistory. Which (to me) would be pointless as the the FakeReposistory is just that - something you can control for unit tests.
mlk
A: 

As your examples are written, it definitely makes sense to run both test suites as they would be testing two different things.

In the first case, it tests your System Under Test (SUT) against a Fake database.

In the second case, it tests your SUT against a real database.

At the very least, these tests will verify that the SUT doesn't violate the Liskov Substitution Principle.

There are other advantages, though: Your product is probably not going to run against a Fake database in production, so the Integration Tests give you a better indication of whether the code works as intended. However, running the Integration Tests will probably be much slower than running the unit test suite, so even if the unit test suite is not a realistic scenario, it will give you more rapid feedback about the state of the code.

Mark Seemann