4.4 KiB
This page lists a number of common questions/issues and techniques useful for testing plugins.
Useful Classes
UsefulTestCase
PlatformTestUtil
CodeInsightTestUtil
EditorTestUtil
PsiTestUtil
VfsTestUtil
ProjectViewTestUtil
TestLookupElementPresentation
Issues
How to avoid blinking tests?
- Always call
super.tearDown()
insidefinally {..}
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). UseinvokeLater(runnable, myProject.getDisposed()
.
How to avoid test failure when using resources?
In some situations, added or changed files (e.g. DTDs provided by plugin) are not refreshed in VFS. In such cases, simply delete test-system/caches in your sandbox directory and try again.
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 per-test basis?
Use UsefulTestCase.getTestName()
or create your own annotation(s) which can be checked via UsefulTestCase.annotatedWith()
.
How to run 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 wait for specified amount of time?
Use com.intellij.util.TimeoutUtil.sleep()
.
JVM: How to add Maven dependencies?
Use MavenDependencyUtil
.