| 1 | /* |
| 2 | * Copyright 2015-2024 the original author or authors. |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | * accompanies this distribution and is available at |
| 7 | * |
| 8 | * https://www.eclipse.org/legal/epl-v20.html |
| 9 | */ |
| 10 | |
| 11 | package org.junit.jupiter.api; |
| 12 | |
| 13 | import static org.apiguardian.api.API.Status.EXPERIMENTAL; |
| 14 | import static org.apiguardian.api.API.Status.STABLE; |
| 15 | |
| 16 | import java.lang.annotation.Documented; |
| 17 | import java.lang.annotation.ElementType; |
| 18 | import java.lang.annotation.Retention; |
| 19 | import java.lang.annotation.RetentionPolicy; |
| 20 | import java.lang.annotation.Target; |
| 21 | |
| 22 | import org.apiguardian.api.API; |
| 23 | |
| 24 | /** |
| 25 | * {@code @RepeatedTest} is used to signal that the annotated method is a |
| 26 | * <em>test template</em> method that should be repeated a {@linkplain #value |
| 27 | * specified number of times} with a configurable {@linkplain #name display |
| 28 | * name} and an optional {@linkplain #failureThreshold() failure threshold}. |
| 29 | * |
| 30 | * <p>Each invocation of the repeated test behaves like the execution of a |
| 31 | * regular {@link Test @Test} method with full support for the same lifecycle |
| 32 | * callbacks and extensions. In addition, the current repetition and total |
| 33 | * number of repetitions can be accessed by having the {@link RepetitionInfo} |
| 34 | * injected. |
| 35 | * |
| 36 | * <p>{@code @RepeatedTest} methods must not be {@code private} or {@code static} |
| 37 | * and must return {@code void}. |
| 38 | * |
| 39 | * <p>{@code @RepeatedTest} methods may optionally declare parameters to be |
| 40 | * resolved by {@link org.junit.jupiter.api.extension.ParameterResolver |
| 41 | * ParameterResolvers}. |
| 42 | * |
| 43 | * <p>{@code @RepeatedTest} may also be used as a meta-annotation in order to |
| 44 | * create a custom <em>composed annotation</em> that inherits the semantics |
| 45 | * of {@code @RepeatedTest}. |
| 46 | * |
| 47 | * <h2>Test Execution Order</h2> |
| 48 | * |
| 49 | * <p>By default, test methods will be ordered using an algorithm that is |
| 50 | * deterministic but intentionally nonobvious. This ensures that subsequent runs |
| 51 | * of a test suite execute test methods in the same order, thereby allowing for |
| 52 | * repeatable builds. In this context, a <em>test method</em> is any instance |
| 53 | * method that is directly annotated or meta-annotated with {@code @Test}, |
| 54 | * {@code @RepeatedTest}, {@code @ParameterizedTest}, {@code @TestFactory}, or |
| 55 | * {@code @TestTemplate}. |
| 56 | * |
| 57 | * <p>Although true <em>unit tests</em> typically should not rely on the order |
| 58 | * in which they are executed, there are times when it is necessary to enforce |
| 59 | * a specific test method execution order — for example, when writing |
| 60 | * <em>integration tests</em> or <em>functional tests</em> where the sequence of |
| 61 | * the tests is important, especially in conjunction with |
| 62 | * {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}. |
| 63 | * |
| 64 | * <p>To control the order in which test methods are executed, annotate your |
| 65 | * test class or test interface with {@link TestMethodOrder @TestMethodOrder} |
| 66 | * and specify the desired {@link MethodOrderer} implementation. |
| 67 | * |
| 68 | * @since 5.0 |
| 69 | * @see DisplayName |
| 70 | * @see RepetitionInfo |
| 71 | * @see TestTemplate |
| 72 | * @see TestInfo |
| 73 | * @see Test |
| 74 | */ |
| 75 | @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) |
| 76 | @Retention(RetentionPolicy.RUNTIME) |
| 77 | @Documented |
| 78 | @API(status = STABLE, since = "5.0") |
| 79 | @TestTemplate |
| 80 | public @interface RepeatedTest { |
| 81 | |
| 82 | /** |
| 83 | * Placeholder for the {@linkplain TestInfo#getDisplayName display name} of |
| 84 | * a {@code @RepeatedTest} method: <code>{displayName}</code> |
| 85 | */ |
| 86 | String DISPLAY_NAME_PLACEHOLDER = "{displayName}"; |
| 87 | |
| 88 | /** |
| 89 | * Placeholder for the current repetition count of a {@code @RepeatedTest} |
| 90 | * method: <code>{currentRepetition}</code> |
| 91 | */ |
| 92 | String CURRENT_REPETITION_PLACEHOLDER = "{currentRepetition}"; |
| 93 | |
| 94 | /** |
| 95 | * Placeholder for the total number of repetitions of a {@code @RepeatedTest} |
| 96 | * method: <code>{totalRepetitions}</code> |
| 97 | */ |
| 98 | String TOTAL_REPETITIONS_PLACEHOLDER = "{totalRepetitions}"; |
| 99 | |
| 100 | /** |
| 101 | * <em>Short</em> display name pattern for a repeated test: {@value} |
| 102 | * |
| 103 | * @see #CURRENT_REPETITION_PLACEHOLDER |
| 104 | * @see #TOTAL_REPETITIONS_PLACEHOLDER |
| 105 | * @see #LONG_DISPLAY_NAME |
| 106 | */ |
| 107 | String SHORT_DISPLAY_NAME = "repetition " + CURRENT_REPETITION_PLACEHOLDER + " of " + TOTAL_REPETITIONS_PLACEHOLDER; |
| 108 | |
| 109 | /** |
| 110 | * <em>Long</em> display name pattern for a repeated test: {@value} |
| 111 | * |
| 112 | * @see #DISPLAY_NAME_PLACEHOLDER |
| 113 | * @see #SHORT_DISPLAY_NAME |
| 114 | */ |
| 115 | String LONG_DISPLAY_NAME = DISPLAY_NAME_PLACEHOLDER + " :: " + SHORT_DISPLAY_NAME; |
| 116 | |
| 117 | /** |
| 118 | * The number of repetitions. |
| 119 | * |
| 120 | * @return the number of repetitions; must be greater than zero |
| 121 | */ |
| 122 | int value(); |
| 123 | |
| 124 | /** |
| 125 | * The display name for each repetition of the repeated test. |
| 126 | * |
| 127 | * <h4>Supported placeholders</h4> |
| 128 | * <ul> |
| 129 | * <li>{@link #DISPLAY_NAME_PLACEHOLDER}</li> |
| 130 | * <li>{@link #CURRENT_REPETITION_PLACEHOLDER}</li> |
| 131 | * <li>{@link #TOTAL_REPETITIONS_PLACEHOLDER}</li> |
| 132 | * </ul> |
| 133 | * |
| 134 | * <p>Defaults to {@link #SHORT_DISPLAY_NAME}, resulting in |
| 135 | * names such as {@code "repetition 1 of 2"}, {@code "repetition 2 of 2"}, |
| 136 | * etc. |
| 137 | * |
| 138 | * <p>Can be set to <code>{@link #LONG_DISPLAY_NAME}</code>, resulting in |
| 139 | * names such as {@code "myRepeatedTest() :: repetition 1 of 2"}, |
| 140 | * {@code "myRepeatedTest() :: repetition 2 of 2"}, etc. |
| 141 | * |
| 142 | * <p>Alternatively, you can provide a custom display name, optionally |
| 143 | * using the aforementioned placeholders. |
| 144 | * |
| 145 | * @return a custom display name; never blank or consisting solely of |
| 146 | * whitespace |
| 147 | * @see #SHORT_DISPLAY_NAME |
| 148 | * @see #LONG_DISPLAY_NAME |
| 149 | * @see #DISPLAY_NAME_PLACEHOLDER |
| 150 | * @see #CURRENT_REPETITION_PLACEHOLDER |
| 151 | * @see #TOTAL_REPETITIONS_PLACEHOLDER |
| 152 | * @see TestInfo#getDisplayName() |
| 153 | */ |
| 154 | String name() default SHORT_DISPLAY_NAME; |
| 155 | |
| 156 | /** |
| 157 | * Configures the number of failures after which remaining repetitions will |
| 158 | * be automatically skipped. |
| 159 | * |
| 160 | * <p>Set this to a positive number less than the total {@linkplain #value() |
| 161 | * number of repetitions} in order to skip the invocations of remaining |
| 162 | * repetitions after the specified number of failures has been encountered. |
| 163 | * |
| 164 | * <p>For example, if you are using {@code @RepeatedTest} to repeatedly invoke |
| 165 | * a test that you suspect to be <em>flaky</em>, a single failure is sufficient |
| 166 | * to demonstrate that the test is flaky, and there is no need to invoke the |
| 167 | * remaining repetitions. To support that specific use case, set |
| 168 | * {@code failureThreshold = 1}. You can alternatively set the threshold to |
| 169 | * a number greater than {@code 1} depending on your use case. |
| 170 | * |
| 171 | * <p>Defaults to {@link Integer#MAX_VALUE}, signaling that no failure |
| 172 | * threshold will be applied, which effectively means that the specified |
| 173 | * {@linkplain #value() number of repetitions} will be invoked regardless of |
| 174 | * whether any repetitions fail. |
| 175 | * |
| 176 | * <p><strong>WARNING</strong>: if the repetitions of a {@code @RepeatedTest} |
| 177 | * method are executed in parallel, no guarantees can be made regarding the |
| 178 | * failure threshold. It is therefore recommended that a {@code @RepeatedTest} |
| 179 | * method be annotated with |
| 180 | * {@link org.junit.jupiter.api.parallel.Execution @Execution(SAME_THREAD)} |
| 181 | * when parallel execution is configured. |
| 182 | * |
| 183 | * @since 5.10 |
| 184 | * @return the failure threshold; must be greater than zero and less than the |
| 185 | * total number of repetitions |
| 186 | */ |
| 187 | @API(status = EXPERIMENTAL, since = "5.10") |
| 188 | int failureThreshold() default Integer.MAX_VALUE; |
| 189 | |
| 190 | } |