C# 自动化单元测试 - 为什么?什么?哪一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1316101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automated Unit testing - why? what? which?
提问by Sandbox
I am a C# winforms developer with an experience of around a year. The only unit testing that I have been doing till now has been manual. I have thinking about following for sometime:
我是一名 C# winforms 开发人员,拥有大约一年的经验。到目前为止,我一直在做的唯一单元测试是手动的。我考虑了一段时间:
- Why do we need to have automated unit testing? How effective is it?
- If I want to start doing automated unit testing. Where should I start from? (have heard about nunit)
- Do I need to keep anything in mind when designing my classes to facilitate automated unit testing?
- Does C# have an in-built support for automated unit testing?
- Can we also test GUI with automated unit testing or is it just business logic?
- Have heard about mocking frameworks. Are they also for unit testing?
- 为什么我们需要自动化单元测试?效果如何?
- 如果我想开始进行自动化单元测试。我应该从哪里开始?(听说过 nunit)
- 在设计我的类以促进自动化单元测试时,我需要记住什么吗?
- C# 是否具有对自动化单元测试的内置支持?
- 我们还可以使用自动化单元测试来测试 GUI 还是仅仅是业务逻辑?
- 听说过模拟框架。它们也用于单元测试吗?
采纳答案by Mark Seemann
Why do we need to have automated unit testing? How effective is it?
为什么我们需要自动化单元测试?效果如何?
Automated unit testing is very valuable first and foremost because it is automatable (normally, we only consider it a 'unit test' when it isautomatable). As the size of an application grows, manually testing the whole application may take hours or even weeks. Even testing just a small part of an application takes time and is error-prone. Unless you are autistic, you will not be able to focus on performing each manual test 100% correctly if you have to do it over and over dozens of times.
自动化单元测试首先非常有价值,因为它是可自动化的(通常,我们只在它可自动化时才将其视为“单元测试” )。随着应用程序规模的增长,手动测试整个应用程序可能需要数小时甚至数周的时间。即使只测试应用程序的一小部分也需要时间并且容易出错。除非您患有自闭症,否则如果您必须一遍又一遍地进行数十次手动测试,您将无法专注于 100% 正确地执行每个手动测试。
In Agile development, we operate with the concept of Rapid Feedback: the sooner you can get feedback about what you did was right or wrong, the more effective you can be. We all make mistakes, but it is a lot less expensive to discover and fix a mistake thirty seconds after it was made, than several days or weeks later. That is why automated testing is so important.
在敏捷开发中,我们采用快速反馈的概念:您越早获得关于您所做的是对或错的反馈,您就可以越有效。我们都会犯错误,但在错误发生 30 秒后发现并修复错误比在几天或几周后花费要少得多。这就是自动化测试如此重要的原因。
While you can do automated testing on many levels, unit testing is the most efficient type to implement. It may be the most difficult testing discipline to understand and master, but it is much more robust than any other type of testing.
虽然您可以在多个级别上进行自动化测试,但单元测试是最有效的实施类型。它可能是最难理解和掌握的测试学科,但它比任何其他类型的测试都要健壮得多。
If I want to start doing automated unit testing. Where should I start from? (have heard about nunit)
如果我想开始进行自动化单元测试。我应该从哪里开始?(听说过 nunit)
First of all, you need to learn some unit testing basics. Roy Osherove's book The Art of Unit Testingis a good introduction.
首先,您需要学习一些单元测试基础知识。Roy Osherove 的书The Art of Unit Testing是一个很好的介绍。
When it comes to frameworks, NUnit has been around for a long time, but it has some inherent issues.
谈到框架,NUnit 已经存在很长时间了,但它有一些固有的问题。
If you already have Visual Studio Professional or Team System, there's a built-in unit testing framework commonly known as MSTest. Most people dislike this framework, but personally, I find it quite adequate. The IDE integration works well, but the API could be better.
如果您已经拥有 Visual Studio Professional 或 Team System,则有一个内置的单元测试框架,通常称为 MSTest。大多数人不喜欢这个框架,但就我个人而言,我觉得它已经足够了。IDE 集成运行良好,但 API 可能会更好。
If you are looking for a free, open source unit testing framework, I would recommend xUnit.net, which is a much more modern framework.
如果您正在寻找一个免费的开源单元测试框架,我会推荐xUnit.net,它是一个更现代的框架。
Do I need to keep anything in mind when designing my classes to facilitate automated unit testing?
在设计我的类以促进自动化单元测试时,我需要记住什么吗?
Yes, each class should be able to be used in isolation. This can be very hard to do by up-front design, or if you are trying to retrofit unit tests onto existing code, but should come more or less naturally if you adopt Test-Driven Development (TDD).
是的,每个类都应该能够单独使用。这可能很难通过预先设计来完成,或者如果您尝试将单元测试改造成现有代码,但如果您采用测试驱动开发 (TDD),则应该或多或少自然而然地做到这一点。
Does C# have an in-built support for automated unit testing?
C# 是否具有对自动化单元测试的内置支持?
No, C# is just a language, but as I mentioned above, certain versions of Visual Studio has MSTest.
不,C# 只是一种语言,但正如我上面提到的,某些版本的 Visual Studio 具有 MSTest。
Can we also test GUI with automated unit testing or is it just business logic?
我们还可以使用自动化单元测试来测试 GUI 还是仅仅是业务逻辑?
Unit testing GUI tends to be very brittle (that is, test maintainance is very high), so is generally not a good idea.
单元测试 GUI 往往非常脆弱(即测试维护性非常高),因此通常不是一个好主意。
However, there are many design patterns that can help you extract all GUI logic into testable classes: Modev-View-Controller, Model-View-Presenter, Application Controller, Model-View-ViewModel, etc.
但是,有许多设计模式可以帮助您将所有 GUI 逻辑提取到可测试的类中:Modev-View-Controller、Model-View-Presenter、Application Controller、Model-View-ViewModel 等。
You can perform automated tests of an entire application via such interfaces, bypassing only the GUI rendering part. Such tests are called Subcutaneous Tests, but are considered Integration Tests, not unit tests.
您可以通过这样的接口对整个应用程序执行自动化测试,仅绕过 GUI 渲染部分。此类测试称为皮下测试,但被视为集成测试,而不是单元测试。
Have heard about mocking frameworks. Are they also for unit testing?
听说过模拟框架。它们也用于单元测试吗?
Dynamic mock libraries are onlyfor unit testing.
动态模拟库仅用于单元测试。
Some good and popular ones are
一些好的和受欢迎的是
回答by Ned Batchelder
The why for automated unit testing is simple: because as your code grows, it will take longer and longer to do manual testing, which means you are less and less likely to do it.
自动化单元测试的原因很简单:因为随着代码的增长,进行手动测试所需的时间会越来越长,这意味着您越来越不可能这样做。
By automating the tests, you make them convenient to run, so they will be run.
通过自动化测试,您可以方便地运行它们,因此它们将被运行。
回答by Preet Sangha
- Why do we need to have automated unit testing? How effective is it?
- 为什么我们需要自动化单元测试?效果如何?
Very.
非常。
If I want to start doing automated unit testing. Where should I start from? (have heard about nunit) Start with nunit - its very easy
Do I need to keep anything in mind when designing my classes to facilitate automated unit testing? If you get good at unit test then you'll want to.
Does C# have an in-built support for automated unit testing? No - but Visual studio does - but I don't recommend using it
Can we also test GUI with automated unit testing or is it just business logic? Anything can be automated, its just question of how hard it is to do.
Have heard about mocking frameworks. Are they also for unit testing? Yes.
如果我想开始进行自动化单元测试。我应该从哪里开始?(听说过 nunit)从 nunit 开始 - 它很容易
在设计我的类以促进自动化单元测试时,我需要记住什么吗?如果你擅长单元测试,那么你会想要的。
C# 是否具有对自动化单元测试的内置支持?不 - 但 Visual Studio 可以 - 但我不建议使用它
我们还可以使用自动化单元测试来测试 GUI 还是仅仅是业务逻辑?任何事情都可以自动化,问题只是做起来有多难。
听说过模拟框架。它们也用于单元测试吗?是的。
回答by PVitt
Point 1) It gives you a continuous overview of whats working and what not. As soon as you change a simple function you have the result if this change breakes anything. Furthermore you can later on refactor your whole application and as far as your unit test are all going green, everything works fine. Just this point is a very important one. Or have you ever done a project that had never to be refactored?
第 1 点)它为您提供了有效和无效的连续概述。一旦你改变了一个简单的函数,如果这个改变破坏了任何东西,你就会得到结果。此外,您可以稍后重构整个应用程序,只要您的单元测试都变绿了,一切正常。只是这一点是非常重要的一点。或者你曾经做过一个永远不需要重构的项目吗?
Point 2) NUnit is a good place to start, but there are several other unit test frameworks. Also i suggest to have a look at CruiseControl or other continuous integration tools that take much work off of the developer.
要点 2) NUnit 是一个很好的起点,但还有其他几个单元测试框架。此外,我建议查看 CruiseControl 或其他持续集成工具,它们可以减少开发人员的大量工作。
Point 3) Well, there are for sure people who can tell you hours and hours what to do and what not. As far as my unit test background is limited to use it for embedded devices we have quite other dificulties.
第 3 点)嗯,肯定有人会告诉你几个小时该做什么,不该做什么。至于我的单元测试背景仅限于将它用于嵌入式设备,我们还有很多其他困难。
Point 4) C# is a programming language, not a testing framework. .Net has at least one Attribute that can help during unit tests, that is the InternalsVisibleTo-Attribute to make your internal classes and methods public for one spezific type.
第 4 点)C# 是一种编程语言,而不是测试框架。.Net 至少有一个可以在单元测试期间提供帮助的属性,即 InternalsVisibleTo-Attribute,用于使您的内部类和方法为一种特定类型公开。
Point 5) If you have a well designed application you dont have to test your gui. Think about this: your gui has no code. Every button a user can press is mapped to controller. Every input a user can provide is handed to the controller. So what do you have to test in your gui? All you have to do is to get your controller tested and working well. And with this MVVM/MVC/WhatElseArchitecture you have one the one hand a well designed application and on the other hand an application that is simple to test.
第 5 点)如果您有一个设计良好的应用程序,您就不必测试您的 gui。想一想:您的 gui 没有代码。用户可以按下的每个按钮都映射到控制器。用户可以提供的每个输入都交给控制器。那么你必须在你的 gui 中测试什么?您所要做的就是让您的控制器测试并运行良好。使用这个 MVVM/MVC/WhatElseArchitecture,您一方面拥有一个设计良好的应用程序,另一方面拥有一个易于测试的应用程序。
回答by Kevin LaBranche
- Why do we need to have automated unit testing? How effective is it?
- 为什么我们需要自动化单元测试?效果如何?
To isolate parts of a program and show they are "correct". They are a contract if you will of how code is suppose to run. With this at hand they can tell you quickly and often if the code is acting correctly.
隔离程序的各个部分并显示它们是“正确的”。如果您愿意了解代码的运行方式,它们就是合同。有了这个,他们可以快速且经常地告诉您代码是否正确运行。
Unit tests enable programmers to change code more easily, with more confidence and less side effects. It promotes / enables refactoring of code which without unit tests can be dangerous. With all this in mind I find unit testing will make your code better.
单元测试使程序员能够更轻松、更有信心地更改代码并减少副作用。它促进/启用代码重构,没有单元测试可能是危险的。考虑到所有这些,我发现单元测试将使您的代码更好。
I find them effective but will admit that this is part of the learning curve. Until you become really good at it (and I am still a beginner in my mind) you will often miss things but I have found it is still way better than the manual testing I have done for way too long. I found almost instant benefit in unit testing. On my first project with it I discovered that I was saving a large amount of time since I didn't have to manually test near as much and I was actually pleasantly surprised more than once when code I was working on caused tests to fail elsewhere that I wouldn't even had thought to retest manually.
我发现它们很有效,但我承认这是学习曲线的一部分。在你真正擅长它之前(在我看来,我仍然是一个初学者)你经常会错过一些东西,但我发现它仍然比我做了太长时间的手动测试要好得多。我发现单元测试几乎立竿见影。在我使用它的第一个项目中,我发现我节省了大量时间,因为我不必手动测试那么多我什至没想过要手动重新测试。
- If I want to start doing automated unit testing. Where should I start from? (have heard about nunit)
- 如果我想开始进行自动化单元测试。我应该从哪里开始?(听说过 nunit)
Nunit is good, MbUnit is good. To get started, read and do the excercises in the readings. Websites and blogs on unit testing, test driven development (TDD), and Refactoring. Object Mentor has a nice series on TDD. Then at some point pick some code you have and try it.
Nunit 很好,MbUnit 很好。首先,阅读阅读材料中的练习并进行练习。有关单元测试、测试驱动开发 (TDD) 和重构的网站和博客。Object Mentor 有一个关于 TDD 的不错的系列。然后在某个时候选择一些您拥有的代码并尝试它。
Books I suggest - Test Driven Development by example, Test Driven Development, A practical guide, The Art of Unit Testing, Refactoring (Martin Fowler), Refactoring Workbook, Refactoring to patterns, Clean Code, and I am sure there are others.
我推荐的书籍 - 示例测试驱动开发、测试驱动开发、实用指南、单元测试艺术、重构(Martin Fowler)、重构工作手册、重构模式、干净代码,我相信还有其他书籍。
I have refactoring books in the list because I have found the techniques to refactor work hand an hand with unit testing.
我在列表中列出了重构书籍,因为我已经找到了重构技术与单元测试相辅相成的技术。
- Do I need to keep anything in mind when designing my classes to facilitate automated unit testing?
- 在设计我的类以促进自动化单元测试时,我需要记住什么吗?
Many of the books I mentioned will discuss this. The answer is yes and no. Often a better designed class that has less dependencies is easier to test. This is just good design but it happens to make testing easier. There is a bit of a debate over if you should change code just to make it more testable. I lean towards you should. At this point for me, my code in the past didn't incorporate a lot of "good design" ideas so for me embarking on this TDD journey is increasing my awareness and level of good design in my code which is in part to wanting to do TDD.
我提到的许多书都会讨论这个问题。答案是肯定的和否定的。通常,一个设计得更好、依赖更少的类更容易测试。这只是一个很好的设计,但它恰好使测试更容易。关于是否应该更改代码只是为了使其更易于测试存在一些争论。我应该倾向于你。在这一点上,我过去的代码并没有包含很多“优秀设计”的想法,所以对我来说,开始这个 TDD 之旅是在提高我的代码中优秀设计的意识和水平,部分原因是想要做TDD。
- Does C# have an in-built support for automated unit testing?
- C# 是否具有对自动化单元测试的内置支持?
If you have VS 2008 Pro, yes, or Team system but naturally I assume you don't.
如果你有 VS 2008 Pro,是的,或者 Team 系统,但我自然地认为你没有。
- Can we also test GUI with automated unit testing or is it just business logic?
- 我们还可以使用自动化单元测试来测试 GUI 还是仅仅是业务逻辑?
Yes, there are several tools out there. Watin is one that comes to mind.
是的,那里有几种工具。Watin 就是其中之一。
- Have heard about mocking frameworks. Are they also for unit testing?
- 听说过模拟框架。它们也用于单元测试吗?
Yes. They allow for simulating/testing very complex objectsthat you can't easily unit test.
是的。它们允许模拟/测试您无法轻松进行单元测试的非常复杂的对象。
In my journey down the TDD path, I purposely ignored mocking in the beginning. For me, I wanted to have a bit of a better understanding of why they are needed and how they would be used by living without it and seeing for myself as I coded where I would get stuck with the traditional TDD techniques.
在我沿着 TDD 路径的旅程中,一开始我故意忽略了嘲讽。对我来说,我想更好地理解为什么需要它们,以及如何在没有它的情况下使用它们,并亲眼看看我在编码时会遇到传统 TDD 技术的地方。
Another book I have found of great use, although it hurts my brain to read is Working Effectively with Legacy Code. It really shows you how seemingly innocent code to the untrained can make life really hard down the line.
我发现另一本书很有用,尽管读起来会伤脑筋,但它是《有效地使用遗留代码》(Working Effectively with Legacy Code)。它确实向您展示了未经训练的看似无辜的代码如何使生活变得非常艰难。
回答by IAmMilinPatel
Why do we need to have automated unit testing? How effective is it?
You don't. It is up to you to decide if you want to or not. It will only be as effective as you make it. You should go for automation only if your going to repeat testing cycle for a piece of code more than 3 times!!! If I want to start doing automated unit testing. Where should I start from? (have heard about nunit)
Whatever feels comfortable, make it your start point and then explore it and use it to your advantage. Can we also test GUI with automated unit testing or is it just business logic?
You can but you should not. GUI is better tested manually. Eyes will catch more errors in the interface than a machine will. Have heard about mocking frameworks. Are they also for unit testing?
Yes most of the frameworks like TestNG, Junit etc are for Unit Testing.
你没有。是否愿意由您决定。它只会和你做的一样有效。只有当您要为一段代码重复 3 次以上的测试周期时,才应该选择自动化!!!如果我想开始进行自动化单元测试。我应该从哪里开始?(听说过 nunit)
无论感觉如何,都将其作为起点,然后探索它并利用它来发挥自己的优势。我们还可以使用自动化单元测试来测试 GUI 还是仅仅是业务逻辑?
你可以,但你不应该。GUI 最好手动测试。眼睛会比机器捕捉到更多的界面错误。听说过模拟框架。它们也用于单元测试吗?
是的,大多数框架如 TestNG、Junit 等都是用于单元测试的。