测试驱动开发上的五大错误

发表于:2013-3-15 10:38

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

  我曾经写过很多的糟糕的单元测试程序。很多。但我坚持着写,现在我已经喜欢上了些单元测试。我编写单元测试的速度越来越快,当开发完程序,我现在有更多的信心相信它们能按照设计的预期来运行。我不希望我的程序里有bug,很多次,单元测试在很多弱智的小bug上挽救了我。如果我能这样并带来好处,我相信所有的人都应该写单元测试!

  作为一个自由职业者,我经常有机会能看到各种不同的公司内部是如何做开发工作的,我经常吃惊于如此多的公司仍然没有使用测试驱动开发(TDD)。当我问“为什么”,回答通常是归咎于下面的一个或多个常见的错误做法,这些错误是我在实施驱动测试开发中经常遇到的。这样的错误很容易犯,我也是受害者。我曾合作过的很多公司因为这些错误做法而放弃了测试驱动开发,他们会持有这样一种观点:驱动测试开发“增加了不必要的代码维护量”,或“把时间浪费在写测试上是不值得的”。

  人们会很合理的推断出这样的结论:

  写了单元测试但没有起到任何作用,那还不如不写。

  但根据我的经验,我可以很有信心的说:

  单元测试能让我的开发更有效率,让我的代码更有保障。

  带着这样的认识,下面让我们看看一些我遇到过/犯过的最常见的在测试驱动开发中的错误做法,以及我从中学到的教训。

  1、不使用模拟框架

  我在驱动测试开发上学到第一件事情就是应该在独立的环境中进行测试。这意味着我们需要对测试中所需要的外部依赖条件进行模拟,伪造,或者进行短路,让测试的过程不依赖外部条件。

  假设我们要测试下面这个类中的GetByID方法:

  1. public class ProductService : IProductService  
  2. {  
  3.     private readonly IProductRepository _productRepository;  
  4.      
  5.     public ProductService(IProductRepository productRepository)  
  6.     {  
  7.         this._productRepository = productRepository;  
  8.     }  
  9.      
  10.     public Product GetByID(string id)  
  11.     {  
  12.         Product product =  _productRepository.GetByID(id);  
  13.      
  14.         if (product == null)  
  15.         {  
  16.             throw new ProductNotFoundException();  
  17.         }  
  18.      
  19.         return product;  
  20.     }  
  21. }

  为了让测试能够进行,我们需要写一个IProductRepository的临时模拟代码,这样ProductService.GetByID就能在独立的环境中运行。模拟出的IProductRepository临时接口应该是下面这样:

  1. [TestMethod]  
  2. public void GetProductWithValidIDReturnsProduct()  
  3. {  
  4.     // Arrange 
  5.     IProductRepository productRepository = new StubProductRepository();  
  6.     ProductService productService = new ProductService(productRepository);  
  7.      
  8.     // Act 
  9.     Product product = productService.GetByID("spr-product");  
  10.      
  11.     // Assert 
  12.     Assert.IsNotNull(product);  
  13. }  
  14.      
  15. public class StubProductRepository : IProductRepository  
  16. {  
  17.     public Product GetByID(string id)  
  18.     {  
  19.         return new Product()  
  20.         {  
  21.             ID = "spr-product",  
  22.             Name = "Nice Product" 
  23.         };  
  24.     }  
  25.      
  26.     public IEnumerable<Product> GetProducts()  
  27.     {  
  28.         throw new NotImplementedException();  
  29.     }  
  30. }

61/6123456>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号