Build/Test Issues

unable to use configuration element with default values

Started by blaz on 8,347 views

It seems that something in the way NCrunch executes tests causes problems with reflection. Consequently I'm unable to set or access configuration properties with declared default value on configuration elements.

All tests in example below fail with following exception when executing in NCrunch. All tests execute successfully in NUnit runner.
If property DefaultValue is removed from configuration element property, all tests with exception of 'AccessPropertyWithDefaultValue' execute successfully in NCrunch and NUnit.

Can this problem be fixed with appropriate configuration?


System.Reflection.CustomAttributeFormatException : 'DefaultValue' property specified was not found.
  ----> System.Reflection.CustomAttributeFormatException : 'DefaultValue' property specified was not found.
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimePropertyInfo property, RuntimeType caType)
   at System.Reflection.RuntimePropertyInfo.GetCustomAttributes(Type attributeType, Boolean inherit)
   at System.Attribute.InternalGetCustomAttributes(PropertyInfo element, Type type, Boolean inherit)
   at System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit)
   at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
   at System.Configuration.ConfigurationElement.CreateConfigurationPropertyFromAttributes(PropertyInfo propertyInformation)
   at System.Configuration.ConfigurationElement.CreatePropertyBagFromType(Type type)
   at System.Configuration.ConfigurationElement.PropertiesFromType(Type type, ConfigurationPropertyCollection& result)
   at System.Configuration.ConfigurationElement.get_Properties()
   at System.Configuration.ConfigurationElement.get_Item(String propertyName)
   at NCrunchBugs.SampleConfigurationElement.get_First() in c:\Users\blorger\AppData\Local\NCrunch\4828\16\NCrunchBugs\DefaultConfigurationValuesBug.cs:line 47
   at NCrunchBugs.DefaultConfigurationValuesBug.AccessPropertyWithDefaultValue() in c:\Users\blorger\AppData\Local\NCrunch\4828\16\NCrunchBugs\DefaultConfigurationValuesBug.cs:line 28
--CustomAttributeFormatException
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)



using System.Configuration;
using NUnit.Framework;

namespace NCrunchBugs
{
    [TestFixture]
    public class DefaultConfigurationValuesBug

    {
        [Test]
        public static void SetPropertyWithDefaultValue()
        {
            var s = new SampleConfigurationElement();
            s.First = "dd";
        }

        [Test]
        public static void SetPropertyWithoutDefaultValue()
        {
            var s = new SampleConfigurationElement();
            s.Second = "dd";
        }

        [Test]
        public static void AccessPropertyWithDefaultValue()
        {
            var s = new SampleConfigurationElement();
            Assert.That(s.First, Is.Not.Empty);
        }

        [Test]
        public static void AccessPropertyWithoutDefaultValue()
        {
            var s = new SampleConfigurationElement();
            Assert.That(s.Second, Is.Empty);
        }
    }

    public class SampleConfigurationElement : ConfigurationElement
    {
        private const string FirstProperty = "first";
        private const string SecondProperty = "second";

        [ConfigurationProperty(FirstProperty, DefaultValue = "something")]
        public string First
        {
            get { return (string)this[FirstProperty]; }
            set { this[FirstProperty] = value; }
        }

        [ConfigurationProperty(SecondProperty)]
        public string Second
        {
            get { return (string)this[SecondProperty]; }
            set { this[SecondProperty] = value; }
        }
    }
}
Interesting ... do you still have this problem if you turn off instrumentation in the project file under test? (Note that turning off instrumentation will also stop NCrunch from finding tests in the built assembly, so you'll need to have your tests in a different project to try this).
Thanks.

Turning off instrumentation solved the problem.
But I had to use attribute TestCase. Tests marked with attribute Test were not found when instrumentation was turned off. I guess this is another thing to fix for NUnit support.

I moved configuration element (SampleConfigurationElement) code to separate assembly. It is sufficient to disable instrumentation for this assembly. This is probably what you meant by "you'll need to have your tests in a different project".

Unfortunately this removes code coverage feedback for tested assembly. I hope original problem can be fixed.
Thanks, this helps narrow things down a great deal. This result tells me that the problem is being caused by instrumentation removing some of the metadata that your tests are relying on. I'll see what I can do about implementing a fix. Sorry for the trouble.

Post a reply

Log in to reply.