自动测试框架 YAT
Yet another test (YAT) 框架是基于 Python3 的自动测试框架。该框架的核心由 Kotlin 语言实现。该框架通过 Python 进行封装和绑定,以提供命令行接口 (CLI)。图 1 显示了总体框架。 YAT 框架不断发展成为更高效、更先进的自动测试框架。
YAT是用于openGauss数据库规范监控的自动测试框架。 openGauss 2.1.0不仅发布了开源YAT,还贡献了超过30000个自动测试用例,以提升openGauss社区的测试能力,丰富openGauss生态,吸引更多的开发者参与社区建设。 YAT代码库:https://gitee.com/opengauss/Yat
[](https://res.cloudinary.com/practicaldev/image/fetch/s--Wi-6KK3Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev- to-uploads.s3.amazonaws.com/uploads/articles/2r7mil2o2lg2lvgpn9lw.jpg)
图 1 总体 YAT 框架
产品优势
-
对数据库测试很友好。用户可以直接编写 SQL 代码并将代码组织成测试套件进行测试,无需额外配置。测试用例通过 JDBC API 执行,可以适配各种数据库。
-
支持多种语言,可扩展。
目前支持 SQL、Shell、Python (unittes) 和 Groovy (Junit/Spock) 等语言。可以通过适配器添加新的语言和框架。 YAT 支持的 SQL 语句是标准 SQL 语句的超集。即 YAT 在标准 SQL 语句的基础上进行了扩展。用户可以在 SQL 脚本中运行 shell 命令、控制连接、执行循环、绑定 SQL 语句、控制多个会话以及并发执行 SQL 语句。例如:
@conn user/passwd@127.0.0.1:9090; -- Reconnect to the database as the new user.
drop table if exists tbl_x; -- Execute SQL statements.
create table tbl_x (id int, age int, xb int);
insert into tbl_x values(1, 2, 4);
insert into tbl_x values(3, 4, 5);
-- Perform the binding operation.
insert into tbl_x values(?, ?, ?);
@bind {
int 3
int 5
int 7
}
-- Perform the binding operation in batches.
insert into tbl_x values(?, ?, ?);
@batch {
int 3 int 4 int 0
int 3 int 4 int 9
int 3 int 4 int 8
int 3 int 4 int 7
}
-- Run the shell commands.
@sh zctl.py -t stop;
@sh zctl.py -t start;
-- Define sessions.
@session(name: s1)
{
@set autocommit false;
update table tbl_x set par1 = 2 par2 = 2;
insert into tbl_x values(1, 3, 4);
commit;
}
@session(name: s2, user: abc, password: 'gauss@123')
{
@set autocommit false;
update table tbl_x set par1 = 2 par2 = 2;
insert into tbl_x values(1, 3, 4);
@step
{
select * from tbl_x for update;
}
commit;
}
-- Executes SQL statements in sessions.
@steps s1.0 s2.0 s1.1 s1.2 s2.2 s2.1;
-- Execute loops.
@for (count: 10)
{
insert into abc values(1,1,3,4);
}
-- Concurrently execute SQL statements.
@parallel {
@session {
select * from abc for update;
commit;
}
@session {
select * from abc for update;
commit;
}
}
进入全屏模式 退出全屏模式
Python 语言测试脚本必须是 unittest 测试脚本。 YAT 提供了一个公共库来方便 Python unittest 测试脚本中的数据库和远程 SSH 操作。例如:
class TestPrimaryStandby(TestCase):
node = None
node_standby = None
test_table_drop = 'drop table if exists tbl_test';
test_table_create = '''create table tbl_test (
id int,
name char(20),
address varchar(1024)
)
'''
@classmethod
def setUpClass(cls):
# Initialize the connection object.
cls.node = Node(node='primary')
cls.node_standby = Node(node='standby')
cls.node.sql(cls.test_table_drop)
cls.node.sql(cls.test_table_create)
@classmethod
def tearDownClass(cls) -> None:
cls.node.close()
cls.node_standby.close() # Close the connection object.
def test_abc_001(self):
# Run the shell command through SSH and check the result.
self.node.sh('echo "success"').expect('success')
# Run the SQL statement and check the result.
self.node.sql('select * from tbl_test').expect(
(1, 'xxx', 'xxxxxxxxxxxxxxxxxxx'),
(2, 'xxx', 'xxxxxxxxxxxxxxxxxxx'))
def test_abc_003(self):
# Run the shell command and determine the result through regular expression matching.
self.node.sh('cm ctl query').regex(r'.*success.*')
进入全屏模式 退出全屏模式
Groovy 脚本可用于编写 JUnit 案例或 Spock 案例。更多详情,请访问官方网站。
- 定义了一套标准的测试用例编写规范、测试执行流程、测试报告呈现方式。
运行 yat init 命令以创建测试套件模板。 yat init 命令执行后,修改指定测试套件目录下的内容。然后,安装JDBC驱动,将lib目录添加到测试套件的根目录下,将驱动复制到该目录下,运行yat suite run命令运行测试套件。您可以添加不同的参数来设置是否打印报告和报告格式。图 2 显示了测试套件目录结果。在运行测试套件之前配置以下目录:
-
conf目录,存放节点配置文件。
-
除了目录,这是测试用例的预期文件。
-
调度目录,存放调度文件。
-
testcase目录,存放测试用例文件。
[](https://res.cloudinary.com/practicaldev/image/fetch/s--sMfCKRfD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to- uploads.s3.amazonaws.com/uploads/articles/bqtna8lxijzube5689qw.jpg)
图2 测试套件目录结构
- Multi-suite Schedule YAT Schedule是YAT提供的一个调度器,用于同时调度多个YAT测试套件。您可以通过自定义调度文件以并行或串行模式调度多个 YAT 测试套件。当有大量测试套件时,您需要确定测试套件的组合和顺序。 YAT 提供了一种方便的方法来将多个测试套件组织成一个大型测试套件,如下图所示。
# File name: all.ys
serial {# Serial execution of test suites or test suite sets
suite '../suite1';
suite '../suite2';
...
parallel { # Parallel execution of test suites or test suite sets
suite 'parallel/suite1';
suite 'parallel/suite2';
...
serial { ... }
}
suite 'suite3';
...
}
进入全屏模式 退出全屏模式
运行以下命令,一键执行所有测试套件:
yat schedule -s all.ys
YAT Schedule 是 YAT 提供的一个调度器,用于同时调度多个 YAT 测试套件。您可以通过自定义调度文件以并行或串行模式调度多个 YAT 测试套件。当有大量测试套件时,您需要确定测试套件的组合和顺序。 YAT 提供了一种方便的方法来将多个测试套件组织成一个大型测试套件,如下图所示。
文件名:all.ys
serial {# 测试套件或测试套件集的串行执行
套房'../suite1';
套房'../suite2';
...
parallel { # Parallel execution of test suites or test suite sets
suite 'parallel/suite1';
suite 'parallel/suite2';
...
serial { ... }
}
suite 'suite3';
...
进入全屏模式 退出全屏模式
}
运行以下命令,一键执行所有测试套件:
yat schedule -s all.ys
进入全屏模式 退出全屏模式
更多推荐
所有评论(0)