[Webtest] Best way to check a database in a webtest

Dierk König Dierk König
Thu, 31 Jul 2008 16:06:41 +0200 (CEST)


Hi Mark,

there are multiple ways to achieve this but first, yes, if you
are using a the groovy Ant task in Ant _outside_ a webtest, it will be
executed at Ant parsing time, not webtest run time.

In order to execute a Groovy Script at WebTest run time, you can:
- use the groovy Ant task nested inside a webtest 
- use the groovy webtest step nested inside a webtest
  (note the difference: the groovy _task_ comes with Groovy and has
   nothing to do with webtest. However, it can be used with 
   webtest since you can nest any Ant task inside webtest.
   The Groovy webtest _step_ is specific to Canoo WebTest and gives
   you more information about the current webtest context, you
   are working in, allows integration with the webtest reporting,
   and so on.)
- use dbunit tasks or equivalent nested inside webtest
- write a custom (Groovy or Java) task

I assume the following is the easiest for your need:

<webtest ...>
  <!-- some steps that change the DB -->
  <groovy>
    import groovy.sql.SQL
    import com.canoo.webtest.steps.StepFailedException as SFE

    def db = new SQL(...)
    def mySet = db.dataSet 'MY_TABLE'
    if (! mySet.findAll { it.myAttribute == 'value'} )
      throw new SFE( "value has not been added to db." )
        
  </groovy>
</webtest>

I hope that gives you a good enough starting point.

And of course I cannot close without mentioning that a test 
like above is somehow against the webtest approach of testing
only, what the user can do through the browser. For testing
whether a submitted value has really been persisted to the DB,
we would typically test that the value appears when the 
user logs in with a new session (i.e. in a new <webtest> 
step).

happy testing
Dierk



| -----Original Message-----
| From: webtest-admin@lists.canoo.com 
| [mailto:webtest-admin@lists.canoo.com] On Behalf Of Mark Johnson
| Sent: Donnerstag, 31. Juli 2008 1:29
| To: webtest@lists.canoo.com
| Subject: [Webtest] Best way to check a database in a webtest
| 
| I have a groovy based webtest which needs to verify that the 
| database has been properly updated.  I am assuming that my 
| groovy db SQL queries are not performing in the web test 
| script because they are getting called when the antbuilder is 
| preparing the test.  Is this a correct assumption?  If so, 
| does anyone have an alternate strategy for doing a sql query 
| within the web script?
| 
| thanks for your help
| 
| mark
| 
|