Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a debugger is attached during the execution of a unit test

Is there a way to check in JUnit code if the debugger is attached to the current test execution?
In .NET/C# i know this is possible with Debugger.IsAttached.

The use case would be to change or completly disable test timeouts when the debugger is attached because this is pretty annoying if you only have about 15 seconds (the defined timeout) for debugging a test.

like image 243
leozilla Avatar asked Sep 23 '14 08:09

leozilla


People also ask

Can you debug a unit test?

You can use Test Explorer to start a debugging session for your tests. Stepping through your code with the Visual Studio debugger seamlessly takes you back and forth between the unit tests and the project under test.

How do I run a JUnit test case in debug mode?

In the case of a test failure you can follow these steps to debug it: Double click the failure entry from the Failures tab in the JUnit view to open the corresponding file in the editor. Set a breakpoint at the beginning of the test method. Select the test case and execute Debug As>JUnit Test from the Debug drop down.

What is the way to debug code in test suite?

Debug mode​ Open a test case and switch to the Script view. Double-click on the leftmost side of the script editor to mark a breakpoint. A breakpoint is where Katalon Studio pauses the test execution for you to start debugging. Choose a browser for Debug from the main toolbar.

How do you debug a unit test in Python?

Using pytest --pdb If you run your tests with pytest --pdb, it will automatically drop into a debugger on every test that fails or has an error of some kind. If you love using the REPL, IPython or Jupyter in your normal work, then you know how handy it is to be in an interactive environment when trying to fix errors!


2 Answers

It is not a 100% fail-safe approach but you can check if the Java Debug Wire Protocol (JDWP) is active which is used by the debugger to connect to a JVM. This can be done by checking for input arguments to the JVM as for example in:

boolean isDebug() {
  for(String arg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
    if(arg.contains("jdwp=")) {
      return true;
    }
  }
  return false;
}

It might however return a false positive if anybody else named something jdwp or if the protocol was used for something else. Also, the command might change in the future what makes this hardly fail-safe. This approach is of course not JUnit specific but as JUnit does not have any privileges within a JVM, this is only natural.

like image 53
Rafael Winterhalter Avatar answered Oct 14 '22 17:10

Rafael Winterhalter


Based on Rafael answer:

    private static boolean isDebug() {
        return ManagementFactory.getRuntimeMXBean().getInputArguments().stream().anyMatch(arg->arg.contains("jdwp=") && arg.contains("suspend=y"));
    }
like image 25
surfealokesea Avatar answered Oct 14 '22 16:10

surfealokesea