数据库单元测试 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,如需转载请自行联系原作者



相关文章
|
9月前
|
数据库
【YashanDB数据库】YAS-02079 archive log mode must be enabled when database is in replication mode
YAS-02079 archive log mode must be enabled when database is in replication mode
|
JavaScript Java 测试技术
大学生体质测试|基于Springboot+vue的大学生体质测试管理系统设计与实现(源码+数据库+文档)
大学生体质测试|基于Springboot+vue的大学生体质测试管理系统设计与实现(源码+数据库+文档)
321 0
|
数据库连接 Go 数据库
Go语言中的错误注入与防御编程。错误注入通过模拟网络故障、数据库错误等,测试系统稳定性
本文探讨了Go语言中的错误注入与防御编程。错误注入通过模拟网络故障、数据库错误等,测试系统稳定性;防御编程则强调在编码时考虑各种错误情况,确保程序健壮性。文章详细介绍了这两种技术在Go语言中的实现方法及其重要性,旨在提升软件质量和可靠性。
194 1
|
资源调度 前端开发 JavaScript
React 测试库 React Testing Library
【10月更文挑战第22天】本文介绍了 React Testing Library 的基本概念和使用方法,包括安装、基本用法、常见问题及解决方法。通过代码案例详细解释了如何测试 React 组件,帮助开发者提高应用质量和稳定性。
405 1
|
中间件 Java 测试技术
单元测试问题之编写单元测试时运行环境、数据库、中间件问题如何解决
单元测试问题之编写单元测试时运行环境、数据库、中间件问题如何解决
259 51
|
NoSQL 关系型数据库 MySQL
AWS Database Migration Service 助力数据库搬迁
AWS Database Migration Service 助力数据库搬迁
|
SQL 关系型数据库 MySQL
云服务器 ECS产品使用问题之出现“1044 - Access denied for user ‘root‘@‘%‘ to database ‘数据库名称‘”这样的错误,该怎么办
云服务器ECS(Elastic Compute Service)是各大云服务商阿里云提供的一种基础云计算服务,它允许用户租用云端计算资源来部署和运行各种应用程序。以下是一个关于如何使用ECS产品的综合指南。
|
监控 Oracle 关系型数据库
关系型数据库Oracle恢复测试
【7月更文挑战第20天】
311 7
|
关系型数据库 MySQL 测试技术
数据库升级是一个涉及数据备份、新版本安装、数据迁移和测试等关键环节的复杂过程
【7月更文挑战第21天】数据库升级是一个涉及数据备份、新版本安装、数据迁移和测试等关键环节的复杂过程
361 1
|
安全 测试技术 网络安全
深入理解数据库黑盒测试
【8月更文挑战第31天】
262 0