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



相关文章
|
1月前
|
数据库连接 Go 数据库
Go语言中的错误注入与防御编程。错误注入通过模拟网络故障、数据库错误等,测试系统稳定性
本文探讨了Go语言中的错误注入与防御编程。错误注入通过模拟网络故障、数据库错误等,测试系统稳定性;防御编程则强调在编码时考虑各种错误情况,确保程序健壮性。文章详细介绍了这两种技术在Go语言中的实现方法及其重要性,旨在提升软件质量和可靠性。
32 1
|
2月前
|
NoSQL 关系型数据库 MySQL
AWS Database Migration Service 助力数据库搬迁
AWS Database Migration Service 助力数据库搬迁
|
7月前
|
JavaScript Java 测试技术
大学生体质测试|基于Springboot+vue的大学生体质测试管理系统设计与实现(源码+数据库+文档)
大学生体质测试|基于Springboot+vue的大学生体质测试管理系统设计与实现(源码+数据库+文档)
122 0
|
4月前
|
安全 测试技术 网络安全
深入理解数据库黑盒测试
【8月更文挑战第31天】
60 0
|
5月前
|
SQL 关系型数据库 MySQL
云服务器 ECS产品使用问题之出现“1044 - Access denied for user ‘root‘@‘%‘ to database ‘数据库名称‘”这样的错误,该怎么办
云服务器ECS(Elastic Compute Service)是各大云服务商阿里云提供的一种基础云计算服务,它允许用户租用云端计算资源来部署和运行各种应用程序。以下是一个关于如何使用ECS产品的综合指南。
|
5月前
|
监控 Oracle 关系型数据库
关系型数据库Oracle恢复测试
【7月更文挑战第20天】
99 7
|
5月前
|
关系型数据库 MySQL 测试技术
数据库升级是一个涉及数据备份、新版本安装、数据迁移和测试等关键环节的复杂过程
【7月更文挑战第21天】数据库升级是一个涉及数据备份、新版本安装、数据迁移和测试等关键环节的复杂过程
146 1
|
5月前
|
测试技术 数据库 容器
开发与运维测试问题之操作数据库进行DAO层测试如何解决
开发与运维测试问题之操作数据库进行DAO层测试如何解决
|
7月前
|
安全 测试技术 Go
Golang深入浅出之-Go语言单元测试与基准测试:testing包详解
【4月更文挑战第27天】Go语言的`testing`包是单元测试和基准测试的核心,简化了测试流程并鼓励编写高质量测试代码。本文介绍了测试文件命名规范、常用断言方法,以及如何进行基准测试。同时,讨论了测试中常见的问题,如状态干扰、并发同步、依赖外部服务和测试覆盖率低,并提出了相应的避免策略,包括使用`t.Cleanup`、`t.Parallel()`、模拟对象和检查覆盖率。良好的测试实践能提升代码质量和项目稳定性。
127 1
|
7月前
|
SQL 调度 数据库
【Database】Sqlserver如何定时备份数据库和定时清除
【Database】Sqlserver如何定时备份数据库和定时清除
277 2