6

Is it a good pratice to use random value to create a test object with JUnit ? Like this :

public class MonomialTest {

    private static final Random RANDOM = new Random();

    private Monomial monomial;
    private float coefficient;
    private int exponent;

    @Before
    public void setUp() throws Exception {
        coefficient = RANDOM.nextFloat();
        exponent = RANDOM.nextInt();
        monomial = new Monomial(coefficient, exponent);
    }

    @Test
    ...
    ...
    ...

}

Or should I use fixed values ?

dblouis
  • 568
  • 1
  • 5
  • 18
  • Why the -1 ? tell me please – dblouis Jan 23 '16 at 13:02
  • A lot of opinion based questions will get voted down or marked as off topic for this forum. The idea is that they attract a lot of spam and unrelated comments to this forum. You might try the code review forum within the stack exchange website family. You might also ask something more specific so you will attract more quality answers. An example would be will this method be more secure and/or faster than the other. I believe the term best practice means a lot of different things to people depending on how they learned to code using that language and where they work. – Larry Lane Jan 23 '16 at 13:10
  • @LarryLane too hypothetical for CodeReview – Caridorc Jan 23 '16 at 13:23
  • 1
    Thanks for the feedback. Do you have a suggestion on where they might ask a question similar to this? It looks like someone was able to answer question for them here. – Larry Lane Jan 23 '16 at 13:25
  • Possible duplicate of http://stackoverflow.com/questions/32458/random-data-in-unit-tests – Raedwald Jan 23 '16 at 15:46

4 Answers4

2

Unit tests should be deterministic to simplify finding the fault when a test fails. Therefore they should not include random values.

Checking that certain properties hold for arbitrary values (which your tests effectively do) does make sense though: For that you can use property-based testing frameworks such as junit-quickcheck which will do the random data generation for you.

avandeursen
  • 8,458
  • 3
  • 41
  • 51
1

it's okay to use random, but to make sure that it's working well: print the random value so u can see it, and make sure that the result is what expected to be.

or just put a known value instead of a random one.

Duha
  • 92
  • 1
  • 9
-1

If i don't get wrong new Random() (without a random seed) always produce the same sequence of pseudo random numbers so I think it's ok to use it because actually you will always test on the same sequence of numbers!

Tommaso Pasini
  • 1,521
  • 2
  • 12
  • 16
-2

Usually it is not a good idea to use random data in unit-test cases. Normally you want your tests to produce deterministic results. There are, however, situations when people test functionality with random data - if the system under test is quite complex and there is doubt whether a systematically designed test suite (which still should be there) will ever obtain sufficient coverage. Such random tests are then, however, mostly long-running tests and are not executed in the normal course of regression testing.

Dirk Herrmann
  • 5,550
  • 1
  • 21
  • 47