简体   繁体   English

单元测试数据库方法

[英]Unit testing a database methodology

I am about to write unit tests for a new web application. 我即将为新的Web应用程序编写单元测试。

My preferred method is to test against the live database structure rather than an in-memory database. 我首选的方法是测试实时数据库结构而不是内存数据库。

I understand that I can wrap my unit test database calls inside transactions that are not committed. 据我所知,我可以将我的单元测试数据库调用包含在未提交的事务中。

What I think is a better approach is to copy the live database before initialising the tests, and allow the transactions to commit to the test database. 我认为更好的方法是在初始化测试之前复制实时数据库,并允许事务提交到测试数据库。 If the tests are run in the correct order this will allow me to create a complete test story from registering the first account, through their various interactions, ending with an account closure. 如果测试以正确的顺序运行,这将允许我通过注册第一个帐户,通过各种交互,以帐户关闭结束来创建完整的测试故事。 ie a real world lifecycle. 即现实世界的生命周期。 I am convinced this should be an excellent approach, but having never put this into practice yet I was wondering if people had attempted a similar approach and what the feedback is before I proceed. 我确信这应该是一个很好的方法,但从未将其付诸实践,但我想知道在我继续之前人们是否尝试过类似的方法和反馈意见。 Thanks in advance. 提前致谢。

I know I am going to be that guy by doing this, but lets establish our terms here before moving forward. 我知道我会通过这样做成为那个人 ,但是让我们在此之前建立我们的条款。 Skip this part if you only care about the answer. 如果您只关心答案,请跳过此部分。


A Unit Test should test just that, a single unit. 单元测试应该只测试一个单元。 It should not rely on external dependencies such as databases, web services, or file systems. 它不应该依赖于外部依赖项,例如数据库,Web服务或文件系统。 Instead, those dependencies should be mocked so that the state of each individual test can be precisely controlled. 相反,应该模拟这些依赖关系,以便可以精确控制每个单独测试的状态。

A Unit Test should be atomic, consistent and repeatable. 单元测试应该是原子的,一致的和可重复的。 As John points out, the test should be able to be run by itself, or in a suite with the exact same behavior every time. 正如John指出的那样,测试应该能够自己运行,或者每次都在具有完全相同行为的套件中运行。

An Integration Test should test the interaction between the various units that compose your system. 集成测试应测试组成系统的各个单元之间的交互。 Thus, they should never seek to stub or mock any dependencies. 因此,他们永远不应该寻求存根或模拟任何依赖。 These tests should be as close to testing what will be deployed to production as can be achieved. 这些测试应该尽可能接近测试可以实现的部署到生产的测试。


Let me be the first to tell you that while it is quite common for Integration Tests to be dependent on setup and tear down, tread lightly in this arena . 让我第一个告诉你,虽然集成测试依赖于设置和拆卸是很常见的,但在这个舞台上轻轻一点

Sure it seems like a really good idea to begin with, but for the same reason that procedural code usually ends up as a nightmare to maintain, procedural tests are even worse. 当然,开始时似乎是一个非常好的主意,但出于同样的原因,程序代码通常最终会成为维护的噩梦,程序测试甚至更糟。 Since they are so tightly coupled to the implementation details of the underlying code, you will end up having to change massive sets of tests if you ever change those details. 由于它们与底层代码的实现细节紧密耦合,因此如果您更改了这些细节,最终将不得不更改大量测试。

In the best case scenario you will throw away large sets of tests and start over. 在最好的情况下,您将丢弃大量测试并重新开始。 In the worst scenarios you will hesitate to refactor your system because you don't want to break your tests... and thus rob you of the very advantage they are supposed to bring. 在最糟糕的情况下,你会毫不犹豫地重构你的系统,因为你不想打破你的测试......从而剥夺了你应该带来的优势。

The best advice I can give you is to start reading up on Acceptance Testing , and Behavior Driven Development . 我能给你的最好建议是开始阅读验收测试行为驱动开发

Some People who can help: 有些人可以提供帮助:

Gojko Adzic , Bob Martin Gojko AdzicBob Martin

Some Tools that can help: 一些可以提供帮助的工具:

FitNesse , SpecFlow , Cucumber FitNesseSpecFlow黄瓜

Please! 请! Please! 请! Please! 请! read and learn from those guys, and those communities. 阅读并向这些人和那些社区学习。 They will steer you in the right direction and help you avoid long term problems with automated testing. 他们将引导您朝着正确的方向前进,并帮助您避免自动化测试的长期问题。

Unit tests should not depend on their order, and should not depend on the state of what is being tested. 单元测试不应该依赖于它们的顺序,也不应该取决于测试的状态。 They should set up all necessary state. 他们应该建立所有必要的状态。

What you've described may be a useful test, but it's not a set of unit tests. 你所描述的可能是一个有用的测试,但它不是一组单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM