How does Eclipse actually run Junit tests?

I run the divergence when running Junit tests in Eclipse and Ant. Here is the scenario:

In Eclipse, everything worked as intended, however, I could not get an accurate Junit report when I ran the Ant build script, which I whipped. I made a couple of changes in our test application and test cases (in a nutshell I added the Test Suite () method to all my test cases), which returns a new Junit4TestAdapter, and our custom runner executed RunNotifier.fireTestAssumptionFailed (Failure) instead of fireTestAssumption. Now everything works fine in Ant, but errors are marked as reported at startup in Eclipse.

Is there any documentation for Eclipse that explains exactly how it runs Junit tests? I mainly try to know exactly how Eclipse runs Junit tests, regardless of whether it runs directly through Ant, if it uses Java to interact with Junit, etc. If someone knows the actual solution to the problem, I also welcome this, but I would really like to try to solve this problem myself, I just need a point in the right direction.

+6
source share
2 answers

To answer your problem first, if you have a discrepancy between Ant junit and Eclipse JUnit, this is probably a class problem or environmental issue. The easiest way is to find a test that runs in different ways between them and print the properties of the system, and work with it from this direction. Another thing is to try to run Ant scripts from Eclipse to make sure that it matters (as the environment will change)

Eclipse does not use Ant to run tests.


As for how Eclipse runs JUnit tests, here is a quick overview. Be careful: the Eclipse JUnit plugin has some deep magic.

Eclipse has 4 JUnit plugins that are installed by default in most configurations:

  • org.eclipse.jdt.junit: git: //dev.eclipse.org/org.eclipse.jdt/org.eclipse.jdt.junit.git
  • org.eclipse.jdt.junit.core: git: //dev.eclipse.org/org.eclipse.jdt/org.eclipse.jdt.junit.core.git
  • org.eclipse.jdt.junit.runtime: git: //dev.eclipse.org/org.eclipse.jdt/org.eclipse.jdt.junit.runtime.git
  • org.eclipse.jdt.junit4.runtime: git: //dev.eclipse.org/org.eclipse.jdt/org.eclipse.jdt.junit4.runtime.git

This is a git mirror of the actual CVS repositories. The last time I tried to use them, they did not compile, but they will give you the code, and you can at least import projects into Eclipse and look at them.

If we ignore the configuration pages, how the plugin creates launch configurations, the code for the JUnit view itself, and how it finds the appropriate tests to run, we can focus on how it runs the tests.

The key classes are org.eclipse.jdt.junit.launcher.JUnitLaunchConfigurationDelegate and org.eclipse.jdt.internal.junit.runner.RemoteTestRunner . JUnitLaunchConfigurationDelegate reads the startup configuration and expands the JVM in which the tests will be run. The main class for this new JVM is RemoteTestRunner . Those tests that must be run are passed as parameters for the forked JVM, either as a single test, or as a list of tests in a temporary file if you run Run as JUnit in a project. If you are debugging, this new JVM can be saved by checking the Keep alive when debugging checkbox in the launch configuration. In this case, the JVM will stick, and retries of existing tests will be sent via sockets.

RemoteTestRunner runs the tests and passes the results through sockets to Eclipse, which then updates the JUnit view. The heart of this is org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference , which runs the test (for JUnit 4) and org.eclipse.jdt.internal.junit4.runner.JUnit4TestListener , which is the RunListener for these tests. JUnit4TestListener extends RunListener , but does not override testAssumptionFailure , which is probably due to the fact that your tests pass in Eclipse. RunListener.testAssumptionFailure is an empty method, it does nothing, so your notifications will be ignored.

I would start cloning git repositories by importing projects into Eclipse and trying to work through code.

+17
source

Download plugin development tools (PDTs) for Eclipse.

Run the plugin project and add org.eclipse.jdt.internal.junit.runner and org.eclipse.jdt.junit.core to your plugin.xml dependencies ..

Explore these classes and you will understand how JUnit is launched. It does not use Ant at all.

To see how failures are flagged and process reporting, you should take a look at org.eclipse.jdt.internal.junit4.runtime if you are using JUnit 4.

In general, be prepared to spend time learning and developing the plugin if you want to change the way Eclipse JUnit works.

+2
source

Source: https://habr.com/ru/post/900056/


All Articles