Backwards compatibility with C# Reflection and NUnit Versions

徘徊边缘 提交于 2019-12-13 21:17:26

问题


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.

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.

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

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

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

Thanks in advance for any help!


回答1:


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 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.

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.

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, 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.

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



来源:https://stackoverflow.com/questions/37995181/backwards-compatibility-with-c-sharp-reflection-and-nunit-versions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!