自动化单元测试 - 为什么?什么?哪一个? [英] Automated Unit testing - why? what? which?

查看:136
本文介绍了自动化单元测试 - 为什么?什么?哪一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个C#的WinForms开发人员一年左右的经验。我一直在做,直至现在唯一的单元测试已经手册。
我还想着下了一段:

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:


  • 为什么我们需要有自动化单元
    测试?效果如何?

  • 如果我要开始做自动
    单元测试。我应该从哪里开始从
    ? (听说NUnit的)

  • 请我需要保持什么记
    设计我班
    时,便于自动化单元测试?

  • C#有对
    自动化单元测试的内置支持?

  • 可我们也测试与自动化
    单元测试GUI或只是业务
    逻辑?

  • 听说过嘲讽的框架。
    是他们也进行单元测试?

  • 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?

推荐答案

为什么我们需要有自动化单元测试?效果如何?

自动化单元测试是第一次非常有价值和最重要的,因为它是自动化的(通常情况下,我们只认为这是一个单元测试时,它的的自动化的)。作为应用程序的大小增长,手动测试整个应用程序可能需要数小时或甚至数周。即使测试只是一个应用程序的一小部分需要时间和容易出错。除非你是自闭症,你将不能够专注于100%正确地执行每个手动测试,如果你要一遍又一遍几十次做到这一点。

Automated unit testing is very valuable first and foremost because it is automatable (normally, we only consider it a 'unit test' when it is automatable). 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.

在敏捷发展,我们与快速反馈的概念进行操作:越早,你可以获取你的所作所为是正确的还是错误的,你可以更有效的反馈。我们都会犯错,但它是少了很多昂贵的发现和解决它作出30秒后,不是几天或几周后一个错误。这就是为什么自动化测试是非常重要的。

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.

虽然你可以在许多层面上做自动化测试,单元测试,是落实最有效的类型。这可能是最困难的测试学科的理解和掌握,但它比任何其他类型的测试更健壮。

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.

如果我要开始做自动化单元测试。我应该在哪里开始? (听说NUnit的)

首先,你需要学习一些单元测试基础知识。罗伊Osherove的书单元测试的艺术是一个很好的介绍。

First of all, you need to learn some unit testing basics. Roy Osherove's book The Art of Unit Testing is a good introduction.

当说到框架,NUnit的已经存在了很长一段时间,但它有一些固有的问题。

When it comes to frameworks, NUnit has been around for a long time, but it has some inherent issues.

如果你已经有Visual Studio专业或团队系统,还有通常被称为MSTest的内置单元测试框架。大多数人不喜欢这个框架,但就个人而言,我觉得相当充足。该IDE集成效果很好,但是API可能会更好。

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.

如果你正在寻找一个免费,开源的单元测试框架,我会推荐的xUnit.net ,这是一个更现代的框架。

If you are looking for a free, open source unit testing framework, I would recommend xUnit.net, which is a much more modern framework.

我需要我的设计类时便于自动化单元测试,以保持什么想法?

是的,每班应能单独使用。这可能是很难通过前期的设计做的,或者如果你想改造单元测试到现有的代码,而应该来,如果你采用测试驱动开发(TDD)或多或少自然。

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).

C#有自动化单元测试的内置支持?

没有,C#仅仅是一个语言,但正如我上面提到的,Visual Studio中的某些版本已MSTest的。

No, C# is just a language, but as I mentioned above, certain versions of Visual Studio has MSTest.

可我们也测试与自动化单元测试GUI或只是业务逻辑?

单元测试GUI往往是非常脆(即,测试修的非常高),所以一般不是一个好主意。

Unit testing GUI tends to be very brittle (that is, test maintainance is very high), so is generally not a good idea.

不过,也有很多的设计模式,可以帮助您提取所有GUI逻辑到可测试类:Modev - 视图 - 控制器模型 - 视图 - 演示,应用控制器,模型 - 视图 - 视图模型等等。

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渲染的部分。这种测试被称为皮下测试,但被认为是集成测试,而不是单元测试。

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.

听说过嘲讽的框架。难道他们也为单元测试?

动态模拟库的只有的进行单元测试。

Dynamic mock libraries are only for unit testing.

有一些很好的和流行的是

Some good and popular ones are

  • Moq
  • Rhino Mocks

这篇关于自动化单元测试 - 为什么?什么?哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆