简体   繁体   English

如何对我的网站进行单元测试?

[英]How do I UnitTest my Website?

Sorry to ask such a simple question. 很抱歉问这样一个简单的问题。 I am a newbie. 我是新手。 I am setting up my website. 我正在建立我的网站。 I try to create a UnitTest Project for it, but I simply cannot not create a reference for it. 我尝试为其创建一个UnitTest项目,但是我根本无法为其创建引用。 Even though I have created a namespace for my class method, Visual Studio doesn't allow me to use the method and to see the namespace at all. 即使为类方法创建了名称空间,Visual Studio也不允许我使用该方法并完全看不到该名称空间。 Is UnitTesting website completely different from UnitTesting normal programs? UnitTesting网站是否与UnitTesting普通程序完全不同? Any reference online available? 在线有参考资料吗? Thank you 谢谢

You might want to consider using the new Fakes support in the Visual Studio 2012. In short, you can detour the call to the web site API and let the call routed to your supplied delegate. 您可能要考虑使用Visual Studio 2012中的新Fakes支持。简而言之,您可以绕过对网站API的调用,然后将调用路由到提供的委托中。 This allows you to isolate testing your code in a modualized fashion. 这使您能够以模块化方式隔离测试代码。

You can find more detail at http://msdn.microsoft.com/en-us/library/hh549175.aspx 您可以在http://msdn.microsoft.com/zh-cn/library/hh549175.aspx中找到更多详细信息

Regards, 问候,

Patrick Tseng 曾志伟

Visual Studio ALM team. Visual Studio ALM团队。

use this article.if u are user mvc use this question.and this stie 使用本文。如果您是用户mvc,请使用问题。

You've probably heard the old adage, "Better, faster, cheaper, pick any two." 您可能已经听过一句老话:“更好,更快,更便宜,选择任何两个。” If you want something good and fast, it isn't going to be cheap, and if you want something fast and cheap, it isn't going to be very good. 如果您想要好又快的东西,那就不会便宜了;如果您想要又快又便宜的东西,那就不会太好了。 Cheaper, faster, and better means that we need to write more code more quickly, right? 便宜,更快,更好意味着我们需要更快地编写更多代码,对吗? If only it were that simple. 像那么简单就好了。 Learning to type faster might satisfy two of the requirements, but it isn't going to make the software you develop any better. 学习打字更快可能会满足两个要求,但这不会使您开发的软件变得更好。 So how do we make software better? 那么我们如何使软件更好呢? What does "better" mean? “更好”是什么意思?

"Better" means producing flexible, maintainable software with a low number of defects; “更好”是指生产具有少量缺陷的灵活,可维护的软件; better software is all about long-term maintainability. 更好的软件就是长期的可维护性。 To achieve this, a key design decision is to ensure that components are loosely coupled. 为此,关键的设计决策是确保组件松耦合。 Loosely coupled software has many benefits. 松耦合的软件有很多好处。 One that stands out is that it improves our ability to test solutions. 一个突出的特点是它提高了我们测试解决方案的能力。 If we write software that can be broken down easily into small parts, it becomes easier to test. 如果我们编写的软件可以很容易地分解为小部分,则测试变得更加容易。 It sounds simple when you word it that way, but the amount of software in use today that is difficult to test or maintain shows that it is not as straightforward as we might like to think. 用这种方式听起来很简单,但是当今正在使用的难以测试或维护的软件数量表明,它并不像我们想的那样简单。 Software needs to be coupled to do anything useful, but developers need tools and techniques to decrease coupling so that solutions are easier to test. 需要对软件进行耦合以执行任何有用的操作,但是开发人员需要工具和技术来减少耦合,以使解决方案易于测试。

I assume that your website code is in one project, and your unit test code is in another project in the same solution? 我假设您的网站代码在同一个解决方案中的一个项目中,而您的单元测试代码在同一个解决方案的另一个项目中? In order for code in one project to access the code of another project, you have to add a reference to the other project: 为了使一个项目中的代码能够访问另一个项目的代码,您必须添加对另一个项目的引用:

  1. Right-click on the unit test project and click 'Add Reference...' 右键单击单元测试项目,然后单击“添加引用...”
  2. Open the 'Projects' tab, select the website project and click 'OK'. 打开“项目”选项卡,选择网站项目,然后单击“确定”。

You should not unit test your website. 您不应该对您的网站进行单元测试。 You should unit test your business layer code. 您应该对业务层代码进行单元测试。 Testing your website would probably be UI testing, you can use tools for this like Selenium. 测试您的网站可能是UI测试,您可以使用诸如Selenium之类的工具。 I would recommend reading up on unit testing, and unit testing frameworks (MSTest, NUnit etc). 我建议阅读单元测试和单元测试框架(MSTest,NUnit等)。 You should layer your application so you can test your classes, objects, data access etc, all in absolute isolation. 您应该对应用程序进行分层,以便可以绝对隔离地测试类,对象,数据访问等。

Example: 例:

A Manage Users page may show an administrator a list of all the users, filtered by a filter criteria. Manage Users页面可以向管理员显示所有用户的列表,并按过滤条件过滤。

The page should only handle the UI side of the problem, as in, it should only be concerned about displaying the data. 该页面仅应处理问题的UI方面,例如,它仅应关注显示数据。 It should send off a request to another object to get a list of users. 它应该向另一个对象发出请求以获取用户列表。 Maybe something like a UserRepository which has a method called GetUsers which takes in a filter criteria. 可能是类似UserRepository东西,它具有一个名为GetUsers的方法,该方法接受过滤条件。

This is where unit testing comes in. You would test (by mocking, perhaps) that when given a very specific list of users in the database, and a certain filter criteria, that the GetUsers method returns the list of users you expect. 这就是进行单元测试的地方。您将测试(也许是通过模拟)当给定数据库中非常特定的用户列表和特定的过滤条件时, GetUsers方法将返回您期望的用户列表。

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

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