乐闻世界logo
搜索文章和话题

Gradle : How to Display Test Results in the Console in Real Time?

1个答案

1

When using the Gradle build tool, you can achieve real-time display of test results in the console through specific configurations and plugins. Below are steps and configuration methods to help you implement this:

1. Enable Gradle Test Logging

First, configure the test task in the build.gradle file to display test results in the console. Use testLogging to adjust log verbosity. For example:

gradle
test { // Set the verbosity of information during testing testLogging { // Ensure output for each test class and method is shown in the console events "passed", "skipped", "failed" } }

Here, events specifies the event types to display, including test passes (passed), skips (skipped), and failures (failed).

2. Run Gradle with --info or --debug Options

When executing the Gradle test task, add the --info or --debug command-line options to increase output verbosity. For example:

bash
g radle test --info

This outputs additional information in the console, including real-time test results.

3. Use Continuous Build Feature

Gradle's continuous build feature (-t or --continuous) automatically re-runs tasks after source code changes, which is useful for real-time test feedback. For example:

bash
g radle test --continuous

Whenever source code changes, this command re-runs tests, allowing immediate visibility of test results.

4. Integrate Additional Plugins or Tools

Consider using third-party plugins to enhance real-time test result display, such as the gradle-rich-console plugin.

5. Example: Real-Time Display of Test Results

Assume a simple Java project where you add a test class CalculatorTest. With the above testLogging configuration, you can see execution results of each test method in real-time in the console.

java
public class CalculatorTest { @Test public void testAdd() { assertEquals(5, Calculator.add(2, 3)); } @Test public void testSubtract() { assertEquals(1, Calculator.subtract(4, 3)); } }

When running gradle test --info, the console outputs results for each test method, enabling developers to quickly assess test status.

By applying these methods and configurations, you can effectively monitor and display test results in real-time within Gradle projects, improving development and debugging efficiency. This is particularly valuable in continuous integration and continuous deployment environments.

2024年8月16日 23:30 回复

你的答案