Recall & Review
beginner
What is a flaky test?
A flaky test is a test that sometimes passes and sometimes fails without any changes in the code. It behaves unpredictably, making it hard to trust test results.
Click to reveal answer
beginner
Why are flaky tests problematic in software testing?
Flaky tests cause confusion because they give inconsistent results. They can hide real bugs or waste time investigating false failures.
Click to reveal answer
beginner
Name one common cause of flaky tests.
One common cause is timing issues, like tests depending on slow network responses or asynchronous operations that are not properly handled.
Click to reveal answer
intermediate
How can you detect flaky tests using JUnit?
You can run the same test multiple times in JUnit and check if it sometimes fails and sometimes passes. Tools or plugins can help automate this detection.
Click to reveal answer
intermediate
What is a simple JUnit code example to run a test multiple times to detect flakiness?
Use @RepeatedTest annotation to run a test multiple times. If results vary, the test might be flaky.
Example:
import org.junit.jupiter.api.RepeatedTest;
import static org.junit.jupiter.api.Assertions.assertTrue;
class FlakyTestExample {
@RepeatedTest(5)
void testSometimesFails() {
boolean condition = Math.random() > 0.2; // 80% chance to pass
assertTrue(condition, "Test failed randomly");
}
}Click to reveal answer
What does a flaky test do?
✗ Incorrect
A flaky test sometimes passes and sometimes fails without code changes.
Which of these is a common cause of flaky tests?
✗ Incorrect
Timing or race conditions often cause flaky tests.
How can you detect flaky tests in JUnit?
✗ Incorrect
Running tests multiple times helps find flaky tests by spotting inconsistent outcomes.
What JUnit annotation helps run a test multiple times?
✗ Incorrect
@RepeatedTest runs a test multiple times to check for flakiness.
Why should flaky tests be fixed?
✗ Incorrect
Flaky tests cause unreliable results, making it hard to trust testing.
Explain what a flaky test is and why it is important to detect it.
Think about tests that sometimes fail without code changes.
You got /4 concepts.
Describe a simple method to detect flaky tests using JUnit.
How can repeating a test help find flakiness?
You got /3 concepts.