2022-02-09 10:10:48 +01:00

7.5 KiB

This page lists a number of common questions/issues and techniques useful for testing plugins.

Useful Classes

Issues

How to avoid blinking tests?

Always call super.tearDown() inside finally {..} block of your test class to avoid leaks and side effects from previously run (failed) tests.

Avoid OS-specific assumptions (e.g., filesystem case-sensitivity, hardcoded separator instead of java.io.File.separator).

Use ordered collections or UsefulTestCase.assertUnorderedCollection().

Code deferring execution (e.g., via Application.invokeLater()) might not run during test execution (and possibly fails in production, too). Use invokeLater(runnable, myProject.getDisposed().

How to avoid test failure when using resources?

In some situations, added or changed files (e.g. DTDs provided by a plugin) are not refreshed in VFS. In such cases, simply delete test-system/caches in your sandbox directory and try again.

How to enable DEBUG/TRACE logging?

Provide JVM system properties (Gradle: via systemProperty for test task) idea.log.debug.categories or idea.log.trace.categories, respectively. Multiple categories can set using a comma separated value list.

How to get separate logs for failing tests?

Set system property idea.split.test.logs to true to generate separate test log files in splitTestLogs subdirectory for failing tests (WARN/ERROR level messages) (2021.3).

Techniques

How to mark test-only elements in production code?

Annotate with org.jetbrains.annotations.TestOnly, usages will be highlighted via inspection JVM languages | Test-only usage in production code.

How to run tests for all files in a directory?

Use FileBasedTestCaseHelper, please see its javadoc for instructions.

How to modify setup on a per-test basis?

Use UsefulTestCase.getTestName() or create your own annotation(s) which can be checked via UsefulTestCase.annotatedWith().

How to run a performance test?

Use PlatformTestUtil.startPerformanceTest() to assert machine-adjusted metrics.

How to dispatch pending UI events?

Use PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue().

How to disable stderr logging?

Use DefaultLogger.disableStderrDumping() passing getTestRootDisposable().

How to register a resource (DTD, XSD) temporarily?

Use ExternalResourceManagerExImpl.registerResourceTemporarily() passing getTestRootDisposable().

How to replace component/service in tests?

Provide testServiceImplementation for service declaration in plugin.xml, or use ServiceContainerUtil.

How to replace extension points in tests?

Use ExtensionTestUtil.

How to wait for a specified amount of time?

Use com.intellij.util.TimeoutUtil.sleep().

How to test a JVM language?

Plugins supporting a JVM language may require JDK and language standard library to be set up in a test project, so the classes like java.lang.String can be correctly resolved during tests. Tests extending LightJavaCodeInsightFixtureTestCase use one of the mock JDKs distributed with the IntelliJ Community project sources (notice java/mockJDK-$JAVA_VERSION$ directories). These JAR files are not available in plugin project dependencies, so the IntelliJ Community sources must be checked out to the machine running the tests, and sources' location must be provided to the test framework. It's done by setting the idea.home.path system property to the absolute path of the checked out sources. In projects using Gradle it can be done by providing system property in the test task configuration:

test {
  systemProperty("idea.home.path", "/path/to/intellij-community")
}

The default JDK version used by the test framework depends on the target platform version and is the latest supported version. The easiest way to change the JDK version to the custom is overriding the LightJavaCodeInsightFixtureTestCase.getProjectDescriptor() method and using one of the project descriptors predefined in LightJavaCodeInsightFixtureTestCase. If a project descriptor requires more customizations, its getSdk() method can use one of the IdeaTestUtil.getMockJdk*() methods.

Sometimes testing a JVM language requires adding standard or other libraries to a test project. If a required library is available in the Maven repository, use MavenDependencyUtil, e.g.:

MavenDependencyUtil.addFromMaven(model, "org.jetbrains.kotlin:kotlin-stdlib:1.6.10");

If a required library is an unpublished JAR file, use PsiTestUtil.addLibrary() or addProjectLibrary() method and the JAR file path, e.g.:

PsiTestUtil.addLibrary(model, "kotlin-stdlib", getTestDataPath(), "kotlin-stdlib.jar");

If a topic you are interested in is not covered in the above sections, let us know via the "Was this page helpful?" feedback form below or other channels.

Please be specific about the topics and reasons for adding them, and leave your email in case we need more details.

{type="note"}