Gradle – How to run single Test

0
2585

In this tutorial you will see how to run specific single test or multiple unit tests in Gradle using Simple name pattern and Fully-qualified name patterns from the command line with an example project.

1. Simple Gradle Project

To run specific test from gradle command we have created a simple project, which is having couple of packages, each package having couple of tets and each test having couple of test methods. Let’s have a look into following project structure and the files it having, which is a simple Java Gradle project having Junit 4 and Junit 5 tests.

1.1. Project Structure :

gradle run specific test

Junit4_Surcharge_Test :

public class Junit4_Surcharge_Test {

	// Junit 4 test case
	@org.junit.Test
	public void surcharge_merchant_test() {
		// ..currentThread().getName() - just to print thread name which is running current test
		System.out.println("surcharge_merchant_test(): " + Thread.currentThread().getName());
	}

	// Junit 4 test case
	@org.junit.Test
	public void surcharge_byCountry_test() {
		System.out.println("surcharge_byCountry_test(): " + Thread.currentThread().getName());
	}

	// Junit 4 test case
	@org.junit.Test
	public void surcharge_formula_test() {
		System.out.println("surcharge_formula_test(): " + Thread.currentThread().getName());
	}
}

Junit5_Payment_Test :

@Tag("regression")
public class Junit5_Payment_Test {

	@Tag("acceptance")
	@Tag("baseline")
	@org.junit.jupiter.api.Test // Junit 5 test
	void payment_success_test() {
		System.out.println("payment_success_test(): " + Thread.currentThread().getName());
	}

	@Tag("acceptance")
	@org.junit.jupiter.api.Test // Junit 5 test
	void payment_decline_test() {
		System.out.println("payment_decline_test(): " + Thread.currentThread().getName());
	}

	@Tag("security")
	@org.junit.jupiter.api.Test // Junit 5 test
	void invalid_country_test() {
		System.out.println("invalid_country_test(): " + Thread.currentThread().getName());
	}
}

Junit5_Registration_Test :

public class Junit5_Registration_Test {

	@Test
	void registration_Success_Test() {
		System.out.println("registration_Success_Test(): " + Thread.currentThread().getName());
	}

	@Test
	void registration_Fail_On_DocCheck_Test() {
		System.out.println("registration_Fail_On_DocCheck_Test(): " + Thread.currentThread().getName());
	}
}

Junit5_SDD_Test :

public class Junit5_SDD_Test {

	@Test
	void simpl_dueDeligence_pass_Test() {
		System.out.println("simpl_dueDeligence_pass_Test(): " + Thread.currentThread().getName());
	}

	@Test
	void simpl_dueDeligence_fail_Test() {
		System.out.println("simpl_dueDeligence_fail_Test(): " + Thread.currentThread().getName());
	}
}

2. Run specific tests using Simple Name Pattern in Gralde

Since 4.7, Gradle has treated a pattern starting with an uppercase letter as a simple class name, or a class name + method name. Use clean task to clean preciously executed results.

2.1. Running single Test Class

Following is the command in gradle to run single specific test class. Option -i is optional, which to used to see print messages of test methods in console.

# clean - Removes whole build that previously generated before task starts
$ gradle clean test --tests Junit4_Surcharge_Test -i
                       OR
# cleanTest - Removes all files created during test task in previous run
$ gradle cleanTest test --tests Junit4_Surcharge_Test -i
                       OR
# --rerun-tasks - Force to rerun task and files get updated, instead removing all files from previous run
$ gradle test --rerun-tasks --tests Junit4_Surcharge_Test -i

Output in Console Results :

Following are the partial output results in console.

Gradle Test Executor 3 started executing tests.

> Task :test

com.javabydeveloper.test.payment.Junit4_Surcharge_Test > surcharge_formula_test STANDARD_OUT
    surcharge_formula_test(): Test worker

com.javabydeveloper.test.payment.Junit4_Surcharge_Test > surcharge_byCountry_test STANDARD_OUT
    surcharge_byCountry_test(): Test worker

com.javabydeveloper.test.payment.Junit4_Surcharge_Test > surcharge_merchant_test STANDARD_OUT
    surcharge_merchant_test(): Test worker

Gradle Test Executor 3 finished executing tests.

2.2. Running Specific Test methods

Following are the commands in gradle to run specific test methods of test class. You can use patter match expression as well.

# Executes a single specified test in Junit5_Payment_Test class
$ gradle clean test --tests Junit5_Payment_Test.payment_success_test -i

# Executes specified tests using pattern match in Junit5_Payment_Test class
$ gradle clean test --tests Junit5_Payment_Test.*payment* -i

Output in Console Results :

Following is partial output for command gradle clean test --tests Junit5_Payment_Test.payment -i

Gradle Test Executor 9 started executing tests.

> Task :test

Junit5_Payment_Test > payment_decline_test() STANDARD_OUT
    payment_decline_test(): Test worker

Junit5_Payment_Test > payment_success_test() STANDARD_OUT
    payment_success_test(): Test worker

Gradle Test Executor 9 finished executing tests.

3. Run specific tests using Fully-qualified Name Pattern in Gralde

Sometimes you might have same test class names in different packages, in such cases you have to run the tests using fully-qualified name. Gralde supports to run specific tests using fully-qualified name. Following are some examples.

3.1. Run Specific Test Class or test methods

# specific class
$ gradle clean test --tests com.javabydeveloper.test.payment.Junit5_Payment_Test -i

# specific class and method
$ gradle clean test --tests com.javabydeveloper.test.payment.Junit5_Payment_Test.payment_success_test -i

# specific class and methods using pattern match
$ gradle clean test --tests com.javabydeveloper.test.payment.Junit5_Payment_Test.payment* -i

3.2. Run Tests in Specific Package

# all classes at specific package (recursively)
$ gradle clean test --tests 'com.javabydeveloper.test.registration*' -i

# specific method at specific package (recursively)
$ gradle clean test --tests 'com.javabydeveloper.test.registration*.simpl_dueDeligence_pass_Test' -i

# specific methods at specific package (recursively)
$ gradle clean test --tests 'com.javabydeveloper.test.registration*.simpl*' -i

Output :

Following is partial output for command gradle clean test --tests 'com.javabydeveloper.test.registration*' -i

Gradle Test Executor 12 started executing tests.

> Task :test

Junit5_Registration_Test > registration_Fail_On_DocCheck_Test() STANDARD_OUT
    registration_Fail_On_DocCheck_Test(): Test worker

Junit5_Registration_Test > registration_Success_Test() STANDARD_OUT
    registration_Success_Test(): Test worker

Junit5_SDD_Test > simpl_dueDeligence_fail_Test() STANDARD_OUT
    simpl_dueDeligence_fail_Test(): Test worker

Junit5_SDD_Test > simpl_dueDeligence_pass_Test() STANDARD_OUT
    simpl_dueDeligence_pass_Test(): Test worker

Gradle Test Executor 12 finished executing tests.

3.3. Run Specific Tests irrespective of Package

# To run specific tests whose class name starts with "Junit5" in any package
$ gradle clean test --tests 'Junit5*' -i

# To run specific tests whose class name starts with "Junit5" and test method contains "registration" in any package
$ gradle clean test --tests 'Junit5*registration*' -i

# To run specific tests whose class name contains "Payment" and test method starts with "invalid" in any package
$ gradle clean test --tests '*Payment*.invalid*' -i

Output :

Following is partial output for command gradle clean test --tests 'Payment.invalid*' -i

Gradle Test Executor 27 started executing tests.

> Task :test

Junit5_Payment_Test > invalid_country_test() STANDARD_OUT
    invalid_country_test(): Test worker

Gradle Test Executor 27 finished executing tests.

4. Conclusion

In this tutorial we have covered how to run specific test in gradle using Simple name pattern and Fully-qualified name patterns from the command line with an example project. Also checkout Run Tag Specific Junit 5 Tests to run tests in Junit 5.

You can checkout source code from our github repository.

You also might interested in our other Gradle Tutorials:

  1. How to create Gradle project in eclipse
  2. Gradle – set System Properties
  3. Gradle – run specific tests
  4. Gradle – Skip Tests
  5. Junit 5 Gradle Example
  6. which method is used to attach a predicate to a skipping task

5. References

  1. Gradle Document
  2. Create Gradle Project in Eclipse
  3. JUnit 5 Test Suites
  4. Gradle – Skip Unit Tests

LEAVE A REPLY

Please enter your comment!
Please enter your name here