Nunit测试dll,单独运行,第二次运行失败

时间:2016-05-03 16:07:53

标签: c# nhibernate nunit structuremap

我有一套nunit测试,在测试跑步者中运行。

2个独立的dll,

如果我独立运行它们,它们都可以工作。 如果我一起运行它们就会失败。

我们正在使用Structure Map 2.5.3

我已将问题追溯到我们的NH会话生命周期。

     x.BuildInstancesOf<INHFactory>()
                .AddInstances(z => z
                    .OfConcreteType<NHFactory>()
                    .WithName("JCDC")
                )
                .CacheBy(InstanceScope.Singleton);

如果我单独运行它们,每次都会创建会话,但是如果一起运行它们,则会重复使用它们,导致它崩溃。

我尝试了实例范围线程,没有快乐,我尝试使用ObjectFactory.ResetDefaults()手动回收结构图;在我的测试夹具中拆解。

没有快乐。

有没有办法强制nunit在单独的线程中运行它们? 或者在dlls之间回收结构图工厂(在基础测试中拆除)?

还有另一种方法可以解决这个问题吗?

这是完整的引导程序代码

using System.Reflection;
using JCDCHelper.Logging.Interfaces;
using JCDCHelper.NhAccess.Interfaces;
using JCDCHelper.NhAccess.BusinessObjects;
using JCDCHelper.Persistence.BusinessObjects;
using JCDCHelper.Persistence.Interfaces;
using POCAdmin3G.DAL.Interfaces;
using POCAdmin3G.Jcdc.EO;
using POCAdmin3G.Jcdc.Map;
using StructureMap;
using StructureMap.Attributes;

namespace _Test_DAL
{
    public class _BootstrapStuctureMap
    {
        private static bool _hasStarted;

        /// <summary>
        /// Bootstraps the structure map.
        ///   Set up IOC for all parts of application
        ///   Set up NHFactory for each DB with scope of one per application.
        ///   Set up NHSession for Tran and NoTran.  Give it a scope of HttpRequest or Thread
        /// </summary>
        /// <Author>fink.pete</Author>
        /// <CreateDate>8/31/2010</CreateDate>
        public void BootstrapStructureMap()
        {
            _hasStarted = true;

            ObjectFactory.Initialize(x =>
            {
                x.PullConfigurationFromAppConfig = false;
                x.Scan(y =>
                {
                    y.Assembly(Assembly.GetAssembly(typeof(IPOCContrCtrlDAL))); // TestDisplay DAL
                    y.Assembly(Assembly.GetAssembly(typeof(IWebAccess))); // JCDCHelper Persistance
                    y.Assembly(Assembly.GetAssembly(typeof(INHSession))); // JCDCHelper NhAccess
                    y.Assembly(Assembly.GetAssembly(typeof(INetLog))); // JCDCHelper Logging

                    y.WithDefaultConventions();
                }
                    );

                // needed for new one WebAccess per application
                x.BuildInstancesOf<IWebAccess>()
                  .TheDefaultIsConcreteType<WinFormAccess>()
                  .CacheBy(InstanceScope.Hybrid);

                // needed for new one Factory for JCDC per application
                x.BuildInstancesOf<INHFactory>()
                    .AddInstances(z => z
                        .OfConcreteType<NHFactory>()
                        .WithName("JCDC")
                    )
                    .CacheBy(InstanceScope.Singleton);

                // needed for NHSession for JCDC HasTran per HttpRequest
                x.ForRequestedType<INHSession>()
                    .AddInstances(z => z
                        .OfConcreteType<NHSession>()
                        .WithName("JCDC_HasTrans")
                        .SetProperty(y => y.DBNameAndHasTran = "JCDC_HasTrans")
                    )
                    .AddInstances(z => z
                        .OfConcreteType<NHSession>()
                        .WithName("JCDC_HasNoTrans")
                        .SetProperty(y => y.DBNameAndHasTran = "JCDC_HasNoTrans")
                    )
                    .CacheBy(InstanceScope.Hybrid);
            });

            //Debug.WriteLine(ObjectFactory.WhatDoIHave());
            //ObjectFactory.AssertConfigurationIsValid();

            // Set up the NhibernateFactories
            INHFactory jcdcFactory = ObjectFactory.GetNamedInstance<INHFactory>("JCDC");
            jcdcFactory.BuildFactoryByConfigFile<AcademicEO, AcademicEOMap>("~/JcdcDb.config");
        }

        /// <summary>
        /// Restarts StructureMap.   Reset to original defaults.
        /// </summary>
        /// <Author>fink.pete</Author>
        /// <CreateDate>8/31/2010</CreateDate>
        public static void Restart()
        {

            if (_hasStarted)
            {
                ObjectFactory.ResetDefaults();
            }
            else
            {
                Bootstrap();
                _hasStarted = true;
            }
        }

        public static void Bootstrap()
        {
            new _BootstrapStuctureMap().BootstrapStructureMap();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

假设使用nunit 2.x,使用nunit-console选项/进程在单独的进程中运行每个程序集:Multiple。