In software testing, what best describes a flaky test?
Think about tests that behave unpredictably.
A flaky test is one that produces different results (pass or fail) without any changes in the code, often due to timing, environment, or dependencies.
What will be the output of this JUnit test if it is flaky due to timing?
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class FlakyTest { @Test void testRandomPass() throws InterruptedException { Thread.sleep((long)(Math.random() * 100)); assertTrue(System.currentTimeMillis() % 2 == 0); } }
Look at the assertion and the use of random timing.
The test assertion depends on the current time's evenness, which changes every millisecond, causing the test to pass or fail unpredictably.
Which assertion best helps detect flaky tests by running the same test multiple times?
Consider how to check if a test passes at least once in multiple runs.
Running a test multiple times and asserting it passes at least once helps detect flaky behavior.
Given this JUnit test code, what is the most likely cause of flakiness?
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class SharedStateTest { private static int counter = 0; @Test void testIncrement() { counter++; assertTrue(counter <= 1); } }
Think about static variables and test isolation.
The static counter variable keeps its value between test runs, causing tests to fail when run multiple times or in parallel.
Which JUnit 5 feature helps detect flaky tests by repeating a test multiple times and reporting failures?
Look for annotations that run tests multiple times.
The @RepeatedTest annotation runs the same test multiple times, helping identify flaky tests by observing inconsistent results.