MSTest and NHibernate

2019-04-24 19:51发布

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.

5条回答
Bombasti
2楼-- · 2019-04-24 20:13

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.

查看更多
老娘就宠你
3楼-- · 2019-04-24 20:19

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();
            }
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-04-24 20:31

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.

查看更多
相关推荐>>
5楼-- · 2019-04-24 20:32

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.

查看更多
老娘就宠你
6楼-- · 2019-04-24 20:34

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

查看更多
登录 后发表回答