Forum Discussion

kumars12's avatar
kumars12
Occasional Contributor
8 years ago

Mongo Context varibale - Is it possible to use it throughout the test suite

Greetings,

While working with MondoDB using gmongo (groovy wrapper), we open connection using mongoDB context, something like this:

def mongoClient = new MongoClient( new ServerAddress(dbServer), Arrays.asList(credentials) )
context.gmongo = new GMongo( mongoClient )
context.mongoDB = context.gmongo.getDB(dbDatabase)

 

In my current framework, I open connection and close it everytime I need to do something with mongodb.

I am trying to somehow define the context once globally and use the variable to access mongodb throughout the test suite.

 

Is there any way to do it ?

 

Any help would be much appreciated.

  • You can set context variables in Project or TestSuite setup scripts which will then be accessible by anything under it. While I have not used this for database connections, I have stored other variables this way.

     

    A word of warning! If for example a test case relies on something defined in a project setup script, you will always have to run from the project level. I ran into problems of not just being able to run a single test case. Now I always try to make my test cases self contained.

  • Radford's avatar
    Radford
    Super Contributor

    You can set context variables in Project or TestSuite setup scripts which will then be accessible by anything under it. While I have not used this for database connections, I have stored other variables this way.

     

    A word of warning! If for example a test case relies on something defined in a project setup script, you will always have to run from the project level. I ran into problems of not just being able to run a single test case. Now I always try to make my test cases self contained.

    • kumars12's avatar
      kumars12
      Occasional Contributor

      Hello Radform, thanks a lot for the reply. could you please provide me some syntax of using context in setup script.

      How to set it and how to get it.

      Thanks again

      • Radford's avatar
        Radford
        Super Contributor

        Take a look at this page:

         

        http://readyapi.smartbear.com/soapui/scripts/scripts

         

        It has an example of storing a JDBC connection in the context which was created in a setup script. While this eaxmple is using a test case setup script, you should be able to do the same at the test suite or project level (Though as I mentioned before do consider how you want to run the tests).