向后兼容 C# 反射和 NUnit 版本 [英] Backwards compatibility with C# Reflection and NUnit Versions

查看:70
本文介绍了向后兼容 C# 反射和 NUnit 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在带有 Visual Studio 2015 的 .NET 4.5 框架上使用 C#.我试图将 NUnit 支持插入到围绕 MSUnit 测试构建的自动化测试系统中.作为系统的一部分,我需要使用反射在提供的 .dll 文件中找到标有 TestAttributeTestCaseAttribute 的方法.目前该系统已为 NUnit 3.2.1 版安装了 NuGet 包.

I am using C# on the .NET 4.5 framework with Visual Studio 2015. I am attempting to plug NUnit support into an automated test system that was built around MSUnit tests. As part of the system, I need to find methods marked with TestAttribute and TestCaseAttribute in provided .dll files using Reflection. Currently this system has NuGet packages installed for NUnit version 3.2.1.

问题:有没有办法在使用旧版 NUnit 创建的测试中检测这些属性?例如,我有一些测试是使用 NUnit 2.6.4 版本创建的,但是找不到它们对应的属性,因为系统正在寻找来自 NUnit 3.2.1 的属性.

Question: is there a way to detect these attributes on tests that were created using older versions of NUnit? For example, I have some tests that were created using NUnit version 2.6.4, but their corresponding attributes are not found because the system is looking for attributes from NUnit 3.2.1.

以下是用于检测所提供的 .dll 中标有 TestFixtureAttribute 的测试类的代码片段:

Here is a snippet of code used to detect the test classes marked with TestFixtureAttribute in the provided .dll:

var testClasses = testAssembly
            .SelectMany(a => a.GetTypes())
            .Where(a => a.IsDefined(typeof(TestFixtureAttribute), false));

同样,这个片段在提供的 .dll 上没有找到任何测试类,因为旧的 TestFixtureAttribute 与 NUnit 3.2.1 中的不同

Again, this snippet doesn't find any test classes on the provided .dll because the older TestFixtureAttribute is not the same as the one in NUnit 3.2.1

我已经在 nunit3-console.exe 上成功运行了较旧的 NUnit 测试,因此目前发现这些测试是一大障碍.

I have successfully run older NUnit tests on the nunit3-console.exe, so discovering the tests is the big roadblock for now.

在此先感谢您的帮助!

推荐答案

这可能不会得到很多选票,但我的回答是不要那样做!"无论如何,这不适合评论.:-)

This may not get a lot of votes, but my answer would be "Don't do that!" And this won't fit in a comment anyway. :-)

NUnit 3.x 有一个用于运行测试的 API.通过查找属性自己发现它们意味着每次有人(包括您自己的用户)添加标识测试的新属性时,您都必须更新您的应用程序.这也意味着您必须弄清楚运行 NUnit 测试的语义,并复制 NUnit 执行方式的每一次转折.

NUnit 3.x has an API for running tests. Discovering them yourself by looking for attributes means that you have to update your application every time somebody (including your own users) adds a new attribute that identifies tests. It also means you have to figure out the semantics of running an NUnit test and duplicate every twist and turn of how NUnit does it.

这就是过去"的做法 - 我的意思是在 NUnit V2 和 MsTest 中.创建 NUnit 3 的主要目标之一是提供一个 API,无需第三方运行器复制 NUnit 中已有的逻辑.

That's how it was done in the "old days" - by which I mean in NUnit V2 and also in MsTest. One of the main goals in creating NUnit 3 was to provide an API that would get rid of the need for third-party runners to duplicate the logic that's already inside NUnit.

即使不等待未来破坏您的实施,您现在也可以看到更改对其的影响.如果您处理 Test 和 TestCase 属性,那么...* 测试用例源* 测试夹具* 测试夹具源* 价值观* 随机的* 范围* 组合* 顺序* 成对(这是我的头脑......我可能错过了一些东西)

Even without waiting for the future to break your implementation, you can see the impact of change on it right now. If you handle Test and TestCase attributes, what about... * TestCaseSource * TestFixture * TestFixtureSource * Values * Random * Range * Combinatorial * Sequential * Pairwise (That's out of my head... I probably missed something)

OTOH,您可以限制您接触几个您调用以运行测试的接口.如果您正在编写需要集成到 IDE 中的内容,它会变得更复杂一些,但无论您如何做都是如此.

OTOH, you can limit your exposure to a couple of interfaces you call to run tests. If you are writing something that needs to integrate in an IDE, it gets a bit more complex but that will be the case no matter how you do it.

如果您决定使用 API,请遵循 Rob 的建议,并在出现细节时到我们的论坛讨论.

If you decide to use the API, follow Rob's advice and come to our forum to discuss details as they arise.

这篇关于向后兼容 C# 反射和 NUnit 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆