0

I would like to enhance efficiency in our software department by providing a VS extension or add in to perform custom code coverage using VS 2013.

The special point in that enterprise is the coverage of special code snippet actually marked by a comment like //INDISPENSABLE_STUFF document comment that is defined as a task token.

I was thinking of introducing a new attribute RiskAttribute to qualify methods for instance but could not separate the corresponding code coverage result from the rest for reporting how much of the indispensable stuff has been covered.

    /// <summary>
    /// Risk Attribute
    /// </summary>
        public class RiskAttribute : Attribute
        {
            private readonly string _document;
            private readonly string _comment;

            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="doc"></param>
            /// <param name="comment"></param>
            public RiskAttribute(string doc, string comment)
            {
                _document = doc;
                _comment = comment;
            }

            /// <summary>
            /// Document
            /// </summary>
            public string Document { get { return _document; } }

            /// <summary>
            /// Comment
            /// </summary>
            public string Comment { get { return _comment; } }
    }

I turned to implement an add in but do not figure out in the 1. step how to launch the vs code coverage tool programmatically for a project using my add-in. The 2. step would be to customize the code coverage in order to parse the custom task token and return a result like xy% of defined task token INDISPENSABLE_STUFF is covered.

For development, I implemented a calculator class with unit test to exercise both variations:

  • The class

    /// <summary>
    /// Calculator
    /// </summary>
    public class Calculator
    {
    
    /// <summary>
    /// Addition
    /// INDISPENSABLE_STUFF (DOC #2150) Test
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>        
    public static decimal Add(decimal a, decimal b) { return a + b; }
    
    /// <summary>
    /// Substraction
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>
    [RiskAttribute("DOC #2150", "Test")]
    public static decimal Sub(decimal a, decimal b) { return a - b; }
    
    }
    
  • The unit test

    /// <summary>
    /// Test class for Calculator
    /// </summary>
    [TestClass]
    public class CalculatorTest
    {
        /// <summary>
        /// Tests addition
        /// </summary>
        [TestMethod]
        public void Test_Sub()
        {
            decimal a = 1;
            decimal b = 2;
            Assert.AreEqual((a - b), Calculator.Sub(a, b));
        }
    }
    

So now, for the question, I already have to add-in code for the integration but miss the main content to start the customized code coverage. Does anybody know :

  1. how to adapt the code coverage to consider task token?
  2. how to start the code coverage programmatically?

Sorry if these are "big" points for only "one" question.

Thanks

user3840527
  • 71
  • 1
  • 2
  • Could you not use a code coverage tool to get your coverage for the whole application and then take the produced coverage report and do reflection to find coverage for the classes/methods with your `RiskAttribute` to produce a second report? – Shaun Wilde Sep 17 '15 at 23:16
  • That sounds reasonable, but can you be more explicit with an example please, since I could not figure out, how to even get the MS code coverage tool starting programmatically? – user3840527 Sep 18 '15 at 08:46
  • http://stackoverflow.com/questions/4951089/how-to-use-ms-code-coverage-tool-in-command-line or use a 3rd party tool like [OpenCover](https://github.com/OpenCover/opencover) or NCover. Read output from said tool (usually in XML). Use reflection to find all methods in code with attribute. Correlate the two. I am sure you'll find the experience a valuable learning experience. – Shaun Wilde Sep 18 '15 at 22:24
  • I found out how to start the unit test with the vstestconsole.exe that can execute the code coverage as option at the same time. I am going to use reflection as you suggested to retrieve the classes with the wanted attribute. Shaun, do you eventually know which vs tool is in charge of parsing task token and show the result in the task list ? – user3840527 Sep 25 '15 at 08:21

0 Answers0