For those that don’t know, the IntelliJ Platform comes with a test framework we can use to test language features added by plugins. The tests themselves are functional tests – we run an in-memory, headless version of the IDE that can load test data, such as projects and source files, execute a feature on those files (such as highlighting) and then compare the output of the feature with known good files. If the files match, the test passes. If they don’t, the test fails. More details on this approach can be found in the SDK documentation.
This approach to testing is very powerful, and abstracting our tests away from implementation details has proven very useful many times. However, there are some downsides to working with external files for input and expected output. One of which is quickly navigating between the files.
IDEA and the IntellIJ Platform have a couple of features to make it easy to navigate to your test data files. The first is the addition of a Navigate to testdata action in the popup menu for the Run Test Method gutter icon.
This is also available from the popup menu in the Run Tests window.
You can also use the Related Symbols action on the test method.
The second navigation feature is a Go to <testdata directory> action, available in the popup menu under the Run Testcase gutter icon. This looks for a @TestDataPath annotation, and will navigate to the directory in the Project tool window. You can also invoke Goto Declaration on the path in the string literal.
Minor Improvements and Fixes
The EAP also includes fixes for several annoying bugs, and some UI improvements.
- In the Navigate to testdata popup, the path to the test data file is now relative to the project/module.
- The test data split editor can now be opened from the Navigate to testdata popup, even if there are other files present – the before/after pair is shown as a single entry in the popup list of files.
- Any files that don’t exist are shown greyed out.
- If one or more test data file doesn’t exist, there is now a Create missing files option in the popup. This was previously only shown if the test data files for the test were a single before/after pair.
And there are some improvements in detecting the test data files for a test method:
- If the test case class does not have the @TestDataPath annotation, it is no longer necessary for the test data subdirectory name to exactly match the test case class with he Test suffix removed. A more relevant directory will be chosen during the scanning for test data files.
- The before/after files can now use before and after as a prefix, and not just as a suffix. For instance, beforeMyQuickfix.java and afterMyQuickfix.java.
Tips for Using Test Data
To enable navigation, the IntelliJ Platform needs to be able to work out where your test data lives. You can do this either explicitly, using the @TestDataPath annotation on your class, or by convention, naming your test data directory after the test class. For example, if your class is called StreamToLoopInspectionTest, then you can name your test data subdirectory streamToLoop.
Another useful tip is to make sure you name your before/after test data files properly. For example, myQuickFix_before.java and myQuickFix_after.java. The IntelliJ Platform will use a split editor to make it easier to view and edit both files.
If you have any suggestions for what else could be improved please share in the comments or in YouTrack.