存储生成的动态唯一ID并解析到下一个测试用例 [英] Store generated dynamic unique ID and parse to next test case

查看:103
本文介绍了存储生成的动态唯一ID并解析到下一个测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关键字groovy,它使我能够生成用于测试数据目的的动态唯一ID.

I have keyword groovy which allowed me to generate a dynamic unique ID for test data purpose.

package kw
import java.text.SimpleDateFormat

import com.kms.katalon.core.annotation.Keyword


class dynamicId {

	//TIME STAMP
	String timeStamp() {
		return new SimpleDateFormat('ddMMyyyyhhmmss').format(new Date())
	}

	//Generate Random Number
	Integer getRandomNumber(int min, int max) {
		return ((Math.floor(Math.random() * ((max - min) + 1))) as int) + min
	}

	/**
	 * Generate a unique key and return value to service
	 */
	@Keyword
	String getUniqueId() {
		String prodName = (Integer.toString(getRandomNumber(1, 99))) + timeStamp()

		return prodName
	}
}

然后我有几个API测试用例,如下所示:

Then I have a couple of API test cases as below:

测试案例1:

通过调用关键字来发布测试数据.这个测试用例效果很好.

POST test data by calling the keyword. this test case works well.

动态唯一ID正在发布并存储在数据库中.

the dynamic unique ID is being posted and stored in the Database.

partial test case


//test data using dynamic Id
NewId = CustomKeywords.'kw.dynamicId.getUniqueId'()
println('....DO' + NewId)

GlobalVariable.DynamicId = NewId

//test data to simulate Ingest Service sends Dispense Order to Dispense Order Service.
def incomingDOInfo = '{"Operation":"Add","Msg":{"id":"'+GlobalVariable.DynamicId+'"}'

现在,测试用例2用作验证测试用例.

now, test case 2 served as a verification test case.

我需要验证GET API可以检索动态唯一ID(按ID获取数据,此ID应该与被发布的ID匹配).

where I need to verify the dynamic unique ID can be retrieved by GET API (GET back data by ID, this ID should matched the one being POSTED).

从测试用例1生成后,如何存储生成的动态唯一ID?

how do I store the generated dynamic unique ID once generated from test case 1?

我在测试用例1中具有"println('.... DO'+ NewId)",但我不知道如何使用它并将其放入测试用例2中.

i have the "println('....DO' + NewId)" in Test Case 1, but i have no idea how to use it and put it in test case 2.

我应该使用哪种方法取回生成的动态唯一ID?

which method should I use to get back the generated dynamic unique ID?

根据建议更新了测试案例2,效果很好.

updated Test Case 2 with the suggestion, it works well.

def dispenseOrderId = GlobalVariable.DynamicId
'Check data'
getDispenseOrder(dispenseOrderId)



def getDispenseOrder(def dispenseOrderId){
	def response = WS.sendRequestAndVerify(findTestObject('Object Repository/Web Service Request/ApiDispenseorderByDispenseOrderIdGet', [('dispenseOrderId') : dispenseOrderId, ('SiteHostName') : GlobalVariable.SiteHostName, , ('SitePort') : GlobalVariable.SitePort]))
	println(response.statusCode)
	println(response.responseText)
	WS.verifyResponseStatusCode(response, 200)
	
	println(response.responseText)
	
	//convert to json format and verify result
	def dojson = new JsonSlurper().parseText(new String(response.responseText))
	println('response text: \n' + JsonOutput.prettyPrint(JsonOutput.toJson(dojson)))
	
	assertThat(dojson.dispenseOrderId).isEqualTo(dispenseOrderId)
	assertThat(dojson.state).isEqualTo("NEW")
}

===================

====================

更新帖子以尝试#2建议,有效

updated post to try #2 suggestion, works

TC2

//retrieve the dynamic ID generated at previous test case
def file = new File("C:/DynamicId.txt")


//Modify this to match test data at test case "IncomingDOFromIngest"
def dispenseOrderId = file.text
'Check posted DO data from DO service'
getDispenseOrder(dispenseOrderId)



def getDispenseOrder(def dispenseOrderId){
	def response = WS.sendRequestAndVerify(findTestObject('Object Repository/Web Service Request/ApiDispenseorderByDispenseOrderIdGet', [('dispenseOrderId') : dispenseOrderId, ('SiteHostName') : GlobalVariable.SiteHostName, , ('SitePort') : GlobalVariable.SitePort]))
	println(response.statusCode)
	println(response.responseText)
	WS.verifyResponseStatusCode(response, 200)
	
	println(response.responseText)
	
}

推荐答案

我可以想到多种方式.

1.将值od动态ID存储在GlobalVariable

如果在测试套件中运行测试用例1(TC1)和TC2,则可以使用全局变量进行存储.

If you are running Test Case 1 (TC1) and TC2 in a test suite, you can use the global variable for inter-storage.

您已经在TC1中这样做了:

You are already doing this in the TC1:

GlobalVariable.DynamicId = NewId

现在,如果TC1和TC2作为同一测试套件的一部分运行,这将仅工作.这是因为GlobalVariables是

Now, this will only work if TC1 and TC2 are running as a part of the same test suite. That is because GlobalVariables are reset to default on the teardown of the test suite or the teardown of a test case when a single test case is run.

让我们说您检索了GET响应并将其放在 response 变量中.

Let us say you retrieved the GET response and put it in a response variable.

assert response.equals(GlobalVariable.DynamicId)

2.将值od动态ID存储在文件系统上

即使您单独运行测试用例(即不在测试套件中),此方法仍然有效.

This method will work even if you run the test cases separately (i.e. not in a test suite).

您可以使用文件系统将ID值永久存储到文件中.有各种时髦的方法可以帮助您.

You can use file system for permanently storing the ID value to a file. There are various Groovy mehods to help you with that.

这是一个有关如何将ID存储到文本文件的示例. c:/path-to/variable.txt :

Here's an example on how to store the ID to a text file c:/path-to/variable.txt:

def file = new File("c:/path-to/variable.txt")
file.newWriter().withWriter { it << NewID }
println file.text

TC2需要此断言(根据您的需要进行调整):

The TC2 needs this assertion (adjust according to your needs):

def file = new File("c:/path-to/variable.txt")
assert response.equals(file.text)

还要确保您也在TC2中定义了 file .

Make sure you defined file in TC2, as well.

3.返回TC1末尾的ID值,并将其用作TC2的输入

这还假设TC1和TC2在同一测试套件中.您可以使用

This also presupposes TC1 and TC2 are in the same test suite. You return the value of the ID with

return NewId

,然后将其用作TC2的输入参数.

and then use as an input parameter for TC2.

4.使用测试监听器

这与第一种解决方案相同,只是使用测试侦听器创建一个临时的保持变量,该变量将在测试套件运行期间处于活动状态.

This is the same thing as the first solution, you just use test listeners to create a temporary holding variable that will be active during the test suite run.

这篇关于存储生成的动态唯一ID并解析到下一个测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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