单元测试、Web 服务和数据库事务

发布于 2024-09-17 11:12:31 字数 259 浏览 0 评论 0原文

我正在尝试为我的 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

善良天后 2024-09-24 11:12:31

首先,您不进行单元测试。单元测试是关于测试单个小单元的代码(函数)。当您测试函数时,您正在为每个执行路径创建单元测试,以便完全覆盖已测试的代码。但是您的测试系统包括客户端到服务的通信以及服务到数据库的通信=几层代码+配置。这就是所谓的集成测试。

这里的问题是你是如何设计你的服务的?你们的服务有交易流程吗?事务流允许在客户端启动事务并将其传递给服务(分布式事务)。这不是默认行为,需要对 WCF 绑定进行特殊配置。如果您使用这种方法,您可以在测试中执行相同的操作。在测试时启动事务并在测试结束时回滚事务。如果您没有将服务设计为流动事务,您根本无法使用它,因为您在测试中启动的事务不会影响服务。在这种情况下,您有多种选择:

  • 创建手动补偿。在每次测试结束时运行自定义 SQL 将数据移至初始状态。这模拟了回滚。我不推荐这种方法。
  • 在每次测试开始时重新创建数据库。这很慢,但完全可以接受,因为集成测试通常每天只在构建服务器上运行几次。
  • 不要测试 WCF 服务级别。 WCF 服务应该只是业务逻辑或数据访问逻辑之上的一些包装器。因此,不要测试服务级别,而是测试包装层。您也许可以在那里使用交易。这种方法可以与前一种方法很好地结合起来,这样您就可以进行一些需要重新创建数据库的复杂集成测试,以及一些可以进行回滚并使用相同数据库的较大测试集。

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.

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:

  • Create manual compensation. At the end of each test run custom SQL to move data to initial state. This simulates rollback. I don't recommend this approach.
  • Recreate database at the beginning of each test. This is slow but fully acceptable because integration tests are usually run only on build server few times per day.
  • Don't test WCF service level. WCF service should be only some wrapper on the top of business logic or data access logic. So don't test service level but instead test the wrapped layer. You can probably use transactions there. This approach can be well combined with previous one so that you have some small set of complex integration tests which requires database recreation and some bigger set of tests which can do rollback and use the same database.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文