测试ASP.Net数据驱动App的最佳方法

时间:2022-08-10 03:36:34

I'm looking at setting up unit tests and integration testing, but I've run into the problem that the connection string in the web.config is unavailable for testing. The solution we've come up with to get around this is to have another connection string for testing, but I was wondering if there was a better way.

我正在考虑设置单元测试和集成测试,但我遇到了web.config中的连接字符串无法进行测试的问题。我们提出解决这个问题的解决方案是使用另一个连接字符串进行测试,但我想知道是否有更好的方法。

The other half of the issue is setting up and tearing down the test db. Right now we just have a giant test that sets up the db before running and then tears it down afterwards. That way we can control the order that the test run to make sure the results are proper.

问题的另一半是设置和拆除测试数据库。现在我们只有一个巨大的测试,在运行之前设置数据库然后在之后撕下它。这样我们就可以控制测试运行的顺序,以确保结果正确。

Just looking to see if there are better ways/tools to use for testing an ASP.Net app.

只是想看看是否有更好的方法/工具用于测试ASP.Net应用程序。

3 个解决方案

#1


0  

The recommended approach is to architect your system that you can test your code without a DB. What this means to a code perspective is using seperation of concerns. You want all of your core business logic seperated from your pages. You can do this using a MVC or MPV pattern, which if you good you can find a great deal about.

建议的方法是构建您的系统,您可以在没有DB的情况下测试代码。对于代码透视,这意味着使用关注点分离。您希望从您的页面中分离出所有核心业务逻辑。你可以使用MVC或MPV模式来做到这一点,如果你很好,你可以找到很多。

Right now there is ASP.Net MVC; however, the MVC pattern has been used in ASP.Net long before this framework existed so if your looking at enhancing an existing web forms app make sure you don't end up looking at the more recent buzz which is about ASP.Net MVC as a programing model in ASP.Net.

现在有ASP.Net MVC;然而,在该框架存在之前很久就已经在ASP.Net中使用了MVC模式,所以如果你想要增强现有的web表单应用程序,请确保你最终不会看到最近关于ASP.Net MVC的嗡嗡声。 ASP.Net中的编程模型。

Now let's say you have your core logic for handling a button click isolated from page so you have a Class let's call it WidgetController. The widget controller might have a method of HandleClick() which performs your business logic.

现在让我们假设您拥有处理从页面隔离的按钮单击的核心逻辑,这样您就可以将它称为WidgetController。窗口小部件控制器可能有一个HandleClick()方法,用于执行业务逻辑。

Let's further assume that your business logic requires data access. You can again use seperation of concerns. Your business logic shouldn't care how to access the DB, all it needs is the data let another class get the data. A popular way of seperating data is by using a Depedancy Injection or Inversion of Control model (DI, IoC respectivley). Essentially what you do is define an interface for your data access, and your controller will program against the interface. You then at runtime provide the actual class to your controller via some method (Property, Constructor etc...)

让我们进一步假设您的业务逻辑需要数据访问。您可以再次使用关注点。您的业​​务逻辑不应该关心如何访问数据库,它所需要的只是数据让另一个类获取数据。一种流行的分离数据的方法是使用Depedancy Injection或Inversion of Control模型(DI,IoC respectivley)。基本上,您所做的是为数据访问定义一个接口,您的控制器将针对该接口进行编程。然后,您在运行时通过某种方法(Property,Constructor等...)向控制器提供实际的类。

What this does is it allows you to provide MOCK implementations during runtime which seperates your DB. The mocks will implement your interface and you can just new up the objects your tests will need store them in memory.

这样做是因为它允许您在运行时提供MOCK实现,从而分离您的数据库。模拟将实现您的界面,您只需要新建您的测试需要将它们存储在内存中的对象。

#2


1  

Actually, as mentioned mocking and using stubs to test system interaction without a database interaction is great, but intergration testing can be just a valid.

实际上,正如所提到的,模拟和使用存根来测试系统交互而没有数据库交互是很好的,但是集成测试可能只是有效的。

What you mention with regards to scripting out the database tables and data, then running a setup script prior to all integration tests, is a fairly sensible approach.

您提到的脚本化数据库表和数据,然后在所有集成测试之前运行安装脚本,是一种相当明智的方法。

Dont know how much of a fan of ORMs you are, but using NHibernate you can rebuild your tables from the mappings. After rebuilding the database I usually just have a script that physically creates the objects and persists them via NHibernate. I know this isnt the best approach but I usually find that the amount of startup test data I need isnt that large, and also inserting via NHibernate is a test itself :-).

不知道你有多少ORM粉丝,但使用NHibernate你可以从映射重建你的表。在重建数据库之后,我通常只有一个物理创建对象的脚本,并通过NHibernate持久化它们。我知道这不是最好的方法但我通常发现我需要的启动测试数据量不大,而且通过NHibernate插入也是一个测试本身:-)。

I have never used Fitnesse as mentioned by "Mike Scott" but I have heard good things and it does look interesting. It is also recomended in the infamous Billy McCafferty article which gives a nice breif introduction to some great general development best practices such as unit testing, ddd, design-by-contract etc..

我从来没有使用过“麦克斯科特”所提到的Fitnesse,但我听到了很好的东西,看起来确实很有趣。臭名昭着的Billy McCafferty文章也提出了一个很好的通用开发最佳实践,如单元测试,ddd,合同设计等。

#3


0  

In your unit tests, isolate the data access code and abstract it (usually using interfaces, e.g. the repository pattern) so that you don't have any database code in your unit tests. Instead, mock your data access abstractions, using a mocking framework (e.g. Moq). That way, your tests will run fast, which is what you need to ensure you run all your tests often.

在单元测试中,隔离数据访问代码并对其进行抽象(通常使用接口,例如存储库模式),这样您的单元测试中就没有任何数据库代码。相反,使用模拟框架(例如Moq)模拟您的数据访问抽象。这样,您的测试将快速运行,这是您确保经常运行所有测试所需的。

When you need to test the whole system, including the database, you're doing integration testing. For that, take a look at Fitnesse. There's a good book on it.

当您需要测试整个系统(包括数据库)时,您正在进行集成测试。为此,请看看Fitnesse。有一本好书。

#1


0  

The recommended approach is to architect your system that you can test your code without a DB. What this means to a code perspective is using seperation of concerns. You want all of your core business logic seperated from your pages. You can do this using a MVC or MPV pattern, which if you good you can find a great deal about.

建议的方法是构建您的系统,您可以在没有DB的情况下测试代码。对于代码透视,这意味着使用关注点分离。您希望从您的页面中分离出所有核心业务逻辑。你可以使用MVC或MPV模式来做到这一点,如果你很好,你可以找到很多。

Right now there is ASP.Net MVC; however, the MVC pattern has been used in ASP.Net long before this framework existed so if your looking at enhancing an existing web forms app make sure you don't end up looking at the more recent buzz which is about ASP.Net MVC as a programing model in ASP.Net.

现在有ASP.Net MVC;然而,在该框架存在之前很久就已经在ASP.Net中使用了MVC模式,所以如果你想要增强现有的web表单应用程序,请确保你最终不会看到最近关于ASP.Net MVC的嗡嗡声。 ASP.Net中的编程模型。

Now let's say you have your core logic for handling a button click isolated from page so you have a Class let's call it WidgetController. The widget controller might have a method of HandleClick() which performs your business logic.

现在让我们假设您拥有处理从页面隔离的按钮单击的核心逻辑,这样您就可以将它称为WidgetController。窗口小部件控制器可能有一个HandleClick()方法,用于执行业务逻辑。

Let's further assume that your business logic requires data access. You can again use seperation of concerns. Your business logic shouldn't care how to access the DB, all it needs is the data let another class get the data. A popular way of seperating data is by using a Depedancy Injection or Inversion of Control model (DI, IoC respectivley). Essentially what you do is define an interface for your data access, and your controller will program against the interface. You then at runtime provide the actual class to your controller via some method (Property, Constructor etc...)

让我们进一步假设您的业务逻辑需要数据访问。您可以再次使用关注点。您的业​​务逻辑不应该关心如何访问数据库,它所需要的只是数据让另一个类获取数据。一种流行的分离数据的方法是使用Depedancy Injection或Inversion of Control模型(DI,IoC respectivley)。基本上,您所做的是为数据访问定义一个接口,您的控制器将针对该接口进行编程。然后,您在运行时通过某种方法(Property,Constructor等...)向控制器提供实际的类。

What this does is it allows you to provide MOCK implementations during runtime which seperates your DB. The mocks will implement your interface and you can just new up the objects your tests will need store them in memory.

这样做是因为它允许您在运行时提供MOCK实现,从而分离您的数据库。模拟将实现您的界面,您只需要新建您的测试需要将它们存储在内存中的对象。

#2


1  

Actually, as mentioned mocking and using stubs to test system interaction without a database interaction is great, but intergration testing can be just a valid.

实际上,正如所提到的,模拟和使用存根来测试系统交互而没有数据库交互是很好的,但是集成测试可能只是有效的。

What you mention with regards to scripting out the database tables and data, then running a setup script prior to all integration tests, is a fairly sensible approach.

您提到的脚本化数据库表和数据,然后在所有集成测试之前运行安装脚本,是一种相当明智的方法。

Dont know how much of a fan of ORMs you are, but using NHibernate you can rebuild your tables from the mappings. After rebuilding the database I usually just have a script that physically creates the objects and persists them via NHibernate. I know this isnt the best approach but I usually find that the amount of startup test data I need isnt that large, and also inserting via NHibernate is a test itself :-).

不知道你有多少ORM粉丝,但使用NHibernate你可以从映射重建你的表。在重建数据库之后,我通常只有一个物理创建对象的脚本,并通过NHibernate持久化它们。我知道这不是最好的方法但我通常发现我需要的启动测试数据量不大,而且通过NHibernate插入也是一个测试本身:-)。

I have never used Fitnesse as mentioned by "Mike Scott" but I have heard good things and it does look interesting. It is also recomended in the infamous Billy McCafferty article which gives a nice breif introduction to some great general development best practices such as unit testing, ddd, design-by-contract etc..

我从来没有使用过“麦克斯科特”所提到的Fitnesse,但我听到了很好的东西,看起来确实很有趣。臭名昭着的Billy McCafferty文章也提出了一个很好的通用开发最佳实践,如单元测试,ddd,合同设计等。

#3


0  

In your unit tests, isolate the data access code and abstract it (usually using interfaces, e.g. the repository pattern) so that you don't have any database code in your unit tests. Instead, mock your data access abstractions, using a mocking framework (e.g. Moq). That way, your tests will run fast, which is what you need to ensure you run all your tests often.

在单元测试中,隔离数据访问代码并对其进行抽象(通常使用接口,例如存储库模式),这样您的单元测试中就没有任何数据库代码。相反,使用模拟框架(例如Moq)模拟您的数据访问抽象。这样,您的测试将快速运行,这是您确保经常运行所有测试所需的。

When you need to test the whole system, including the database, you're doing integration testing. For that, take a look at Fitnesse. There's a good book on it.

当您需要测试整个系统(包括数据库)时,您正在进行集成测试。为此,请看看Fitnesse。有一本好书。