7

In my application I have multiple small entity framework dbcontexts which share the same database, for example:

public class Context1 : DbContext {
    public Context1()
        : base("DemoDb") {
    }
}

public class Context2 : DbContext {
    public Context2()
        : base("DemoDb") {
    }
}

All database updates are done via scripts and do not rely on migrations (nor will they going forward). The question is - how would you do integration testing against these contexts?

I believe there are three options here (there may be more I just don't know them)

Option 1 - Super context - a context which contains all models and configurations required for setting up the database:

public class SuperContext : DbContext
{
    public SuperContext()
        : base("DemoDb") {
    }
}

In this option the test database would be setup against the super context and all subsequent testing would be done through the smaller contexts. The reason I am not keen on this option is that I will be duplicating all the configurations and entity models that i have already built.

Option 2 - create a custom initialiser for integration tests that will run all the appropriate db initialisation scripts:

public class IntegrationTestInitializer : IDatabaseInitializer<DbContext> {

    public void InitializeDatabase(DbContext context) {
        /* run scripts to set up database here */
    }
}

This option allows for testing against the true database structure but will also require updating everytime new db scripts are added

Option 3 - just test the individual contexts:

In this option one would just let EF create the test database based upon the context and all tests would operate within there own "sandbox". The reason that I don't like this is that it doesn't feel like you would be testing against a true representation of the database.

I'm currently swaying towards options 2. What do you all think? Is there a better method out there?

2 Answers 2

8

I'm using integration testing a lot, because I still think it's the most reliable way of testing when data-dependent processes are involved. I also have a couple of different contexts, and DDL scripts for database upgrades, so our situations are very similar.

What I ended up with was Option 4: maintaining unit test database content through the regular user interface. Of course most integration tests temporarily modify the database content, as part of the "act" phase of the test (more on this "temporary" later), but the content is not set up when the test session starts.

Here's why.

At some stage we also generated database content at the start of the test session, either by code or by deserializing XML files. (We didn't have EF yet, but otherwise we would probably have had some Seed method in a database initializer). Gradually I started to feel misgivings with this approach. It was a hell of a job to maintain the code/XML when the data model or the business logic changed, esp. when new use cases had to be devised. Sometimes I allowed myself a minor corruption of these test data, knowing that it would not affect the tests.

Also, the data had to make sense, as in they had to be as valid and coherent as data from the real application. One way to ensure that is to generate the data by the application itself, or else inevitably you will somehow duplicate business logic in the seed method. Mocking real-world data is actually very hard. That's the most important thing I found out. Testing data constellations that don't represent real use cases isn't only a wast of time, it's false security.

So I found myself creating the test data through the application's front end and then painstakingly serializing this content into XML or writing code that would generate exactly the same. Until one day it occurred to me that I had the data readily available in this database, so why not use it directly?

Now maybe you ask How to make tests independent?

Integration tests, just as unit tests, should be executable in isolation. They should not depend on other tests, nor should they be affected by them. I assume that the background of your question is that you create and seed a database for each integration test. This is one way to achieve independent tests.

But what if there is only one database, and no seed scripts? You could restore a backup for each test. We chose a different approach. Each integration test runs within a TransactionScope that's never committed. It is very easy to achieve this. Each test fixture inherits from a base class that has these methods (NUnit):

[SetUp]
public void InitTestEnvironment()
{
    SetupTeardown.PerTestSetup();
}

[TearDown]
public void CleanTestEnvironment()
{
    SetupTeardown.PerTestTearDown();
}

and in SetupTeardown:

public static void PerTestSetup()
{
    _tranactionScope = new TransactionScope();
}

public static void PerTestTearDown()
{
    if (_tranactionScope != null)
    {
        _tranactionScope.Dispose(); // Rollback any changes made in a test.
        _tranactionScope = null;
    }
}

where _tranactionScope is a static member variable.

1
  • 1
    Thanks for taking the time to answer this. The transactional side of the testing that you outlined is certainly useful and something that I am now doing.
    – tully2003
    Oct 10, 2014 at 14:14
0

Option 2, or any variation thereof that runs the actual DB update scripts would be the best. Otherwise than this you are not necessarily integration testing against the same database you have in production (with respect to the schema, at least).

In order to address your concern about requiring updating every time new DB scripts are added, if you were to keep all the scripts in a single folder, perhaps within the project with a build action of "copy if newer", you could programmatically read each file and execute the script therein. As long as the place you're reading the files from is your canonical repository for the update scripts, you will never need to go in and make any further changes.

3
  • While i do like the idea of reading in each file so that i wouldn't have to update the initialiser the only problem is that generally the scripts will need to run in a certain order. for example If script 1 creates a table but script 2 adds a field to that table and script 2 runs first then it would fail. I'm sure there could be a workaround though
    – tully2003
    Oct 9, 2014 at 11:49
  • Prefix the name of the file with the datetime stamp in ISO 8601 format. Oct 9, 2014 at 12:25
  • Yeah that would be a sensible thing to do - not sure why i didn't think of that but thanks :)
    – tully2003
    Oct 9, 2014 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.