katalon 测试用例 if sql query return 正确值

katalon test case if sql query return correct value

我有数据 files/database 查询 table 1 (table 1 has column A, B, C) 中的所有项目

SELECT * from DB1.table1

然后测试用例

  1. get data from data files

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

  2. find value 123 in column A, then check column C if 0 passed, if not 0 then failed. earlier codes works. as below codes and result.

  3. now, there is issue that 123 may appear multiple times in column A, how to enhance codes to check only the latest value by using datatime column?

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。然后测试用例失败。

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code>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[=15=].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

也许您正在寻找某种 IF 条件来显示某些内容

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

 from table as t

如果示例中的测试数据 table 'Data Files/Database' 是 excel table(不确定如何执行与数据库相同):

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'
    }
}