单元测试,Web服务和数据库事务 [英] Unit Testing, Web Services and Database Transactions

查看:101
本文介绍了单元测试,Web服务和数据库事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写测试用例为我的Web服务中,我可以回滚任何数据库的变化,他们可能会让一种方式。我可以尝试包围他们与交易范围,但如何指定事务上下文?换句话说,如何在交易知道回滚哪个数据库和服务器?在我的情况下,SQL服务器本地运行,以及Web服务。在你告诉我可以直接调用Web服务,无需客户端,请理解Web服务有非常具体的运行环境设置,这将是一个皇家疼痛重现我的测试案例。也许,交易范围是不是我想要的使用,是有别的选择吗?是否有一个数据库的功能,​​我可以打电话来启动一个事务?谢谢你。

I'm trying to write test cases for my web services in a way that I can rollback any database changes they may make. I can try to surround them with a transaction scope, but how do I specify a context for the transaction? In other words, how does the transaction know which database and server to rollback on? In my case, the SQL server runs locally as well as the web services. Before you tell me to call the web services directly without a client, please understand that the web services have very specific runtime environment settings that would be a royal pain to reproduce for my test cases. Perhaps, the transaction scope isn't what I want to use, is there an alternative? Is there a database function I could call to start a transaction? Thanks.

推荐答案

首先,你没有做单元测试。单元测试是关于测试code(功能)的单一的小单位。当您测试您创建的每个执行路径的单元测试功能,让你有测试code的全覆盖。但是,你的下测试系统包括客户服务沟通和服务数据库通信= code +配置的里三层外三层。这就是所谓的集成测试。

First you are not doing unit testing. Unit test is about testing single small unit of code (function). When you test a function you are creating unit test for each execution path so that you have full coverage of tested code. But your system under tests includes client to service communication and service to database communication = several tiers of code + configuration. That is called integration testing.

这里的问题是你是怎么设计你的服务?请问您的业务流程的交易?交易流程让你的客户开始交易,并把它传递给服务(分布式事务)。这不是默认行为,它需要WCF绑定的特殊配置。如果你使用这种方法,你可以做同样的测试。在试验开始的事务,并在试验结束时回滚事务。如果您没有设计服务流事务,因为您的交易在测试开始不会影响服务你根本无法使用它。在这种情况下,你有几种选择:

The problem here is how did you design your service? Does your service flow transactions? Transaction flow allows starting transaction at your client and pass it to the service (distributed transaction). It is not default behavior and it requires special configuration of WCF bindings. If you use this approach you can do the same in your test. Start transaction at test and rollback the transaction at the end of the test. If you didn't design service to flow transaction you simply can't use it because your transaction started in the test will not affect the service. In that case you have several choices:


  • 创建手动补偿。在每个测试运行定制SQL年底将数据移动到初始状态。这模拟回滚。我不推荐这种方法。

  • 在每次测试开始时重新创建数据库。这是缓慢的,但完全可以接受的,因为集成测试通常每天生成服务器几次才能运行。

  • 请不要测试WCF服务水平。 WCF服务应该是只对业务逻辑或数据访问逻辑的顶部一些包装。所以不要测试服务水平,而是考包裹层。你也许可以使用事务那里。这种方法可以让你有一些小的一套复杂的集成测试需要数据库娱乐和一些较大的一套测试它可以做回退并使用相同的数据库以及与previous一个组合。

这篇关于单元测试,Web服务和数据库事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆