katalon测试用例,如果sql查询返回正确的值

时间:2019-09-25 01:39:26

标签: katalon-studio

我有查询table 1 (table 1 has column A, B, C)中所有项目的数据文件/数据库 enter image description here

SELECT * from DB1.table1

然后测试用例

  
      
  1. 从数据文件中获取数据

         

    def数据= findTestData('数据文件/数据库')

  2.   
  3. 在A列中找到值123,然后检查C列是否传递了0,如果不是0,则失败。较早的代码有效。如下代码和结果。

  4.   
  5. 现在,问题是A列中可能会出现123次,如何通过使用datatime列来增强代码以仅检查最新值?

  6.   

import static com.kms.katalon.core.testdata.TestDataFactory.findTestData

import com.kms.katalon.core.logging.KeywordLogger
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.util.KeywordUtil

def KeywordLogger result = KeywordUtil.logInfo()

TestData data = findTestData('Data Files/Database')



for(i=1; i<= data.getRowNumbers(); i++){
	if(data.getValue(1, i)=='123'){
		println data.getValue(3,i)
		assert data.getValue(3,i) == '0'
		KeywordUtil.markPassed("test passed")
	}
}

测试用例结果,实际:123值为0。测试结果通过。

    2019-09-26 09:40:55.333 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
    2019-09-26 09:40:55.335 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/DatabaseTest/Get DB data - Copy - Copy
    2019-09-26 09:40:55.602 DEBUG testcase.Get DB data - Copy - Copy       - 1: result = logInfo()
    2019-09-26 09:40:55.605 INFO  com.kms.katalon.core.util.KeywordUtil    - null
    2019-09-26 09:40:55.607 DEBUG testcase.Get DB data - Copy - Copy       - 2: data = findTestData("Data Files/Database")
    2019-09-26 09:40:55.904 INFO  c.k.katalon.core.db.DatabaseConnection   - dbServerInfo = PostgreSQL 11.5 (Debian 11.5-1.pgdg90+1)
    2019-09-26 09:40:55.939 DEBUG testcase.Get DB data - Copy - Copy       - 3: for ([i = 1, i <= data.getRowNumbers(), (i++)])
    2019-09-26 09:40:55.954 DEBUG testcase.Get DB data - Copy - Copy       - 1: if (data.getValue(1, i) == "123")
    2019-09-26 09:40:56.042 DEBUG testcase.Get DB data - Copy - Copy       - 1: if (data.getValue(1, i) == "123")
    2019-09-26 09:40:56.043 DEBUG testcase.Get DB data - Copy - Copy       - 1: if (data.getValue(1, i) == "123")
    2019-09-26 09:40:56.044 DEBUG testcase.Get DB data - Copy - Copy       - 1: if (data.getValue(1, i) == "123")
    2019-09-26 09:40:56.045 DEBUG testcase.Get DB data - Copy - Copy       - 1: println(data.getValue(3, i))
    true
    2019-09-26 09:40:56.050 DEBUG testcase.Get DB data - Copy - Copy       - 2: assert data.getValue(3, i) == "0"
    2019-09-26 09:40:56.054 DEBUG testcase.Get DB data - Copy - Copy       - 3: markPassed("test passed")
    2019-09-26 09:40:56.057 DEBUG com.kms.katalon.core.util.KeywordUtil    - ✓ test passed
    2019-09-26 09:40:56.058 DEBUG testcase.Get DB data - Copy - Copy       - 1: if (data.getValue(1, i) == "123")
    2019-09-26 09:40:56.059 DEBUG testcase.Get DB data - Copy - Copy       - 1: if (data.getValue(1, i) == "123")
    2019-09-26 09:40:56.060 DEBUG testcase.Get DB data - Copy - Copy       - 1: if (data.getValue(1, i) == "123")
    2019-09-26 09:40:56.062 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/DatabaseTest/Get DB data - Copy - Copy

然后我将123更改为234。实际:234不是0。然后测试用例失败。

2019-09-26 09:44:38.155 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2019-09-26 09:44:38.157 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/DatabaseTest/Get DB data - Copy - Copy
2019-09-26 09:44:38.403 DEBUG testcase.Get DB data - Copy - Copy       - 1: result = logInfo()
2019-09-26 09:44:38.408 INFO  com.kms.katalon.core.util.KeywordUtil    - null
2019-09-26 09:44:38.409 DEBUG testcase.Get DB data - Copy - Copy       - 2: data = findTestData("Data Files/Database")
2019-09-26 09:44:38.681 INFO  c.k.katalon.core.db.DatabaseConnection   - dbServerInfo = PostgreSQL 11.5 (Debian 11.5-1.pgdg90+1)
2019-09-26 09:44:38.709 DEBUG testcase.Get DB data - Copy - Copy       - 3: for ([i = 1, i <= data.getRowNumbers(), (i++)])
2019-09-26 09:44:38.722 DEBUG testcase.Get DB data - Copy - Copy       - 1: if (data.getValue(1, i) == "234")
2019-09-26 09:44:38.798 DEBUG testcase.Get DB data - Copy - Copy       - 1: if (data.getValue(1, i) == "234")
2019-09-26 09:44:38.799 DEBUG testcase.Get DB data - Copy - Copy       - 1: if (data.getValue(1, i) == "234")
2019-09-26 09:44:38.801 DEBUG testcase.Get DB data - Copy - Copy       - 1: if (data.getValue(1, i) == "234")
2019-09-26 09:44:38.802 DEBUG testcase.Get DB data - Copy - Copy       - 1: println(data.getValue(3, i))
false
2019-09-26 09:44:38.806 DEBUG testcase.Get DB data - Copy - Copy       - 2: assert data.getValue(3, i) == "0"
2019-09-26 09:44:38.826 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/DatabaseTest/Get DB data - Copy - Copy FAILED.
Reason:
Assertion failed: 

assert data.getValue(3,i) == '0'
       |    |          |  |
       |    false      43 false
       com.kms.katalon.core.testdata.DBData@79145d5a

	at Get DB data - Copy - Copy.run(Get DB data - Copy - Copy:16)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1569462275661.run(TempTestCase1569462275661.groovy:21)

2019-09-26 09:44:38.838 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/DatabaseTest/Get DB data - Copy - Copy

2 个答案:

答案 0 :(得分:1)

也许您正在寻找某种中频条件来显示某些东西

 select t.a ,t.b,
     IF( t.c = 1 , "SUCCESS", "FAIL")  as status

 from table as t

答案 1 :(得分:1)

只要示例中的测试数据表'Data Files/Database'是excel表(不确定如何对数据库执行相同操作),此代码段即可完成您想要的操作:

def data = findTestData("Data Files/Database")

for(i=1; i<= data.getRowNumbers(); i++){
    if(data.getObjectValue(1, i)=='123'){
        println data.getObjectValue(3,i)
        assert data.getObjectValue(3,i) != '0'
    }
}