vendredi 18 juin 2021

Dynamically generate Java test classes from given YAML

I need to generate Java test files from given YAML files. These YAML files will contain given and expected values.

Let's assume I have a generate-test.yaml file that looks like this:

tests:
  - name: StudentPassingGrade
    inputs:
      - input: 
          grade:
            type: exam #assume that this is enum and can take values like exam, quiz, etc.
            point: 60
            outOf: 100
            date: 2021-01-01
            notes:
              - note: "First exam of fall semester"
      - input: 
          grade:
            type: quiz #assume that this is enum and can take values like exam, quiz, etc.
            point: 4
            outOf: 10
            date: 2021-02-05
            notes:
              - note: "Pop quiz"
      - input: 
          grade:
            type: exam #assume that this is enum and can take values like exam, quiz, etc.
            point: 80
            outOf: 100
            date: 2021-03-10
            notes:
              - note: "Second exam of fall semester"
    results:
      - result:
          name: StudentWeighedGrade
          numOfGrades: 3
          numOfExamGrades: 2
          numOfQuizGrades: 1
          averageOfExamGradesWithPercentile: 70%
          averageOfQuizGradesWithPercentile: 40%
          weighedGradeWithPercentile: 64%
          passing: true 

From such a YAML file, I need to generate a Java test class, set it up with the values given in the input entry in the YAML file, define the assertion cases given in the results entry in the YAML file, and actually run the tests.

Such Java test file could look like this:

public class GeneratedTestClass1 extends SomeAbstractTestClass {
    
    @Test
    public void testStudentPassingGrade() { # name is taken from the given YAML file
        
        {
            Student student = new Student();

            Grade grade1 = new Grade();
            grade1.setType(Type.exam);
            grade1.setPoint(60);
            grade1.setPointsOutOf(100);
            grade1.setDate(LocalDate.of(2021,1,1));
            grade1.setNotes(new Note("First exam of fall semester"));

            Grade grade2 = new Grade();
            grade2.setType(Type.quiz);
            grade2.setPoint(4);
            grade2.setPointsOutOf(10);
            grade2.setDate(LocalDate.of(2021,2,5));
            grade2.setNotes(new Note("Pop quiz"));

            Grade grade3 = new Grade();
            grade3.setType(Type.exam);
            grade3.setPoint(80);
            grade3.setPointsOutOf(100);
            grade3.setDate(LocalDate.of(2021,3,10));
            grade3.setNotes(new Note("Second exam of fall semester"));

            student.setGrade(grade1);
            student.setGrade(grade2);
            student.setGrade(grade3);
        }
        {
            Assertions.assertEquals(student.getNumberOfGrades, 3) # value 3 from the given YAML
            Assertions.assertEquals(student.getNumberOfExamGrades, 2) # same as above
            Assertions.assertEquals(student.getNumberOfQuizGrades, 1) # same as above

            float weighedGrade = student.calculateWeighedGrade();
            Assertions.assertEquals(weighedGrade, 64);

            Assertions.assertEquals(student.isPassing(), true); # again, taken from YAML
        }
    }
}

Any tips on how to achieve this task? I googled around, found that maybe I can use Java Reflection API or Cucumber (though I am not sure about this one). But I am not sure how to start. Any help is appreciated!





Aucun commentaire:

Enregistrer un commentaire