Click here to Skip to main content
14,932,457 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to test c# project methods. For that, I have to write many unit test methods.
Is there any other way to test that method without writing any extra code

What I have tried:

C#
internal class Program
{
        private static void Main(string[] args)
        {

        }

        private static void MethodToTest()
        {

        }
}
Posted
Updated 16-Mar-21 4:50am

Quote:
Is there any other way to test that method without writing any extra code
As a matter of fact, your methods and all the C# code are "tested" by the compiler for certain things, and the most important one being type safety. Try updating the method to:
C#
private static string MethodToTest() {
}
And you will see that certain "tests" will not pass. For more advanced tests, try using an inheritance construct with your Program type, and see what the compiler says.

As for logical tests (for what goes inside the method), that is not something that your compiler understands. For that, you need to write custom tests where you tell the testing suite how to verify that the program works as expected.
   
Comments
BillWoodruff 16-Mar-21 19:29pm
   
+5
I don't think there is a "magical solution", but you could try ReSharper, see:
refactoring-plugins-for-visual-studio[^]
It usually finds "code smells" while you are typing.
   
Comments
BillWoodruff 16-Mar-21 19:31pm
   
+5 ReSharper is not free, but, I can't imagine coding in C# washout it :)
Quote:
Is there any other way to test that method without writing any extra code

I fear the only way is you doing extensive tests manually, soon, you will understand that it takes much more time than writing testing code.
In real life, any project evolve during years.
Do you expect yourself to remember all tests for all project you work on ?

Writing extensive test code is the only reasonable solution.
   
Debug Method Helper(DMH) visual studio extension may help to solve your problem.
see: Debug Method helper(DMH)
   
Comments
Balaji 35 16-Mar-21 11:15am
   
Looks good. Need more testing.
https://marketplace.visualstudio.com/items?itemName=BalajiPriya.DebugMethodHelper

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900