Question

The setup method here:

NHibernate unit test

uses the NHibernate.config:

[SetUp]
public virtual void SetUp()
{
    var mappingAssemblies = RepositoryTestsHelper.GetMappingAssemblies();
    this.configuration = NHibernateSession.Init(
    new SimpleSessionStorage(),
    mappingAssemblies,
    new AutoPersistenceModelGenerator().Generate(),
    "../../../Northwind.Web/NHibernate.config");
}

Is this not a bit dodgy as it introduces an external dependency? I guess one could also create the NHibernate.config stuff programmatically and pass it to the correct Init method siganture. Am I correct and has someone done this during Setup? Thanks.

Was it helpful?

Solution

I had successfuly adopted similar approach on my current project. First it's not unit, but mapping integration tests because external resource i.e. database is involved, test's execution time and typical technical issues:

  1. It's much better not to use existed database schema, but create and export it every time from nHibernate using configuration with random schema name. This will eliminate possibility that two or more developers to run integration tests at same time on same schema:

    var export = new SchemaExport(configuration); export.Execute(script: false, export: true, justDrop: false, connection: databaseConnection, exportOutput: null);

  2. Remember to cleanup this schema after use. It easy to run out of disk space, etc in several days

  3. Think about move this code from {[SetUp]} to {[FixtureSetUp]} to save test fixture execution time. Work with database takes a lot of time compare to usual in-memory unittests execution time

  4. This kind of test could be triggered less frequently by develoeprs/CI server, compare to usual unittests, due of time and resource consumption. At my project they are scheduled only during night builds or manualy triggered builds.

  5. You could generate nHibernate configuration in runtime, so "../../../Northwind.Web/NHibernate.config" is not required

  6. Instead of using SQL Server you could use SQLite in many cases, but mapping is usually specific for each SQL engine

OTHER TIPS

You could do it programatically as you mentiond or just create a different NHibernate.Config in your test project or just add a link to the actual nhibernate.config in your project (right click project, add existing item, find NHibernate.Config then instead of clicking add do click the arrow next to add and select 'Add As Link').

I would say the really dodgy part is using the NHibernate.Config from production for your testing.

Where I have done such a thing in the past, we would create a database specfically for test (through a db migrations tool) and use that for our tests. Either by generating the config file for the test with the created database details, or generate the database matching the configuration in your test project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top