locked
how to do code coverage Test on Visual Studio 2017 Enterprise? RRS feed

  • Question

  • I Want to Know how many codes are excuted in visual studio 2017 enterprise

    example

    int main()

    {

        int a=0;

        if(a==0)

        {

            printf("here");

        }

        else

        {

            printf("???");

        }

        return 0;

    }

    i expect Bolded Line is Not excuted ,

    Thursday, August 17, 2017 10:21 AM

Answers

  • Hi joeswish,

    Glad to see you.

    Based on my knowledge, we could analyze code coverage on unit test in Test Explorer.

    So, if you want to do code coverage on visual studio 2017 enterprise. Please first write the unit test for the functional method (has input and output, and they have fixed relationship) you want to test in visual studio 2017 enterprise. Then, choose Analyze Code Coverage on the Test menu after ran your unit test code in Test Explorer.

    You could refer to this link to get more details about“Using Code Coverage to Determine How Much Code is being Tested”: https://msdn.microsoft.com/en-us/library/dd537628.aspx

    >> i expect Bolded Line is Not excuted

    Based on your code example, the bold part of course not executed.

    And I don’t know what you want to test. I only find a fixed output, no input found. And the main() is a convention entry method. So, this example doesn't count as a functional method.

    Thanks for your understanding.

    Regards,

    Judyzh

    • Marked as answer by joeswish Wednesday, August 23, 2017 8:10 AM
    Friday, August 18, 2017 6:33 AM
  • Hi friend,

    Thanks for your posting.

    To use Code Coverage you need a Test project. (A project under test and a unit test project like in my side):

    I will suggest build a Console project which is under test, and a unit test project to test the function as you gave the example above.

    In the Console project my code is:

    public class Trigonometric
        {
            static void Main(string[] args)
            {
                
                
                Trigonometric.outputstr(0);
                Console.ReadKey();
                
    
            }
    
    
    
            public static void outputstr(int a)
            {
    
    
                if (a == 0)
    
                {
    
                    Console.WriteLine("here");
                     
    
                }
    
                else
    
                {
    
                    Console.WriteLine("???");
                   
    
                }
    
    
    
            }
    }

    and the test code is just simple like:

            [TestMethod]
            public void TestMethod1()
            {
                Trigonometric.outputstr(0);
    
                
    
            }

    When I run the test I got it in the test explorer, and I could right-click the Testmethod,

    choose Analyze Code coverage for selected tests , after you will get the code coverage result, and you click the button:

    you will see color displays the coverage of code(with foreground color):

    Hope it helps.

     

    Best regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    • Edited by Fletch Zhou Friday, August 18, 2017 8:24 AM
    • Marked as answer by joeswish Wednesday, August 23, 2017 8:10 AM
    Friday, August 18, 2017 7:48 AM
  • Hi joeswish,

    Thanks for your response.

    Unfortunately, there is no that tool in VS2017 for now, but you may Go to Help-> Send feedback -> Provide a Suggestion  

    Thanks for your understandings.

     

    Bes regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Monday, August 21, 2017 2:05 AM
    • Proposed as answer by Fletch Zhou Tuesday, August 22, 2017 9:15 AM
    • Marked as answer by joeswish Wednesday, August 23, 2017 8:10 AM
    Monday, August 21, 2017 2:05 AM

All replies

  • Hi joeswish,

    Glad to see you.

    Based on my knowledge, we could analyze code coverage on unit test in Test Explorer.

    So, if you want to do code coverage on visual studio 2017 enterprise. Please first write the unit test for the functional method (has input and output, and they have fixed relationship) you want to test in visual studio 2017 enterprise. Then, choose Analyze Code Coverage on the Test menu after ran your unit test code in Test Explorer.

    You could refer to this link to get more details about“Using Code Coverage to Determine How Much Code is being Tested”: https://msdn.microsoft.com/en-us/library/dd537628.aspx

    >> i expect Bolded Line is Not excuted

    Based on your code example, the bold part of course not executed.

    And I don’t know what you want to test. I only find a fixed output, no input found. And the main() is a convention entry method. So, this example doesn't count as a functional method.

    Thanks for your understanding.

    Regards,

    Judyzh

    • Marked as answer by joeswish Wednesday, August 23, 2017 8:10 AM
    Friday, August 18, 2017 6:33 AM
  • Hi friend,

    Thanks for your posting.

    To use Code Coverage you need a Test project. (A project under test and a unit test project like in my side):

    I will suggest build a Console project which is under test, and a unit test project to test the function as you gave the example above.

    In the Console project my code is:

    public class Trigonometric
        {
            static void Main(string[] args)
            {
                
                
                Trigonometric.outputstr(0);
                Console.ReadKey();
                
    
            }
    
    
    
            public static void outputstr(int a)
            {
    
    
                if (a == 0)
    
                {
    
                    Console.WriteLine("here");
                     
    
                }
    
                else
    
                {
    
                    Console.WriteLine("???");
                   
    
                }
    
    
    
            }
    }

    and the test code is just simple like:

            [TestMethod]
            public void TestMethod1()
            {
                Trigonometric.outputstr(0);
    
                
    
            }

    When I run the test I got it in the test explorer, and I could right-click the Testmethod,

    choose Analyze Code coverage for selected tests , after you will get the code coverage result, and you click the button:

    you will see color displays the coverage of code(with foreground color):

    Hope it helps.

     

    Best regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    • Edited by Fletch Zhou Friday, August 18, 2017 8:24 AM
    • Marked as answer by joeswish Wednesday, August 23, 2017 8:10 AM
    Friday, August 18, 2017 7:48 AM
  • thanks for reply

    actually i want to test my project(MFC - Dialog Base)

    in other coverage tool(like code scroll) , tool makes test case automatically,  and I just run the test case

    so easily reached 100% code coverage

    i expect  vs2017 ent can provide easy solution like code scoll(dynamic code coverage tool)

    but accoding to your reply, VS 2017 can't provide these function, 

    for coveragage test, i should make unit test manually. ㅠㅠ

     

    Friday, August 18, 2017 10:03 PM
  • Hi joeswish,

    Thanks for your response.

    Unfortunately, there is no that tool in VS2017 for now, but you may Go to Help-> Send feedback -> Provide a Suggestion  

    Thanks for your understandings.

     

    Bes regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Monday, August 21, 2017 2:05 AM
    • Proposed as answer by Fletch Zhou Tuesday, August 22, 2017 9:15 AM
    • Marked as answer by joeswish Wednesday, August 23, 2017 8:10 AM
    Monday, August 21, 2017 2:05 AM