Skip to content

Running Unit Tests

Sleet01 edited this page Apr 29, 2024 · 5 revisions

Running Unit Tests in Intellij Idea

Unit tests are meant to be run as part of the build system's code quality checks, and will be executed automatically whenever a Pull Request is submitted, as well as for nightly builds.

However, unit tests can also benefit developers while actively working on the code by providing quick notification of breaking changes or invalid code, in two ways:

  1. any syntax errors will be found prior to the tests running,
  2. any logic errors deriving from code changes are likely to cause tests to fail, even if the code in question is not directly covered.

Note that the only way to guarantee unit test coverage for new code is to write unit tests oneself!

Running All Tests [Intellij Idea]

This is the simplest method of running unit tests for a given project: run all defined unit tests via the Gradle task.

  1. Open the project you wish to test (or use the IDE you are already working in)
  2. If you have not previously run any unit tests in this project, open the Gradle sidebar (elephant icon) and navigate to 'MegaMekRoot' -> 'Tasks' -> 'Verification'.

Gradle Verification Menu

  1. Either double-click on 'run', or right-click 'run' and select one of the run options.

Gradle Verification Context Menu

  1. Observe the test run; tests will run serially (for thread safety in tests) and the results will show up in the Debug pane of the IDE (bottom for this picture):

Unit Tests Running in Idea

  1. Passing tests will display total test counts:

All Tests Passed

  1. Any failing tests will halt testing and display the failure for debugging:

Failed MekHQ Test

  1. To return the IDE to running the project rather than tests, there are three options: 7.a. Select "Run" from the Gradle dropdown next to the Run and Debug buttons; 7.b. Navigate to MegaMekRoot -> Tasks -> Application and double-click "run"; 7.c. Navigate to MegaMekRoot -> Tasks -> Application and double-click "run";

Return to Run 1

Return to Run 2

Return to Run 3

(The second and third are most useful after running many different individual tests, which pushes "Run" out of the dropdown box)

Running A Single Test Suite

The process for running a specific test suite - all of the unit tests in one Java file - is similar, but it is easiest to navigate to the file via the file explorer, open the file, and start all tests within it from the class declaration level by right-clicking on the green (or red, if after a failure) icon next to the class declaration:

Running a Single Suite

After the test has run once, it will be entered into the Gradle task dropbox at top right and can be re-run from there as well.

Running A Single Unit Test

Similar to the above, but the simplest approach is to find the unit test function within the Java file and run it from that point, by right-clicking on the green (or red if after a failure) icon next to the function definition:

Running a Single Unit Test

After the test has run once, it will be entered into the Gradle task dropbox at top right and can be re-run from there as well.

Running Unit Tests in Eclipse

(WIP)

Running All Tests

(WIP)

Running A Single Test Suite

(WIP)

Running A Single Unit Test

(WIP)

More Information

About Unit Tests

Writing Unit Tests

Clone this wiki locally