Click here to Skip to main content
15,895,746 members
Articles / Desktop Programming / WPF

Catel - Part 4 of n: Unit testing with Catel

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
28 Jan 2011CPOL11 min read 49.2K   572   11  
This article explains how to write unit tests for MVVM using Catel.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CoverageExcludeAttribute.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Use this enum to provide a valid reason for excluding coverage. Expand this enum
//   if you encounter a new type of reason.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;

namespace Catel
{
    /// <summary>
    /// Use this enum to provide a valid reason for excluding coverage. Expand this enum 
    /// if you encounter a new type of reason. 
    /// </summary>
    internal enum ExcludeReason
    {
        /// <summary>
        /// Static singleton will only be covered in production scenarios.
        /// </summary>
        StaticSingletonWillOnlyBeCoveredInProductionScenario,

        /// <summary>
        /// Constructor will only be covered in production scenarios. 
        /// </summary>
        ConstructorWillOnlyBeCoveredInProductionScenario,

        /// <summary>
        /// Method will only be covered in production scenarios. 
        /// </summary>
        MethodWillOnlyBeCoveredInProductionScenario,

        /// <summary>
        /// Must be implemented in a future sprint.
        /// </summary>
        ToBeImplementedInFutureSprint,

        /// <summary>
        /// Property will be substituted during a test.
        /// </summary>
        PropertyWillBeSubstitutedInTest,

        /// <summary>
        /// Contains code which cannot be substituted in a test.
        /// </summary>
        ContainsCodeWhichCannotBeSubstitutedInTest,

        /// <summary>
        /// Interface will not be implemented in this class.
        /// </summary>
        InterfaceMethodWillNotBeImplementedInThisClass,

        /// <summary>
        /// This is test code, and therefore needs to be excluded.
        /// </summary>
        TestCode,

        /// <summary>
        /// Class will only be covered in production scenarios. 
        /// </summary>
        ClassWillOnlyBeCoveredInProductionScenario,

        /// <summary>
        /// This is a data type, and therefore needs to be excluded.
        /// </summary>
        DataType,

        /// <summary>
        /// This is a generated class, and therefore needs to be excluded.
        /// </summary>
        GeneratedClass,

        /// <summary>
        /// Native method will be covered in native unit tests.
        /// </summary>
        NativeMethodWillBeCoveredInNativeUnitTests,

        /// <summary>
        /// This object is deprecated, no need to test it any longer.
        /// </summary>
        Deprecated,

        /// <summary>
        /// This is debug logging, and therefore needs to be excluded.
        /// </summary>
        DebugLogging,

        /// <summary>
        /// Object is a non-used abstract implementation.
        /// </summary>
        NonUsedAbstractImplementation,

        /// <summary>
        /// Attribute is not covered by unit tests.
        /// </summary>
        Attribute
    }

    /// <summary>
    /// Use this to skip coverage for the method which is decorated with this 
    /// attribute. Use with care! 
    /// Do not put this attribute in a specific namespace.
    /// </summary>
    [CoverageExclude(ExcludeReason.TestCode)]
    internal class CoverageExcludeAttribute : Attribute
    {
        /// <summary>
        /// Reason why the object is excluded from coverage.
        /// </summary>
        private readonly ExcludeReason _reason;

        /// <summary>
        /// Initializes a new instance of the <see cref="CoverageExcludeAttribute"/> class.
        /// </summary>
        /// <param name="reason">The reason.</param>
        public CoverageExcludeAttribute(ExcludeReason reason)
        {
            _reason = reason;
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return base.ToString() + Environment.NewLine + _reason;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions