Created
May 22, 2019 00:05
-
-
Save DavidIAm/a894ef018464887aab53718c300bd170 to your computer and use it in GitHub Desktop.
Run your tests in a randomly selected order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.skylosian; | |
import static com.google.common.math.LongMath.factorial; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Random; | |
import org.junit.runners.model.FrameworkMethod; | |
import org.junit.runners.model.InitializationError; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
public class PermuteRunner extends SpringJUnit4ClassRunner { | |
private static Random rand; | |
/** | |
* Creates a BlockJUnit4ClassRunner to run {@code klass} | |
* | |
* @throws InitializationError if the test class is malformed. | |
*/ | |
public PermuteRunner(Class<?> klass) throws InitializationError { | |
super(klass); | |
} | |
private static <T> List<T> permutationHelper(long no, LinkedList<T> in, List<T> out) { | |
if (in.isEmpty()) { | |
return out; | |
} | |
long subFactorial = factorial(in.size() - 1); | |
out.add(in.remove((int) (no / subFactorial))); | |
return permutationHelper(no % subFactorial, in, out); | |
} | |
protected List<FrameworkMethod> computeTestMethods() { | |
List<FrameworkMethod> methods = super.computeTestMethods(); | |
rand = new Random(); | |
rand.setSeed(System.currentTimeMillis()); | |
return permutationHelper( | |
rand.nextInt( | |
Math.toIntExact((factorial(methods.size()) - factorial(methods.size() - 1)))), | |
new LinkedList(methods), | |
new ArrayList<FrameworkMethod>()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment