I'm a beginner at JUnit, I got a problem when I am learning it. I did a lot of searches, but still can't find the answer.
The version of JUnit Jupiter I'm using is 5.3.0-M1.
I'm doing tests on some sorting algorithms. I'd like to test if they are correct, and the performance on different arrays with different length. Here is my solution.
@BeforeAll
Generated a random unsorted array as source, and a target array which is a sorted.
@BeforeEach
Copy the unsorted source to a new array as inputs ready for the test.
Each @Test method sorts the inputs array and compares it with target, so I know if it is correct, and the IDE will tell me how much time it cost.
The problem is: I have to manually change the length every time, and test it again.
I wonder if there is a way to setup up the test, so it will run for each different length I set.
For example, pass an array like [16, 32, 64, ...] as different length I want to test with.
I know I can pass parameters to @Test method, but then I have to generate source and target multiple times in each @Test.
(Sorry for my bad English, I hope I have explained the problem clearly enough.)
Here is my test class looks like:
class SortingTest {
static int length = 512; // manually change every time.
// static int length = 1024;
// static int length = 2048;
// static int length = 4096;
static int[] source;
static int[] target;
static int[] inputs;
@BeforeAll
static void beforeAllSort() {
source = MintArray.generateIntArray(length);
target = new int[length];
inputs = new int[length];
System.arraycopy(source, 0, target, 0, source.length);
Arrays.sort(target);
System.out.println("[SORT] Array Length : [" + length + "]");
System.out.println("[SORT] Source Array : " + Arrays.toString(source));
System.out.println("[SORT] Target Array : " + Arrays.toString(target));
}
@BeforeEach
void beforeEachSort() {
System.arraycopy(source, 0, inputs, 0, source.length);
System.out.println();
}
@Test
@DisplayName("Bubble Sort")
void bubbleSort() {
System.out.println("[SORT] Bubble Sort");
inputs = new BubbleSort().bubbleSort(inputs);
assertArrayEquals(inputs, target);
}
@Test
@DisplayName("Insertion Sort")
void insertionSort() {
System.out.println("[SORT] Insertion Sort");
inputs = new InsertionSort().insertionSort(inputs);
assertArrayEquals(inputs, target);
}
@Test
.......more sort
}
Aucun commentaire:
Enregistrer un commentaire