Last active
May 22, 2019 00:06
-
-
Save DavidIAm/6fbbffd033c4985d38729b473d34e02f to your computer and use it in GitHub Desktop.
Stupid test runner tricks - Permute all the orders your tests can run in
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.Arrays; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Random; | |
import java.util.stream.LongStream; | |
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()); | |
List<FrameworkMethod> collector = new ArrayList(); | |
Arrays.stream(LongStream | |
.range(1, factorial(methods.size()) - factorial(methods.size() - 1)).toArray()) | |
.forEach( | |
number -> | |
permutationHelper( | |
number, | |
new LinkedList(methods), | |
collector) | |
); | |
return collector; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment