gpt4 book ai didi

c# - Nunit 测试 dll,单独运行通过,一起运行第二个失败

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:47 24 4
gpt4 key购买 nike

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

2 个独立的 dll,

如果我独立运行它们,它们都可以工作。如果我同时运行它们,则第二个失败。

我们正在使用 Structure Map 2.5.3

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

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

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

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

没有快乐。

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

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

这是完整的 Bootstrap 代码

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

最佳答案

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

关于c# - Nunit 测试 dll,单独运行通过,一起运行第二个失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37009105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com