What is the strategy for building test data

I'm new to the development world, and I wonder which one is the best strategy for creating consistent and consistent complex test data (I mean POJO is hard to fill) for Unit Testing?

I heard about the "Test Data Builder", but says too little about it on the Web.

+3
source share
1 answer

I often need to perform the exact same task. Fuzz testing is an appropriate approach, although we must be careful to distinguish between raw fusers and smart fusers. Intelligent fuzzer is different than a regular fuzz tool (like zzuf ) because it creates data designed for your application. Obviously, in this case you need a smart fuser.

To write an intelligent fuzzer, you will need to extract those rules that will be “consistent and consistent” and use them as logical ones. Probably the best example. There Modelis some logic in the class below.

class Model {

    // Should always be between 0 and 10
    int a;

    // Children
    List<Model> children;

    // Only true at the root
    boolean isRoot;
}

We can write a test data builder for this by simply coding these rules.

class ModelGenerator {
   private Random random;

   // A seed is a good idea; you want your tests to be reproducible
   public ModelGenerator(int seed) {
       random = new Random(seed);
   }

   public Model arbitrary () {
       return generateSingleItem(true);
   }

   private Model generateSingleItem(boolean isRoot) {
       Model model = new Model();
       model.isRoot = isRoot;
       model.a = random.nextInt(10);

       int childrenCount = random.nextInt(100);
       model.children = new ArrayList<Model>(childrenCount);
       for (int i=0;i<childrenCount;++i) {
            model.children.add(generateSingleItem(false));
       }

       return model;
   }
}

( ) , .

QuickCheck. Java, ( !), .

0

All Articles