数据库单元测试 What is database unit testing?

简介:
+关注继续查看

Unit Testing, while a well understood technique in the application development world, is not nearly as prevalent in the data community. Therefore, before jumping into how exactly you would utilize this technique and our toolset around it, you must understand the overall fundamentals of the unit testing methodology. And that's exactly what I'm about to help you do :)

 

Unit testing provides a structured and automated way for testing individual components of a system. Unit tests are most often authored by the developer of the component under test. As a methodology, it has many advantages over manual ad hoc testing and debugging. By coding test methods, the developer can create a battery of tests that can be run during development to ensure features work as expected. Since unit tests focus specifically on an individual method under test, it is much easier to determine the source of a failure for a failing unit test. Such a battery of tests is very useful for regression testing, since as new features are implemented, existing tests can be re-run to ensure existing functionality has not been broken. Unit tests, in addition, serve as documentation for users of the methods under test. Developers can quickly review unit tests to determine how exactly they are expected to consume particular components.

 

How does this all relate to database development? The direct analog of application unit tests in the database world are tests of a database’s programmability objects. These include, for example, a database’s stored procedures, functions, and triggers.

 

What might a database unit test for a stored procedure look like? Let’s say you are attempting to test the CustOrderHist stored procedure in the Northwind database. The stored procedure should give you back the order history for a given customer ID. To test this, you can imagine writing a SQL script that executed the stored procedure and checked whether the expected number of rows were returned. Such a script might look like the following:

 

DECLARE @CustomerId nchar(5)

SELECT @CustomerId = 'EASTC'

EXEC dbo.CustOrderHist @CustomerId

 

IF (@@ROWCOUNT <> 19)

RAISERROR('Actual Rowcount not equal to expected 19',1,1)

 

So what you are looking at above is a very simple unit test for a stored procedure.

 

Unit testing is not, however, limited to testing the database’s programmability objects. There are many other sorts of database unit tests that one may wish to write. I'll save the specifics about the different kinds of tests you may want to create in another post.


    本文转自灵动生活博客园博客,原文链接:http://www.cnblogs.com/ywqu/archive/2009/03/03/1402345.html,如需转载请自行联系原作者



相关文章
|
3月前
|
SQL 存储 安全
在 Go 中如何使用 database/sql 来操作数据库
在 Go 中如何使用 database/sql 来操作数据库
|
3月前
|
存储 Oracle 关系型数据库
|
5月前
|
测试技术 Go
Golang:testing单元测试的使用示例
Golang:testing单元测试的使用示例
29 0
Golang:testing单元测试的使用示例
|
7月前
|
关系型数据库 MySQL 数据库
mysql 数据库中出现no database selected
mysql 数据库中出现no database selected
361 1
|
7月前
|
存储 运维 NoSQL
如何运维多集群数据库?58 同城 NebulaGraph Database 运维实践
随着 58 同城内部图数据库 NebulaGraph 应用的场景越来越多,集群数量与日俱增,如何高效地管理多个数据库集群便成了首要解决的问题,而本文所实践的部分运维内容同样也适用于其他数据库运维。
108 0
|
8月前
|
存储 关系型数据库 API
数据库(Database
数据库(Database
41 0
|
9月前
|
测试技术 PHP
TP5.0安装testing 单元测试 报错
原因:使用了比较高版本的php,topthink/tesing v1.x仅限php7.1使用 太高太低都会出现报错
56 0
|
9月前
|
数据库
UVA1592 数据库 Database
UVA1592 数据库 Database
|
存储 SQL Oracle
MySQL数据库(1):数据库 Database 基本概念
MySQL数据库(1):数据库 Database 基本概念
|
SQL Kubernetes 负载均衡
Database Mesh 2.0 如何在云原生场景下提高数据库治理性能?
在计算机科学的世界里,操作系统和数据库可谓是两大最重要的基础软件。就拿 SQL 这门语言来说,它的半衰期之长令人记忆深刻。SQL 不仅在早期的 DBMS 系统中扮演了相当重要的角色,近些年在数据科学领域和 Python 一同成为从业人员的必备技能。SQL 的生命力真可谓是“历久弥新”,以至于有论文直言希望“One SQL to rule all”。这也从侧面反映了数据库领域历史之久,地位之重,具备浓重的领域特色。
158 0
Database Mesh 2.0 如何在云原生场景下提高数据库治理性能?
推荐文章
更多