79593882

Date: 2025-04-26 11:48:44
Score: 1
Natty:
Report link

thanks,

CoreExtensions.Host.InitializeService();

would have done trick, but didnt work for me as i want to use nunit 2 which is shipped with mono 4.5. When calling the InitializeService I run into a filenotfound exception because it could not find the system.runtime.configuration.dll. Although there was a try catch around it, it seems that exceptions caused by dlls that are referenced and cannot be loaded cannot be catched.

one solution around this was:

public class NUnitTestCaseBuilderFixed: AbstractTestCaseBuilder
{
    public override bool CanBuildFrom(MethodInfo method)
    {
        if (Reflect.HasAttribute(method, "NUnit.Framework.TestAttribute", inherit: false))
        {
            return true;
        }
        return false;
    }

    protected override NUnit.Core.TestCase MakeTestCase(MethodInfo method)
    {
        return new NUnitTestMethod(method);
    }

    protected override void SetTestProperties(MethodInfo method, NUnit.Core.TestCase testCase)
    {
        NUnitFramework.ApplyCommonAttributes(method, testCase);
        NUnitFramework.ApplyExpectedExceptionAttribute(method, (TestMethod)testCase);
    }
}

public class NUnitTestFixtureBuilderFixed: AbstractFixtureBuilder
{
    public NUnitTestFixtureBuilderFixed()
    {
        testCaseBuilders.Install(new NUnitTestCaseBuilderFixed());
    }

    protected override TestSuite MakeSuite(Type type)
    {
        return new NUnitTestFixture(type);
    }

    protected override void SetTestSuiteProperties(Type type, TestSuite suite)
    {
        base.SetTestSuiteProperties(type, suite);
        NUnitFramework.ApplyCommonAttributes(type, suite);
    }

    public override bool CanBuildFrom(Type type)
    {
        return Reflect.HasAttribute(type, "NUnit.Framework.TestFixtureAttribute", inherit: true);
    }

    protected override bool IsValidFixtureType(Type fixtureType, ref string reason)
    {
        if (!base.IsValidFixtureType(fixtureType, ref reason))
        {
            return false;
        }

        if (!fixtureType.IsPublic && !fixtureType.IsNestedPublic)
        {
            reason = "Fixture class is not public";
            return false;
        }

        if (CheckSetUpTearDownMethod(fixtureType, "SetUp", NUnitFramework.SetUpAttribute, ref reason) && CheckSetUpTearDownMethod(fixtureType, "TearDown", NUnitFramework.TearDownAttribute, ref reason) && CheckSetUpTearDownMethod(fixtureType, "TestFixtureSetUp", NUnitFramework.FixtureSetUpAttribute, ref reason))
        {
            return CheckSetUpTearDownMethod(fixtureType, "TestFixtureTearDown", NUnitFramework.FixtureTearDownAttribute, ref reason);
        }

        return false;
    }

    private bool CheckSetUpTearDownMethod(Type fixtureType, string name, string attributeName, ref string reason)
    {
        int num = Reflect.CountMethodsWithAttribute(fixtureType, attributeName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, inherit: true);
        if (num == 0)
        {
            return true;
        }

        if (num > 1)
        {
            reason = $"More than one {name} method";
            return false;
        }

        MethodInfo methodWithAttribute = Reflect.GetMethodWithAttribute(fixtureType, attributeName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, inherit: true);
        if (methodWithAttribute != null && (methodWithAttribute.IsStatic || methodWithAttribute.IsAbstract || (!methodWithAttribute.IsPublic && !methodWithAttribute.IsFamily) || methodWithAttribute.GetParameters().Length != 0 || !methodWithAttribute.ReturnType.Equals(typeof(void))))
        {
            reason = $"Invalid {name} method signature";
            return false;
        }

        return true;
    }
}

and

        var fixtureBuilder = new NUnitTestFixtureBuilderFixed();
        var setUpFixtureBuilder = new SetUpFixtureBuilder();
        CoreExtensions.Host.FrameworkRegistry.Register("NUnit", "nunit.framework");
        ((ExtensionPoint)CoreExtensions.Host.SuiteBuilders).Install(fixtureBuilder);                             
((ExtensionPoint)CoreExtensions.Host.SuiteBuilders).Install(setUpFixtureBuilder);
Reasons:
  • Blacklisted phrase (0.5): thanks
  • RegEx Blacklisted phrase (1): i want
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Clemens Pecinovsky