guoguibing / librec

LibRec: A Leading Java Library for Recommender Systems, see
https://www.librec.net/
Other
3.24k stars 1.03k forks source link

Test Smell: it is not a good practice to use the random number generators in test code #350

Open TestSmell opened 2 years ago

TestSmell commented 2 years ago

Hi! We notice that the random number generator (RNG) is used to produce test code in your project. For example, a random generator in the test method named ''testTimesWithSparseVectorPerformance() '' in ''DenseMatrixTestCase.java'', ''testDotWithSparseVectorPerformance()'' in ''DenseVectorTestCase.java'', etc.

But generating random numbers in test code is not a good test practice. Random number generation can create couplings between classes and timing artifacts because most random number generator classes are thread-safe and therefore introduce additional synchronization. So, a potential problem is that a test that should fail due to incorrect synchronization in the class under test might pass because of synchronization in the RNG used in the test code.

In terms of solutions,

  1. you could use another simple random algorithm (e.g., https://pastebin.com/BTCa6Jqh) that was not synchronized;
  2. you could also generate a class that stored 10000 random numbers (or however many you need) beforehand and then handed them out without synchronization.
water-95 commented 2 years ago

yes, I think you are right.