创建一个mongo连接,并使它在Ready!API中执行一个完整的测试套件 [英] Create a mongo connection and make it alive for execution of an Entire Test Suite in Ready!API

查看:168
本文介绍了创建一个mongo连接,并使它在Ready!API中执行一个完整的测试套件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您希望为整个测试套件创建 gmongo 连接,然后在整个测试套件被执行,那么我们怎么做?



目前我在做什么,我正在创建一个特定的测试步骤,然后执行测试步骤后,使用代码 mongoClient.close()



但现在需要在测试套件开始执行之前创建连接,并在整个测试套件内的测试用例/测试步骤,然后在整个测试套件执行后关闭连接。



任何人都可以告诉我如何使用 Ready!API 执行此操作?



上午新的就绪API,所以请与我联系
这是我用来创建一个连接到mongo的代码

  def dbUser = context.expand('$ {#Project#MongoUser}')
def dbPassword = context.expand('$ {#Project#MongoPassword}')
def dbServer = context.expand(' $ {#Project#MongoServer}')
def dbDatabase = context.expand('$ {#Project#MongoDatabase}')
def credentials = MongoCredential.createCredential(dbUser,dbDatabase,dbPassword as char [] )
def mongoClient = new MongoClient(new ServerAddress(dbServer),Arrays.asList(credentials))
context.gmongo = new GMongo(mongoClient)
context.mongoDB = context.gmongo.getDB (dbDatabase)

所以我一直在使用当前的代码来创建连接。其实我想这是三个测试套件。第一个测试套件包含用于创建连接的groovy脚本,第二个测试套件包含我所有的测试用例,第三个测试套件包含mongo关闭连接脚本。



我们使用属性文件中的环境值。在这里,MongoServer具有连接所在环境的值。



我无法理解@Rao,你是怎么在测试用例中调用conn变量的。尤其是context.testCase.testSuite.db?.connection部分。什么是?表示并且能否请你在上面的上下文中告诉我,怎么可能进行这个过程

解决方案

您正在寻找 ReadyAPI / SoapUI 。请注意,您已经知道如何连接到Groovy中的 gmongo ,您需要通过内嵌注释将该逻辑添加到占位符中。



以下是用于创建数据库连接的测试套件级别设置脚本

  class DatabaseDetails {
def server
def user
def password
def log
def getConnection(){
log。 info'connection created'
//编写逻辑创建连接
}
def closeConnection(){
log.info'关闭连接'
//将逻辑写入关闭连接
}
}
//根据您的环境更改服务器,用户,密码值
def db = [server:'localhost',user:'dbuser',password :'dbuserpasswd',log:log] as DatabaseDetails
if(!db.connection){
db.connection
testSuite.metaClass.db = db
}

下方是测试套件级别 TearDown脚本来关闭数据库连接。由于这是在拆卸脚本中,所以只要测试套件执行完成,连接就会自动关闭

  testSuite.db?.closeConnection()

现在,不需要创建步骤数据库连接一次又一次。
您只需在 Groovy Script 测试步骤中使用以下脚本以获取现有数据库连接

  def conn = context.testCase.testSuite.db?.connection 

使用 conn 变量,您应该能够执行查询。



注意:由于数据库连接是在测试套件的 Setup Script 中完成的,所以如果您只是运行测试用例(即,未调用测试套件或执行),您可能无法获得连接。在这种情况下,请手动执行测试套件的设置脚本

编辑:基于OP对问题和他的代码片段的编辑,以下是更新后的测试套件的安装脚本。这根据OP的编辑来处理 getConnection() closeConnection()的实现。请添加/编辑Mongo类的导入语句,因为我没有真正意识到这些。



更新了测试套件的安装脚本

  import com.gmongo。* 
import com.mongodb。*
$ b $ class DatabaseDetails {
def context
def log
def mongoClient
def mongoDB
def getConnection(){
log.info'创建连接'
//写逻辑创建连接
if(!mongoDB){
def credentials = MongoCredential.createCredential(
context.expand('$ {#Project#MongoUser}'),
context .expand('$ {#Project#MongoDatabase}'),
context.expand('$ {#Project#MongoPassword}')char [])
mongoClient = new MongoClient(new ServerAddress .expand('$ {#Project#MongoServer}')),Arrays.asList(credentials))
mongoDB = new GMongo(mongoClient).getDB(context.e xpand('$ {#Project#MongoDatabase}'))
}
mongoDB
}
$ b $ def closeConnection(){
log.info'Closing连接'$​​ b $ b //编写逻辑关闭连接
mongoClient.close()
}
}

def db = [context:context,log: log] as DatabaseDetails
if(!db.connection){
db.connection
testSuite.metaClass.db = db
}

如前所述,要获得连接,请使用下面的代码并解释它。

  context.testCase.testSuite.db?.connection 

Groovy具有很棒的功能,称为ExpandoMetaclass。 db 被注入到 testSuite 类和 db DatabaseDetails 类,我们在测试套件的 Setup Script 中创建并实例化了这个类。
$ b db 包含 getConnection() db.getConnection(),它也可以与 db.connection 相同。这就是连接在上述语句中的可用方式。


If you want to make an gmongo connection alive for an entire test suite and then close it in a tear down operation after the entire test suite is executed then, How could we do that?

Currently what am I doing is, I am creating an connection for an particular test step and then after the test step is executed, I close the connection by using the code mongoClient.close()

But now there is a requirement where I need to create the connection before the test suite starts executing, use the same connection throughout the test suite inside the test cases/test steps and then close the connection the connection after the entire test suite gets executed.

Could anyone please tell me how could I do this using Ready!API?

I may sound retard cause I am new to Ready API so please bear with me This is the code that I use to create an Connection to mongo

 def dbUser = context.expand( '${#Project#MongoUser}' )
    def dbPassword = context.expand( '${#Project#MongoPassword}' )
    def dbServer = context.expand( '${#Project#MongoServer}' )
    def dbDatabase = context.expand( '${#Project#MongoDatabase}' )
    def credentials = MongoCredential.createCredential(dbUser,dbDatabase,dbPassword as char[])
    def mongoClient = new MongoClient( new ServerAddress(dbServer),Arrays.asList(credentials) )
    context.gmongo = new GMongo( mongoClient )
    context.mongoDB = context.gmongo.getDB(dbDatabase)

So i have been using the current code in order to create the connection. Actually I want this as three test suites. The First Test Suite would contain the groovy script to create the connection, The Second Test Suite would contain all of my Test Cases and the Third test suite would contain the mongo close connection script.

We use the Environment values from the properties file. Here the MongoServer has the values of the environment in which the connection is laid

I could not understand @Rao, how did you call the conn variable inside the test cases. Especially the context.testCase.testSuite.db?.connection part. What does the "?" denote and could you please tell me in the above context, how could carry out the process

解决方案

Below script address how you achieve what you are looking for in ReadyAPI / SoapUI. Note that you already know how to connect to gmongo in Groovy which you need to add that logic in the place holder by following the comment inline.

Below is the test suite level Setup Script to create the db connection.

class DatabaseDetails {
    def server
    def user
    def password
    def log
    def getConnection() {
        log.info 'connection created'
        //Write logic to create connection
    }
    def closeConnection() {
        log.info 'Closing connection'
        //Write logic to close connection
    }
}
//Change server, user, password values according to your environment
def db = [ server:'localhost', user:'dbuser', password: 'dbuserpasswd', log: log] as DatabaseDetails
if (!db.connection) {
    db.connection
    testSuite.metaClass.db = db 
}

Below is the test suite level TearDown Script to close the db connection. Since this is in tear down script, connection gets closed automatically as soon the test suite execution is completed.

testSuite.db?.closeConnection()

Now, there is no need to have step to create the db connection again and again. You just need to use below script in Groovy Script test step to get the existing db connection.

def conn = context.testCase.testSuite.db?.connection

Using conn variable, you should be able to execute the queries.

Note : Since the db connection is done in Setup Script of test suite, if you just run the test case(i.e., test suite is not invoked or executed), you may not able to get the connection. In such cases, manually execute the Setup Script of the test suite.

EDIT: Based on OP's edit to the question and his code snippet, here is the updated test suite's Setup Script. This takes care of implementation of getConnection() and closeConnection() based on OP's edit. Please add / edit import statements for Mongo classes that are used as I am not really aware of those.

Updated Test Suite's Setup Script

import com.gmongo.*
import com.mongodb.*

class DatabaseDetails {
    def context
    def log
    def mongoClient
    def mongoDB
    def getConnection() {
        log.info 'Creating connection.'
        //Write logic to create connection
        if (!mongoDB){        
        def credentials = MongoCredential.createCredential(
           context.expand('${#Project#MongoUser}'),
           context.expand('${#Project#MongoDatabase}'),
           context.expand('${#Project#MongoPassword}') as char[])
        mongoClient = new MongoClient( new ServerAddress(context.expand('${#Project#MongoServer}')),Arrays.asList(credentials) ) 
        mongoDB = new GMongo( mongoClient ).getDB(context.expand('${#Project#MongoDatabase}'))          
      }
      mongoDB
    }

    def closeConnection() {
        log.info 'Closing connection'
        //Write logic to close connection
        mongoClient.close()
    }
}

def db = [ context: context, log: log] as DatabaseDetails
if (!db.connection) {
    db.connection
    testSuite.metaClass.db = db 
}

As mentioned earlier, to get the connection, use below code and explaining it down.

context.testCase.testSuite.db?.connection

Groovy has great feature called ExpandoMetaclass. db is injected to testSuite class and db is object of DatabaseDetails class that we created and instantiated in Setup Script of test suite.

And db contains getConnection() i.e., db.getConnection() which can also same as db.connection. That is how connection is available in the above statement.

这篇关于创建一个mongo连接,并使它在Ready!API中执行一个完整的测试套件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆