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.
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.
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.
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.
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!
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.
Based on Rafael answer:
private static boolean isDebug() {
return ManagementFactory.getRuntimeMXBean().getInputArguments().stream().anyMatch(arg->arg.contains("jdwp=") && arg.contains("suspend=y"));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With