在 SQLScript 中使用库的现有机制可重复用于编写最终用户测试。引入语言类型 SQLSCRIPT TEST 以指定库包含最终用户测试。目前,此语言类型只能用于库。 备注为确保明确区分生产编码和仅测试编码,该语言类型的库不能用于任何不使用语言类型 SQLSCRIPT TEST 的函数、过程或库。
CREATE LIBRARY LIB_TEST LANGUAGE SQLSCRIPT TEST AS BEGIN END;在此类测试库的正文中,您可以使用某些 SQLScript 编译指示将库成员过程标记为测试或测试相关编码: @Test(), @TestSetup(), @TestTeardown(), @TestSetupConfig('ConfigName'), @TestTeardownConfig('ConfigName'), @TestSetupLibrary() as well as @TestTearDownLibrary(). 这些编译指示指令仅适用于库成员过程,并且过程可能没有任何参数。
备注 所有这些编译指示指令都是可选的,缺省情况下在 SQLSCRIPT TEST 库中不是必需的。但是,要使库成员过程能够由 SQLScript 测试框架作为最终用户测试调用,至少需要 @Test() 编译指示指令。 示例代码CREATE LIBRARY LIB_TEST LANGUAGE SQLSCRIPT TEST ASBEGIN @TestSetUpLibrary() public procedure SetUpLibrary() as begin select 'SetUpLibrary' from dummy; end; @TestTearDownLibrary() public procedure TearDownLibrary() as begin select 'whatever' from dummy; end; @TestClassification('FAST','base') @TestSetUpConfig('config1') public procedure SetUpConfig1() as begin truncate table tab_test; insert into tab_test values(1, 'first entry'); insert into tab_test values(2, 'second entry'); insert into tab_test values(3, 'third entry'); end; @TestSetUpConfig('config2') public procedure SetUpConfig2() as begin truncate table tab_test; insert into tab_test values(5, 'fifth entry'); insert into tab_test values(6, 'sixth entry'); insert into tab_test values(7, 'seventh entry'); end; @TestSetUpConfig('config3') public procedure SetUpConfig3() as begin truncate table tab_test; insert into tab_test values(5, 'some pattern string'); end; @TestTearDownConfig('config1', 'config2', 'config3') public procedure TearDownConfig() as begin truncate table tab_test; end; @TestSetUpTest() public procedure SetUpTest() as begin using sqlscript_test as testing; declare num_entries int = record_count(tab_test); testing:expect_ne(0, num_entries); end; @TestTearDownTest() public procedure TearDownTest() as begin select 'whatever' from dummy; end; @TestClassification('SLOW') @Test() public procedure TestA as begin using sqlscript_test as testing; tab1 = select 'A1' as A from dummy; tab2 = select 'A2' as A from dummy; testing:expect_table_eq(:tab1, :tab2); end; @Test() public procedure TestC as begin using sqlscript_test as testing; declare str nclob; call proc_test(:str); testing:expect_eq('some replaced string', :str); end;END; 要运行上述示例 SQLSCRIPT TEST 库,还需要一个要测试的对象,例如以下过程: 示例代码CREATE TABLE TAB_TEST(A INT, B NCLOB);CREATE PROCEDURE PROC_TEST(OUT result VARCHAR(20)) ASBEGIN DECLARE str STRING; SELECT B INTO str FROM TAB_TEST WHERE A = 5; IF LOCATE(:str, 'pattern') <> 0 THEN result = REPLACE(:str, 'pattern', 'replaced'); ELSE result = :str; END IF;END;
调用最终用户测试时,SQLScript 测试框架考虑 SQLSCRIPT TEST 库的成员过程,用上述编译指示指令之一标记。但是,在此类库中仍然可以有附加成员函数或过程,没有任何编译指示指令。然后,这些可用作帮助器或用于分隔通用编码。
具有这些编译指示指令的库成员过程的执行顺序定义如下:
1. @TestSetupLibrary()
- @TestSetupConfig('Config1')
- @TestSetup()
- @Test()
- @TestTeardown()
- @TestSetUp()
- @Test()
- @TestTeardown()
- [...]
- @TestTeardownConfig('Config1')
- @TestSetupConfig('Config2')
- @TestSetup()
- @Test()
- @TestTeardown()
- @TestSetUp()
- @Test()
- @TestTeardown()
- [...]
- @TestTeardownConfig('Config2')
- [...]
- @TestTeardownLibrary()
备注如果具有 SetUp 编译指示指令之一的库成员过程执行失败,则不会执行相应的 TearDown 以及测试。通过 @TestClassification(…) 编译指示指令、SetUpLibrary、SetUpConfiguration 和 Test,可以为过程分配可在测试过滤器中使用的附加标记。