views:

953

answers:

5

Does anyone have any experience getting MSTest to copy hibernate.cfg.xml properly to the output directory? All my MSTests fail with a cannot find hibernate.cfg.xml error (I have it set to Copy Always), but my MBUnit tests pass.

+9  A: 

You can try adding the DeploymentItemAttribute to one of your tests, or edit your .testrunconfig file and add the file to the Deployment list.

Will
A: 

a workaround rather than an answer: NHibernate supports programmatic configuration. so you can write your own native properties/config file and parse it into hibernate configurations on startup.

Amir Arad
A: 

Ran into the same thing a few weeks ago -- this is actually a bug with MSTest -- I believe this was corrected with the recent Service Pack Release (even though it still says "Active"). If not, all I had to do was reference my hibernate.cfg.xml directly (sloppy but works for testing -- this is referencing the hibernate.cfg.xml file in my tests project from the "TestResults" folder):

 try
           {
                sessionFactory = new Configuration()
                    .Configure()
                    .BuildSessionFactory();
            }
            // Assume we are in "MSTest mode"
            catch (Exception)
            {
                sessionFactory = new Configuration()
                    .Configure(@"..\..\..\Program.Tests\" + @"\hibernate.cfg.xml")
                    .BuildSessionFactory();
            }
Watson
A: 

I like to mark my NHibernate config files as Embedded Resources, and use the Configuration.Configure() overload which reads config files from the Assembly Resources.

Justice
+2  A: 

Edit localtestrun.testrunconfig (in your solution items folder). Select the deployment option and add the hibernate.cfg.xml file to the list of additional files to deploy. The file should then get copied to the output directory where the test gets run.