Ever wonder how you can run one single test with gradlew? Ever get tried of waiting all the tests to be finished? You have came to the right place. Running tests can be a time consuming tasks. Ability to run one single test or all the tests in a test class is very powerful. And, it will save TONS of time. Here is how you can do it.

Let’s assume that this is your testClass,

  package io.github.poanchen.projectName.api
  class BlogControllerTests {
    void testCreate_BlogWithoutTitle() {
      // some test code
    }
    void testUpdate_BlogWithInvalidAuthorEmail() {
      // some test code
    }
  }

source code hosted on GitHub

To run single test (testCreate_BlogWithoutTitle),

  ./gradlew :projectName-api:unitTest --tests io.github.poanchen.projectName.api.BlogControllerTests.testCreate_BlogWithoutTitle

source code hosted on GitHub

Note: Depends on how you write your configuration file for gradlew tasks, “:projectName-api:unitTest” might look different for you than the one that I show you here.

To run all the tests in a test class (BlogControllerTests),

  ./gradlew :projectName-api:unitTest --tests io.github.poanchen.projectName.api.BlogControllerTests

source code hosted on GitHub

Tada. You should now be able to run a single test or all the tests in a test class with gradlew =)

Wrapping Up

Hopefully this guide has help you to learn how to run tests with gradlew. Thank you for reading!

Resources

I’ll try to keep this list current and up to date. If you know of a great resource you’d like to share or notice a broken link, please let us know.

Getting started