0

I'm looking for ways to quickly debug the code I've just written to see if it behaves correctly and all my assumptions are correct. I don't want to run the full system yet because parts of infrastructure to get to this code are missing. I use unit testing for it but I find it cumbersome. Is there anything better?

Objectives are:

  • Debug any code quickly without creating additional projects, applications etc.
  • Easily repeat debugging.
  • The test is for this specific purpose, might use fixed file paths, database connections, anything. Typically thrown away after getting things right.
  • Need to access internal members of my objects.
  • Accessing private member would be great benefit.
  • I'm fine with writing test functions directly in my object. Actually this would be preferred.

The dream way of doing it would be:

namespace Aaa
{
    class SomeClass
    {
        public string Name { get; private set; }

        public SomeClass(string name, int value)
        {
            this.Name = name;
            InitializeSth();
        }

        public DoSomethingPublic()
        {
            // ...
        }

        private DoSomethingPrivate()
        {
            // ...
        }

        public static void TestThis()         // <-- debug this
        {
            var obj = new SomeClass("a", 1);  // <-- put breakpoint here
            obj.DoSomethingPublic();
            obj.DoSomethingPrivate();
        }
    }
}

This is possible in Java and is such a great thing. This allows for accessing private things too.

But I'm open to other options as well. Is there anything like this in VS2015?

What I have tried so far:

  • Immediate Window - I don't think it can be configured for such purpose
  • C# Interactive - this doesn't seem to support debugging. Or does it?
  • Unit testing - this is what I use now (with MSTest). But I find it very cumbersome, because:
    • I need to create new projects, or include references to MS testing assemblies
    • I need to make extra steps to access internal types and members, or change things to public (I don't like this).
    • Even more steps to access private members.
    • I mess with other tests if Unit Testing is used in the project.
    • Starting debugging again needs many clicks instead of sth+sth+F5.
    • There are some workarounds for some of these items, but in general the testing infrastructure seems to be made for different purposes and I always have a feeling I'm fighting against it.

I also found some information about Resharper having ability to debug any static function. But I don't want to use Resharper, mainly because of performance.

Arek
  • 1,276
  • 1
  • 10
  • 19
  • Hi Arek, what about this issue? Could you get useful information from our suggestions? Any update?:) – Jack Zhai Dec 06 '16 at 09:45
  • I think I found some way but I need to test it. Let me come back from my winter holiday, I will post something next week. – Arek Dec 08 '16 at 16:52
  • I look forward to hearing from you. if you get any better solution, please feel free to share the answer, of course, if any reply is helpful for you, you could also mark it as the answer, have a nice holiday:) – Jack Zhai Dec 09 '16 at 01:18

3 Answers3

1

To debug one method without running the whole app, I often use the unit test in VS IDE, since it would not really impact our app's development.

I also got the method using the Resharper tool before: https://blog.jetbrains.com/dotnet/2015/08/28/run-configurations-debug-any-static-method-in-visual-studio-and-more/

In VS IDE, to debug the method directly without debugging/running the app, it really has no better suggestions than unit test project.

Of course, it also has other third party tool if you want to debug code without running the app:

http://www.linqpad.net/

Jack Zhai
  • 6,230
  • 1
  • 12
  • 20
  • Jack: as I wrote in the question, I already use unit testing, but find it not optimal and look for better options. Also the Resharper method is mentioned in a question. – Arek Dec 12 '16 at 09:33
  • Thanks for your response, Arek, no other better workaround, as you said that you use the unit test, one better way during using the unit test, you could use the new feature "live unit test"in VS2017 RC: https://blogs.msdn.microsoft.com/visualstudio/2016/11/18/live-unit-testing-visual-studio-2017-rc/. Or use the debugging feature in VS IDE with the "Edit and Continue" function, so you could edit the code in debugging time. If it really doesn't meet your requirement, I can help you submit a feature request to the product team. – Jack Zhai Dec 12 '16 at 10:05
0

Take a look at this answer: #if DEBUG vs. Conditional(“DEBUG”)

Basically you could use something like the following:

#if DEBUG
public void DoSomething() { }
#endif

public void Foo()
{
    #if DEBUG
    DoSomething(); 
    #endif
}
Community
  • 1
  • 1
PmanAce
  • 4,000
  • 2
  • 24
  • 29
0

Answering my own question - it seems I didn't evaluate Immediate Window enough.

It is possible to invoke any static method either using

  • Immediate Window (just type TestThis())
  • Command Window (type ?TestThis() or Debug.EvaluateStatement TestThis())

This even allows to invoke private static methods. The projects will be built before starting the method if they need to be and the method is executed in debugger, so it will stop at any breakpoint.

So summarizing: press Alt+Ctrl+I to get to the Immediate Window, type-in method name with parentheses and press Enter.

There are two disadvantages:

  • If the method is not in main class (one with Main method), you have to use full namespace and class name, for example MyCompany.MyApp.Logic.SomeClass.TestMethod() to run it
  • to start debugging again, you have to go to immediate window and recall last command with Up-Arrow, Enter. This is better than right-clicking on a test and selecting "Debug this test", but not optimal.

Please add an answer if you know anything better or some ways to make these two issues better. I looked for quite some time to find a keyboard shortcut to repeat last Immediate Window command, but cannot find one. I don't mark this answer as accepted for now, in case something better is posted.

Arek
  • 1,276
  • 1
  • 10
  • 19
  • actually it would be a better solution than other workarounds, of course, you could also use the command line windows to execute commands or aliases directly in the VS IDE, of course, it really has the different functions: http://stackoverflow.com/questions/1557371/visual-studio-command-window. I suggest you mark it as the temporary answer before other community members could provide other answer:). Have a nice day! – Jack Zhai Dec 13 '16 at 11:38
  • OK did that. Thanks for useful links. I wish it would be possible to quickly bind some keyboard key to a command with parameters, so I could bind ie. `sth+sth+F5` to `Debug.EvaluateStatement SomeNamespace.SomeClass.SomeMethod` which would work like _selecting current test method_. – Arek Dec 14 '16 at 12:12