Skip to content

IntelliJ Testing Methods

Amir Sagiv edited this page Jul 1, 2017 · 1 revision

Our Testing Methods

  • Our current coverage is about 62%, we should get to 70-80% coverage at the end of the semester. Of course higher coverage will be even better.
  • There will be unit tests and plugin tests.
  • Unit tests will be written as parameterized tests, as described below.
  • Plugin tests will check the refactoring action of the tippers. Use the information below and examples from Intellij IDEA CE github repo to write these tests.

Parameterized Tests

Requires JUnit 4. Parameterized tests allow you to write a test once and run it multiple times with different values instead of writing a test for each set of values.

Steps

To create parameterized tests several steps should be completed:

  1. Add @RunWith(Parameterized.class) annotation to the test class. (the relevant imports should be added too). For example:

    image

  2. Create instance variables as parameters to hold the different values. For example:

    image

  3. To state the wanted values for the parameters, add a method that returns the wanted values as a Collection (using Arrays.asList). This method should be annotated with @Parameters and it should be public static. For example:

    image

    It is also possible to name the tests by adding a name parameter to @Parameters, like that:

    image

    Now when running the tests you will see this:

    image

  4. To let the runner know which variable gets which value you have two options: (you should do only one of those options)

    • Create a public constructor that gets the values and sets the variables, like that:

      image

    • Add annotations on the variables, like that:

      image

      Notice that in this option the variables have to be public. The parameter to the annotation is the index for setting the values, this annotation without a parameter sets the index to default as 0, so make sure to not use it twice!

  5. And finally you can write your test! Make sure to use the variables you created before for the parameterization. For example:

    image

Plugin Tests

These are tests that run in a headless environment which uses real production implementations for the majority of components, except for a number of UI components. They will test features as a whole in addition to our unit tests.

Categories

There are two categories for these tests:

  • Light tests - tests that reuse the project from the previous test run.
  • Heavy tests - tests that create a new project for each test.

You should write light tests rather than heavy ones whenever possible.

Light Tests

To write light tests you need to extend one of the next classes:

  • LightPlatformCodeInsightFixtureTestCase - for tests that don’t have any Java dependencies.
  • LightCodeInsightFixtureTestCase - for tests that require the Java PSI or any related functionality.

Before the execution of each test case LightCodeInsightFixtureTestCase.getProjectDescriptor() will be called and it will return a LightProjectDescriptor (a class to specify the requirements for the project that you need to have in your test, such as the configured SDK, libraries etc.), if it is the same project descriptor as it was in the previous test case the project will be reused, otherwise it will be recreated.

Heavy Tests

These tests will be used if you need to set up a multi-module project for your tests. For such projects the setup code should look something like that:

final TestFixtureBuilder<IdeaProjectTestFixture> projectBuilder = IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder(getName());

// Repeat the following line for each module
final JavaModuleFixtureBuilder moduleFixtureBuilder = projectBuilder.addModule(JavaModuleFixtureBuilder.class);

myFixture = JavaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(projectBuilder.getFixture());

Test Data

When using LightPlatformCodeInsightFixtureTestCase the files for the test project will be stored in an in-memory file system. These files will be in a testdata directory. Usually they are not valid source code and therefore should not be compiled (which means testdata can't be under the src directory). You should override LightPlatformCodeInsightFixtureTestCase.getTestDataPath() in order to specify the location of testdata.

image

To copy files or directories from your testdata directory to the test project directory, you can use the copyFileToProject() and copyDirectoryToProject(). To copy a file from the testdata directory to the test project directory and immediately open it in the editor, you can use the CodeInsightTestFixture.configureByFile() or configureByFiles() methods.

The Editor

The in-memory editor instance is returned by CodeInsightTestFixture.getEditor(). This is where the files will be opened when using CodeInsightTestFixture.configureByFile() and configureByFiles() methods.

Important Methods

image

The most useful one for our project is probably: image

Another important method is checkResultByFile(). It is used to compare the results of executing the action with the expected results. If multiple files were refactored you can use PlatformTestUtil.assertDirectoriesEqual().

More Info

For more information about parameterized tests:

For more information about plugin tests:

Clone this wiki locally