【问题标题】:Nunit test dlls, run separately pass, run together 2nd one failsNunit 测试 dll,单独运行通过,一起运行第二个失败
【发布时间】:2016-05-03 16:07:52
【问题描述】:

我有一套 nunit 测试,在测试运行器中运行。

2 个独立的 dll,

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

我们正在使用结构图 2.5.3

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

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

如果我分别运行它们,则每次都会创建会话,但如果一起运行它们,则会重复使用相同的会话,从而导致它崩溃。

我试过实例作用域线程,不开心,我试过用 ObjectFactory.ResetDefaults(); 手动回收结构映射;在我的测试夹具拆解中。

不开心。

有没有办法强制 nunit 在单独的线程中运行它们? 还是回收dll之间的结构映射工厂(在基础测试中的拆解中)?

还有其他方法可以解决这个问题吗?

这是完整的引导代码

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();
        }
    }
}

【问题讨论】:

    标签: c# nhibernate nunit structuremap


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多