# Test Project and Testdata Directories

> For the complete documentation index, see [llms.txt](https://plugins.jetbrains.com/docs/intellij/llms.txt).

> **Note: Testing Tutorial and Code Samples**
> Check out [this step-by-step tutorial](writing-tests-for-plugins.html) teaching how to write and run automated tests for a custom language plugin.
>
>
>
> Also, code samples [comparing_string_references_inspection](https://github.com/JetBrains/intellij-sdk-code-samples/tree/main/comparing_string_references_inspection) and [conditional_operator_intention](https://github.com/JetBrains/intellij-sdk-code-samples/tree/main/conditional_operator_intention) demonstrate using tests.

> **Tip: Configuring Test Frameworks (2024.2+)**
> All required [test-framework dependencies](tools-intellij-platform-gradle-plugin-dependencies-extension.html#testing) must be declared explicitly.

The test fixture creates a test project environment. Unless the project creation is customized, the test project will have one module with one source root called `src`. The test project files exist either in a temporary directory or in an in-memory file system, depending on which implementation of [TempDirTestFixture](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/testFramework/src/com/intellij/testFramework/fixtures/TempDirTestFixture.java) is used.

[BasePlatformTestCase](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/testFramework/src/com/intellij/testFramework/fixtures/BasePlatformTestCase.java) uses an in-memory implementation; when setting up the test environment with `IdeaTestFixtureFactory.createCodeInsightFixture()`, the implementation is configurable.

> **Warning:**
> If tests use the in-memory implementation and their execution is aborted, the persisted filesystem caches may get out of sync with the in-memory structures, which may cause spurious errors in the tests. If an unexpected error occurs after a series of successful runs, try rerunning the test, and if that doesn't help, delete the "system" subdirectory in the [sandbox directory](ide-development-instance.html#the-development-instance-sandbox-directory).

## Testdata Files

A plugin usually stores the test data for its tests (such as files on which plugin features will be executed and expected output files) in the `testdata` directory. This is just a directory under the plugin's content root, but not under a source root. Files in `testdata` usually are not valid source code and must not be compiled.

To specify the location of `testdata`, override the `getTestDataPath()` method. The default implementation assumes running as part of the IntelliJ Platform source tree and is not appropriate for third-party plugins.

> **Note:**
> A widespread pattern in IntelliJ Platform tests is to use the test method's name being executed as the base for building the `testdata` file paths. This way, most of the code can be reused between different test methods that test various aspects of the same feature, and this approach is also recommended for third-party plugin tests. The name of the test method can be retrieved using `UsefulTestCase.getTestName()`.

> **Note:**
> If the plugin builds on top of Java support, see [How to test a JVM language?](testing-faq.html#how-to-test-a-jvm-language) to set up the test environment to obtain the required Mock JDK automatically.

To copy files or directories from the `testdata` directory to the test project directory, use the `copyFileToProject()` and `copyDirectoryToProject()` methods from [CodeInsightTestFixture](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/testFramework/src/com/intellij/testFramework/fixtures/CodeInsightTestFixture.java).

Most operations in plugin tests require a file open in the in-memory editor, in which highlighting, completion, and other operations will be performed. The in-memory editor instance is returned by `CodeInsightTestFixture.getEditor()`. To copy a file from the `testdata` directory to the test project directory and immediately open it in the editor, use the `CodeInsightTestFixture.configureByFile()` or `configureByFiles()` methods. The latter copies multiple files to the test project directory and opens the first of them in the in-memory editor.

Alternatively, use one of the other methods, which take parameters annotated with [@TestDataFile](https://github.com/JetBrains/intellij-community/tree/idea/263.7307.200/platform/testFramework/src/com/intellij/testFramework/TestDataFile.java). These methods copy the specified files from the `testdata` directory to the test project directory, open the first of the specified files in the in-memory editor, and then perform the requested operation such as highlighting or code completion.

> **Tip:**
> The IDE supports smart navigation between test code and related test data file(s); see this [blog post](https://blog.jetbrains.com/platform/2017/10/improvements-in-testing-intellij-platform-plugins/) for more details.

### Special Markup

When a file is opened in the in-memory editor, special markup in the file content can specify the caret position or selection.

Use one of the following markers:

* `<caret>` specifies the position where the caret should be placed.

* `<selection>` and `</selection>` specify the start and end of the selected text range.

* `<block>` and `</block>` specify the column selection's start and end points.

