简体   繁体   English

测试业务线(LOB).NET应用程序?

[英]Testing Line-of-Business (LOB) .NET Applications?

I was wondering if anyone here has experience in unit testing LOB applications (typically CRUD). 我想知道这里是否有人有单元测试LOB应用程序(通常是CRUD)的经验。

I have dabbled in the built-in unit testing tools in Visual Studio, but I have found it difficult to run tests that hit a database. 我已经涉足Visual Studio中的内置单元测试工具,但我发现很难运行打到数据库的测试。 Since the data changes, and combined with the fact that I have little idea what I'm doing, it seems very difficult to produce expected results and assert against them. 由于数据发生了变化,再加上我不知道自己在做什么这一事实,似乎很难产生预期的结果并断言它们。 Also I've even heard that you shouldn't run unit tests against databases... but how does everyone else out there perform unit tests on CRUD LOB software? 另外我甚至听说你不应该对数据库进行单元测试......但是其他人如何对CRUD LOB软件进行单元测试呢?

I hear so much about TDD and continuous integration with testing, but it seems like if i can't even create unit tests to begin with, I can't really use those methodologies. 我听到很多TDD和测试的持续集成,但似乎我甚至不能创建单元测试,我真的不能使用这些方法。 It makes since that a product like Notepad would be an easy one to create unit tests for... you have a certain amount of features and those features should always produce the same result. 因为像记事本这样的产品很容易创建单元测试...你有一定数量的功能,这些功能应该总是产生相同的结果。 But with LOB applications, you've got things like Sales Orders that could be created or deleted or modified in your testing environment. 但是对于LOB应用程序,您可以在测试环境中创建或删除或修改销售订单等内容。

Any insight would be appreciated! 任何见解将不胜感激!

Typically with CRUD you are going to need to either Mock or use an IOC Container for your data access layer so you aren't always hitting the database and "changeable data". 通常使用CRUD,您需要模拟或使用IOC容器作为数据访问层,这样您就不会总是访问数据库和“可变数据”。

With unit tests, you always expect the same values, so having it hooked up to a database is typically going to cause problems. 使用单元测试,您始终期望相同的值,因此将其连接到数据库通常会导致问题。

Check out RhinoMocks or Inversion Of Control Containers. 查看RhinoMocks或Inversion Of Control Containers。

I create a baseline database and use dbUnit to export it to a set of XML files. 我创建一个基线数据库并使用dbUnit将其导出到一组XML文件。 The first pass of the test is wipe out and populate the test database with the baseline. 测试的第一步是擦除并用基线填充测试数据库。 After that, you should be able to assert the unit tests against the expected values. 之后,您应该能够根据预期值断言单元测试。

Automated Testing is a broad category, which contains two smaller categories: Unit Testing and Integration Testing. 自动化测试是一个广泛的类别,它包含两个较小的类别:单元测试和集成测试。

Unit Testing is the practice of identifying the smallest units of your program, and writing procedures testing them in isolation from each other. 单元测试是识别程序中最小单元的过程,并编写相互隔离的程序。 When the smallest units of your program are coupled together tightly, it may be hard to test them in isolation from each other. 当程序的最小单元紧密耦合在一起时,可能很难彼此隔离地测试它们。 In that case, there are techniques and tools to help you (mocking and mocking frameworks). 在这种情况下,有一些技术和工具可以帮助您(模拟和模拟框架)。 In order to effectively do unit testing, your codebase needs to be written in a particular way, a way which supports unit testing - the smallest units of your program must not be coupled together. 为了有效地进行单元测试,您的代码库需要以特定的方式编写,这种方式支持单元测试 - 程序的最小单元不能耦合在一起。 Most programs are not built with this principle in mind, and so they are difficult to unit test. 大多数程序都没有考虑到这一原则,因此难以进行单元测试。 Unit test where you can, and keep that principle in mind for any new code you write, so that you can always unit test your new code. 您可以在哪里进行单元测试,并为您编写的任何新代码牢记这一原则,以便您始终可以对新代码进行单元测试。

Integration Testing is the practice of identifying features that are large units of your program integrating many smaller units, and writing procedures testing them in isolation from other large units of your program. 集成测试是一种识别功能的实践,这些功能是程序的大单元,集成了许多较小的单元,并且编写了与程序的其他大型单元隔离测试它们的过程。 For example, a procedure testing that your persistence layer correctly saves objects to persistent storage and correctly retrieves objects from persistent storage is an integration test, because it tests both your persistence layer as well as the TCP/IP pipeline, the database configuration, the database schema, the data already in the database, and the interaction between data already in the database and what your code is doing. 例如,测试持久层是否正确地将对象保存到持久存储并从持久存储中正确检索对象的过程是集成测试,因为它测试持久层以及TCP / IP管道,数据库配置,数据库模式,数据库中已存在的数据,以及数据库中已存在的数据与代码正在执行的操作之间的交互。 Definitely do integration testing, in addition to unit testing. 除了单元测试之外,绝对要进行集成测试。

Also, create a separate database purely for the purpose of automated integration tests. 此外,创建一个单独的数据库纯粹是为了自动集成测试。 Try to avoid running automated integration tests on a production or a development database, because you will just run into problems. 尽量避免在生产或开发数据库上运行自动化集成测试,因为您只会遇到问题。

I stumbled upon the same problem, you don't have that many options... 我偶然发现了同样的问题,你没有那么多的选择......

  • rollback after every test (using xtUnit for NUnit or the MbUnit rollback attribute). 每次测试后回滚(使用xtUnit for NUnit或MbUnit rollback属性)。 this works fine for typical unit tests of CRUD operations. 这适用于CRUD操作的典型单元测试。
  • use a backup restore strategy before after a test. 在测试之前使用备份恢复策略。 This will make unit test execution long. 这将使单元测试执行时间长。 This is however supported by MbUnit (2.X the attributes are not present in the 3.x trunk) 但是,MbUnit支持这一点(2.X中的属性不存在于3.x主干中)
  • create/drop a db using sql scripts. 使用sql脚本创建/删除数据库。

The biggest advantage with the last 2 options is that you can use them while executing so called integration tests (like when automating an UI where you need to support database changes on some other tier (physical server)). 最后两个选项的最大优点是,您可以在执行所谓的集成测试时使用它们(例如,当您需要在某个其他层(物理服务器)上支持数据库更改时自动化UI)。

I use the last one in combination with Watin for web app's. 我将最后一个与Watin结合用于网络应用程序。 For WPF and Win32 take a look at White , it looks promising. 对于WPF和Win32来看看White ,它看起来很有希望。

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

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