Runner.Load fails on CF if the test assembly contains a generic method

Bug #1071343 reported by Jan Šotola
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
NUnit Framework
Fix Released
High
Charlie Poole
NUnitLite
Fix Released
High
Charlie Poole

Bug Description

For our .NET CF projects, we use our own GUI test runner. It loads the test assemblies using the NUnitLiteTestAssemblyRunner.Load() method.

Everything works well until the test assembly does not contain a non-generic method which has an ancestor with either a generic argument or a generic return value.

Here is an example of such class structure and method:

 public abstract class BaseClass<T>
 {
  public abstract void GenericMethod(T par);
 }

 public class DerivedClass: BaseClass<string>
 {
  public override void GenericMethod(string par)
  {
  }
 }

Note: the class need not be a test fixture at all - the AssemblyBuilder.Build searches methods within all classes (regardless the TestFixture attribute).

The problem is unfortunately not directly in NUnitFramework but in the system method RuntimeMethodInfo.IsDefined. This method (if called with a parameter inherit = true) searches the attribute in all ancestors of the examined method. And if any ancestor has a generic argument or a generic return type, the IsDefined method fails with ArgumentNullException.

Here is the complete exception:

System.ArgumentNullException was unhandled
  Message="ArgumentNullException"
  StackTrace:
       at System.Reflection.RuntimeMethodInfo.GetParentDefinition()
       at System.Reflection.CustomAttribute.IsDefined(MemberInfo member, Type caType, Boolean inherit)
       at System.Reflection.RuntimeMethodInfo.IsDefined(Type attributeType, Boolean inherit)
       at NUnit.Framework.Internal.Reflect.HasMethodWithAttribute(Type fixtureType, Type attributeType, Boolean inherit)
       at NUnit.Framework.Builders.NUnitTestFixtureBuilder.CanBuildFrom(Type type)
       at NUnit.Framework.Internal.TestFixtureBuilder.CanBuildFrom(Type type)
       at NUnit.Framework.Internal.NUnitLiteTestAssemblyBuilder.GetFixtures(Assembly assembly, IList names)
       at NUnit.Framework.Internal.NUnitLiteTestAssemblyBuilder.Build(Assembly assembly, IDictionary options)
       at NUnit.Framework.Internal.NUnitLiteTestAssemblyRunner.Load(Assembly assembly, IDictionary settings)

I'm attaching a simple CF project (for Visual Studio 2008, including sources of NUnitFramework 0.8).
The reproduction is within the GUI Form1.cs - there are two reproductions:
1) a failure using the Load method of NUnitLite
2) a pure failure of the system IsDefined method

I know the problem is in .NET CF framework. Since we cannot expect Microsoft will ever fix it, it would be great to put some workaround to NUnitLite.

In our company we use just the TestAttribute on methods (we don't use the TestCaseAttribute, TestCaseSourceAttribute or TheoryAttribute). So we can afford to limit the HasMethodWithAttribute to search only void methods without arguments (AFAIK only such methods can have the TestAttribute).

But in the NUnitLie framework it is not possible so I have only some tips for the workaround:
A) I was thinking about writing a custom IsDefined method, but it would be quite complex problem.
B) Or maybe a searching of ancestor methods would be a little bit more limited. For instance, to check whether the method's base definition is generic using methodInfo.GetBaseDefinition().IsGenericMethodDefinition(). And then exclude those methods from inherit attribute searching.

Related branches

Revision history for this message
Jan Šotola (jansotola) wrote :
Changed in nunitlite:
status: New → Triaged
importance: Undecided → High
Changed in nunitlite:
assignee: nobody → Charlie Poole (charlie.poole)
milestone: none → 1.0
Revision history for this message
Charlie Poole (charlie.poole) wrote :

It turns out that the true parameter is not needed in this situation and was just added without much thought in the past!

Changed in nunitlite:
status: Triaged → Fix Committed
Changed in nunit-3.0:
status: New → Triaged
importance: Undecided → High
assignee: nobody → Charlie Poole (charlie.poole)
milestone: none → 2.9.6
Changed in nunitlite:
milestone: 1.0 → 1.0b2
Changed in nunitlite:
status: Fix Committed → Fix Released
Changed in nunit-3.0:
status: Triaged → Fix Committed
Changed in nunit-3.0:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.