简体   繁体   English

使用NUnit在不同的appdomain中运行单元测试

[英]Run unit tests in different appdomain with NUnit

I seem to be having an issue, the application we're using uses a Ninject kernel, and contains a particular configuration that's gathered with contents of the WCF call (username, token...). 我似乎遇到了一个问题,我们正在使用的应用程序使用Ninject内核,并包含一个特定的配置,该配置是使用WCF调用的内容(username,token ...)收集的。

Based on that a particular configuration the user is assigned rights on the app, this is shielded through a particular instance in the Ninject kernel. 基于特定配置,用户被分配了应用程序的权限,这通过Ninject内核中的特定实例进行屏蔽。

We cannot just recompose the Ninject kernel, what we'd like to do is run a couple of Nunit tests, but run each of them in a separate app domain (recreating the kernel each time with different settings). 我们不能只重新组合Ninject内核,我们想要做的是运行几个Nunit测试,但是在一个单独的应用程序域中运行它们(每次使用不同的设置重新创建内核)。

I've only found ways to run whole test projects in different app domains but not test per test. 我只找到了在不同应用领域运行整个测试项目的方法,但没有测试每个测试。

Is there a way to solve this? 有办法解决这个问题吗?

Unfortunately the Ninject configuration is not on our side, we have to 'live' with it this way. 不幸的是,Ninject配置不在我们这边,我们必须以这种方式“生活”。

I needed to do the the exact same thing, so I created a library which basically takes the current test and re-executes it in a new AppDomain. 我需要做同样的事情,所以我创建了一个基本上接受当前测试并在新的AppDomain中重新执行它的库。 It's a nuget package called NUnit.ApplicationDomain and is open source . 它是一个名为NUnit.ApplicationDomain的nuget包,是开源的

Example Code: 示例代码:

[Test, RunInApplicationDomain]
public void Method()
{
  Console.WriteLine("I'm in a different AppDomain")
}

I don't think there is a way to solve it without re-writing parts of the NUnit code. 我认为没有办法解决它而不重写NUnit代码的部分内容。 It has been a while since I've been inside the NUnit code, but I am pretty sure the main app domain loading part has not changed. 我已经进入NUnit代码已经有一段时间了,但我很确定主应用程序域加载部分没有改变。

NUnit typically uses two app domains. NUnit通常使用两个应用程序域。 The default one that is created when NUnit is run and a separate one to load the test assemblies and the assemblies they reference. 运行NUnit时创建的默认值,以及加载测试程序集及其引用的程序集的单独一个。 It's main reason in doing this is to allow the unloading of the test assemblies. 这样做的主要原因是允许卸载测试组件。 You can't unload a dll, but you can unload an appdomain. 你无法卸载一个DLL,但你可以卸载一个appdomain。

You might be able to run NUnit once per test and pass the test on the command line, but that's ugly and I'm not sure it will help. 您可以在每次测试时运行NUnit并在命令行上传递测试,但这很难看,我不确定它会有所帮助。

It might also be possible to leverage Action Attributes in NUnit 2.6, but you are going to do a lot of work in there to do it. 也可以在NUnit 2.6中利用Action Attributes ,但是你要在那里做很多工作。

You might also be able to create a new app domain in your setup method and call into it in each test. 您也可以在设置方法中创建一个新的应用程序域,并在每个测试中调用它。 Awkward but possible. 尴尬但可能。

I'm sorry I don't have a more complete answer. 对不起,我没有更完整的答案。

I'm not entirely sure about your question. 我不完全确定你的问题。 However it seems like you need some kind of a custom implementation. 但是,您似乎需要某种自定义实现。 Did you consider custom test attributes? 您是否考虑过自定义测试属性? Then may be configure each attribute to run in a different App Domain? 然后可以配置每个属性在不同的App域中运行? I'm just spinning up some ideas, but there may be better ways of doing this. 我只是提出了一些想法,但可能有更好的方法来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM