Easy Way of Running the Same Junit Test Over and Over

Easy way of running the same junit test over and over?

With JUnit 5 I was able to solve this using the @RepeatedTest annotation:

@RepeatedTest(10)
public void testMyCode() {
//your test code goes here
}

Note that @Test annotation shouldn't be used along with @RepeatedTest.

Running the same JUnit test case multiple time with different data

take a look to junit 4.4 theories:

import org.junit.Test;
import org.junit.experimental.theories.*;
import org.junit.runner.RunWith;

@RunWith(Theories.class)
public class PrimeTest {

@Theory
public void isPrime(int candidate) {
// called with candidate=1, candidate=2, etc etc
}

public static @DataPoints int[] candidates = {1, 2, 3, 4, 5};
}

How do I run the same JUnit test multiple times with different test data each time?

I would come at it a little differently. Instead of just creating several sets of test data and calling the same test each time break it up in to something meaningful. Instead of writing one test called test() write several separate tests for each aspect of ShuntingYard. For example:

@Test public void 
itDoesntDivideByZero()
{
ArrayList<String> divideByZeroExpression = Arrays.asList("5", "0", "/");
// Add code to call your method with this data here
// Add code to verify your results here
}

@Test public void
itCanAdd()
{
ArrayList<String> simpleAdditionExpression = Arrays.asList("1", "2", "+");
// Add code to call your method with this data here
// Add code to verify your results here
}

and so on. This will make your JUnit output much easier to read. When there's a failure you know that it failed while trying to add, or it failed while trying to evaluate an expression that would cause a divide by zero, etc. Doing it the way you have it in the original you'd only know that it failed in the test() method.

Each of the tests here does 3 things:

  1. Arranges the test data
  2. Performs some action with that data
  3. Asserts that the results of the action are as expected

This Arrange, Assert, Act idiom is very common in automated testing. You may also see it called Given, When, Then as in, "Given these conditions, when I call this method, then I should get this result".

Try to get out of the mindset of writing one test to test an entire class or method. Write a test to test one part of a method. Consider this class:

public class Adder {
public int addOneTo(int someNumber) {
return someNumber + 1;
}
}

You might end up with a test suite that looks like:

@Test public void
itAddsOne()
{
int numberToAddTo = 1;
int result = new Adder().addOneTo(numberToAddTo);
assertEquals("One plus one is two", 2, result);
}

@Test(expected="NullPointerException.class") public void
itChokesOnNulls()
{
new Adder().addOneTo((Integer)null);
}

@Test public void
itDoesntOverflow()
{
int result = new Adder().addOneTo(Integer.MAX_VALUE);
// do whatever here to make sure it worked correctly
}

And so on.

In Eclipse, how do I run a JUnit test case multiple times

There is a test decorator for this. See Junit API at
http://junit.org/apidocs/junit/extensions/RepeatedTest.html

for example

@Test  
@Repeat(10)
public void FailRandomlyNeedToKnowWhy() {
....
}

Junit4 run a test class a fixed number of times and display results (eclipse)

As suggested by @MatthewFarwell in the comments I implemented a test rule as per his answer

public static class Retry implements TestRule {

private final int retryCount;

public Retry(int retryCount) {
this.retryCount = retryCount;
}

@Override
public Statement apply(final Statement base,
final Description description) {
return new Statement() {

@Override
@SuppressWarnings("synthetic-access")
public void evaluate() throws Throwable {
Throwable caughtThrowable = null;
int failuresCount = 0;
for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
} catch (Throwable t) {
caughtThrowable = t;
System.err.println(description.getDisplayName()
+ ": run " + (i + 1) + " failed:");
t.printStackTrace();
++failuresCount;
}
}
if (caughtThrowable == null) return;
throw new AssertionError(description.getDisplayName()
+ ": failures " + failuresCount + " out of "
+ retryCount + " tries. See last throwable as the cause.", caughtThrowable);
}
};
}
}

as a nested class in my test class - and added

@Rule
public Retry retry = new Retry(69);

before my test methods in the same class.

This indeed does the trick - it does repeat the test 69 times - in the case of some exception a new AssertionError, with an individual message containing some statistics plus the original Throwable as a cause, gets thrown. So the statistics will be also visible in the jUnit view of Eclipse.

Running Junit test in loop and continuing further if one in between fail

All you need is a try-catch:

String failureMsg = "";
for (int i = 0; i < tests.length; i++) {
try {
// your test logic comes here
} catch (AssertionError assertFaild) {
// log error or signal it somehow, e.g.:
failureMsg = failureMsg + assertFaild.getMessage();
}
}
if (!failureMsg.isEmpty()) {
// you might want to collect more data
Assert.fail(failureMsg);
}

How to run selected junit tests with different parameters

Try using JUnit parameterized testing. Here is a tutorial from TutorialsPoint.com:

JUnit 4 has introduced a new feature called parameterized tests. Parameterized tests allow a developer to run the same test over and over again using different values. There are five steps that you need to follow to create a parameterized test.

  • Annotate test class with @RunWith(Parameterized.class).

  • Create a public static method annotated with @Parameters that returns a Collection of Objects (as Array) as test data set.

  • Create a public constructor that takes in what is equivalent to one "row" of test data.

  • Create an instance variable for each "column" of test data.

  • Create your test case(s) using the instance variables as the source of the test data.

The test case will be invoked once for each row of data.



Related Topics



Leave a reply



Submit