Forum Discussion

jshu's avatar
jshu
Occasional Contributor
9 years ago
Solved

Static values in data driven test

I have a simple project to check whether a customer is 18 years old, and I have an excel spreadsheet of customer's dob to compare with a static value of the last day of this quarter

 

I can add this static date in each customer's data row to make the data driven test work, however, is there any other way to make is simpler please? For example, can I set a property such as quarter_end_date to point to a cell in the spreadsheet so it'll get the value but let the loop exclude this cell please? Or shall I write my own script to define this value please? Is there any sample please?

 

Thanks a lot

  • It is not mandatory to have the date in every data if it is a static value. And it can be taken from the script directly.

    Here is the sample script that shows how to do:

     

    Step1:

    AgeCalculator.groovy -- copy this class under ../ReadyAPI-<version>/bin/scripts

    courtesy: tim_yates from stackoverflow

     

    import groovy.time.DatumDependentDuration
    import org.apache.log4j.Logger
    import java.text.SimpleDateFormat
    
    class AgeCalculator {
    
        String dob
        String when
        String format = 'yyyy-MM-dd'
        Logger log
    
        def getDate = { String date, String format ->
            def dateFormat = new SimpleDateFormat(format)
            dateFormat.parse(date)
        }
    
        def getAge() {
            if (!dob) {
                log.error "dob is mandatory"
            }
            Date now
            if (!when) {
                log.info "Value not provided for when, considering today's date"
                now = new Date()
            } else {
                now = getDate(when, format)
            }
            Date dob = getDate(dob,format)
            dob.clearTime()
            now.clearTime()
            assert dob < now
            Calendar.instance.with { c ->
                c.time = dob
                def (years, months, days) = [ 0, 0, 0 ]
    
                while( ( c[ YEAR ] < now[ YEAR ] - 1 ) ||
                        ( c[ YEAR ] < now[ YEAR ] && c[ MONTH ] <= now[ MONTH ] ) ) {
                    c.add( YEAR, 1 )
                    years++
                }
    
                while( ( c[ YEAR ] < now[ YEAR ] ) ||
                        ( c[ MONTH ] < now[ MONTH ] && c[ DAY_OF_MONTH ] <= now[ DAY_OF_MONTH ] ) ) {
                    // Catch when we are wrapping the DEC/JAN border and would end up beyond now
                    if( c[ YEAR ] == now[ YEAR ] - 1 &&
                            now[ MONTH ] == JANUARY && c[ MONTH ] == DECEMBER &&
                            c[ DAY_OF_MONTH ] > now[ DAY_OF_MONTH ] ) {
                        break
                    }
                    c.add( MONTH, 1 )
                    months++
                }
    
                while( c[ DAY_OF_YEAR ] != now[ DAY_OF_YEAR ] ) {
                    c.add( DAY_OF_YEAR, 1 )
                    days++
                }
                log.info "Years : ${years}"
                new DatumDependentDuration( years, months, days, 0, 0, 0, 0 )
            }
        }
    }

    Step2

    Have a groovy script in your data driven test step, with below script.

    It uses 'yyyy-MM-dd' date format.

    import AgeCalculator
    
    //Please assign value for dateOfBirth from data driven by editing, for now using assumed values
    def dateOfBirth = '1995-12-26'
    def quarterEndDate = '2015-12-31'
    def calculate = new AgeCalculator(log: log,
                                     dob: dateOfBirth,
                                     when: quarterEndDate)
    assert calculate.age.years >= 18, "Not an adult"

    If date format is different, use below snippet instead of above

     

    import AgeCalculator
    
    
    //Provide dateFormat as needed
    def dateFormat = 'dd/MM/yyyy'
    
    //Please assign value for dateOfBirth from data driven by editing, for now using assumed values
    
    def dateOfBirth = '26/12/1995'
    def quarterEndDate = '31/12/2015'
    def calculate = new AgeCalculator(log: log,
                                     dob: dateOfBirth,
                                     when: quarterEndDate,
                                     format: dateFormat)
    assert calculate.age.years >= 18, "Not an adult"

    It will show "Not an adult" in the assertion failure if age is not greater or equal to 18 years by quarterEndDate.

     

    Also note that, provided it in two steps because, it will clean and modularized. You may also have both in one groovy step as well. In that case 'Import AgeCalculator' statement is not required

     

    Hope this is useful.

2 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    It is not mandatory to have the date in every data if it is a static value. And it can be taken from the script directly.

    Here is the sample script that shows how to do:

     

    Step1:

    AgeCalculator.groovy -- copy this class under ../ReadyAPI-<version>/bin/scripts

    courtesy: tim_yates from stackoverflow

     

    import groovy.time.DatumDependentDuration
    import org.apache.log4j.Logger
    import java.text.SimpleDateFormat
    
    class AgeCalculator {
    
        String dob
        String when
        String format = 'yyyy-MM-dd'
        Logger log
    
        def getDate = { String date, String format ->
            def dateFormat = new SimpleDateFormat(format)
            dateFormat.parse(date)
        }
    
        def getAge() {
            if (!dob) {
                log.error "dob is mandatory"
            }
            Date now
            if (!when) {
                log.info "Value not provided for when, considering today's date"
                now = new Date()
            } else {
                now = getDate(when, format)
            }
            Date dob = getDate(dob,format)
            dob.clearTime()
            now.clearTime()
            assert dob < now
            Calendar.instance.with { c ->
                c.time = dob
                def (years, months, days) = [ 0, 0, 0 ]
    
                while( ( c[ YEAR ] < now[ YEAR ] - 1 ) ||
                        ( c[ YEAR ] < now[ YEAR ] && c[ MONTH ] <= now[ MONTH ] ) ) {
                    c.add( YEAR, 1 )
                    years++
                }
    
                while( ( c[ YEAR ] < now[ YEAR ] ) ||
                        ( c[ MONTH ] < now[ MONTH ] && c[ DAY_OF_MONTH ] <= now[ DAY_OF_MONTH ] ) ) {
                    // Catch when we are wrapping the DEC/JAN border and would end up beyond now
                    if( c[ YEAR ] == now[ YEAR ] - 1 &&
                            now[ MONTH ] == JANUARY && c[ MONTH ] == DECEMBER &&
                            c[ DAY_OF_MONTH ] > now[ DAY_OF_MONTH ] ) {
                        break
                    }
                    c.add( MONTH, 1 )
                    months++
                }
    
                while( c[ DAY_OF_YEAR ] != now[ DAY_OF_YEAR ] ) {
                    c.add( DAY_OF_YEAR, 1 )
                    days++
                }
                log.info "Years : ${years}"
                new DatumDependentDuration( years, months, days, 0, 0, 0, 0 )
            }
        }
    }

    Step2

    Have a groovy script in your data driven test step, with below script.

    It uses 'yyyy-MM-dd' date format.

    import AgeCalculator
    
    //Please assign value for dateOfBirth from data driven by editing, for now using assumed values
    def dateOfBirth = '1995-12-26'
    def quarterEndDate = '2015-12-31'
    def calculate = new AgeCalculator(log: log,
                                     dob: dateOfBirth,
                                     when: quarterEndDate)
    assert calculate.age.years >= 18, "Not an adult"

    If date format is different, use below snippet instead of above

     

    import AgeCalculator
    
    
    //Provide dateFormat as needed
    def dateFormat = 'dd/MM/yyyy'
    
    //Please assign value for dateOfBirth from data driven by editing, for now using assumed values
    
    def dateOfBirth = '26/12/1995'
    def quarterEndDate = '31/12/2015'
    def calculate = new AgeCalculator(log: log,
                                     dob: dateOfBirth,
                                     when: quarterEndDate,
                                     format: dateFormat)
    assert calculate.age.years >= 18, "Not an adult"

    It will show "Not an adult" in the assertion failure if age is not greater or equal to 18 years by quarterEndDate.

     

    Also note that, provided it in two steps because, it will clean and modularized. You may also have both in one groovy step as well. In that case 'Import AgeCalculator' statement is not required

     

    Hope this is useful.

    • jshu's avatar
      jshu
      Occasional Contributor

      Perfect! Thanks again