简体   繁体   English

为单元测试准备编码的最佳策略

[英]Best strategy to get coding prepared for unit testing

I have a solution that is missing a lot of code coverage. 我有一个缺少大量代码覆盖率的解决方案。 I need to refactor this code to decouple to begin to create unit tests. 我需要重构这个代码来解耦才能开始创建单元测试。 What is the best strategy? 什么是最好的策略? I am first thinking that I should push to decouple business logic from data access from businessobjects to first get some organization and then drill down from there. 我首先想到的是,我应该将业务逻辑从业务对象的数据访问分离到首先得到一些组织,然后从那里向下钻取。 Since many of the classes don't support single responsible principle, it's hard to begin testing them. 由于许多类不支持单一责任原则,因此很难开始测试它们。

Are there are other suggestions or best practices from taking a legacy solution and getting it into shape to be ready for code coverage and unit testing? 是否有其他建议或最佳实践从采用传统解决方案并使其成型为代码覆盖和单元测试做好准备?

One of the most important things to do and best ways to approach in legacy code is defects. 在遗留代码中,最重要的事情之一和最佳方法是缺陷。 It is a process that you will continue to do with any code base that you introduce unit testing to, as well. 这是一个过程,您将继续使用您引入单元测试的任何代码库。 Whenever a defect is reported, write a unit test that will expose the defect. 每当报告缺陷时,编写一个单元测试,以揭示缺陷。 You will quickly find that code that would break on a regular basis (ie "Oh, yay. The plugh() method in the xyzzy class is broken again !) will start breaking less and less. 你会很快发现那些会定期中断的代码(即“哦,yay。在xyzzy类中的plugh()方法再次破坏!)将开始越来越少的破解。

Really, just start doing it. 真的,就这样开始吧。 You aren't going to have tremendous coverage in a legacy application overnight. 您不会在一夜之间在遗留应用程序中获得巨大的覆盖率。 Start by hitting the code that is more prone to breakage, and start branching out. 首先点击更容易破损的代码,然后开始分支。 Make sure that any new development within the code has a higher code coverage, as well. 确保代码中的任何新开发都具有更高的代码覆盖率。

Remember the mantra of TDD is "red/green/refactor", and you might want to look into refactoring tools to help do some of the tedious tasks that go along with it. 记住TDD的口号是“红色/绿色/重构”,你可能想要研究重构工具来帮助完成一些繁琐的任务。 JetBrain's ReSharper is popular, and my personal choice. JetBrain的ReSharper很受欢迎,也是我个人的选择。

Start by creating the tests. 首先创建测试。 Refactor the code as needed to support the tests. 根据需要重构代码以支持测试。

I suggest creating tests for the existing code first, until you have good enough coverage. 我建议首先为现有代码创建测试,直到你有足够好的覆盖率。 You need to test your code as it is to make sure you don't break anything when you refactor. 您需要按原样测试代码,以确保在重构​​时不会破坏任何内容。 Of course, you'll want to do this piece by piece, writing tests for a module, then refactoring it before moving on to the next one. 当然,你会想要一块一块地做,为模块编写测试,然后重构它,然后再转到下一个模块。 Once you have good coverage you can decide if it's worth continuing with the refactoring just to make the code more testable. 一旦你有良好的覆盖范围,你就可以决定是否值得继续进行重构,以使代码更易于测试。 From your description, I suspect it will be. 根据你的描述,我怀疑它会是。

Try to start reading about TDD. 尝试开始阅读有关TDD的内容。

http://www.codeproject.com/KB/dotnet/tdd_in_dotnet.aspx http://www.codeproject.com/KB/dotnet/tdd_in_dotnet.aspx

These are generic guidelines I find useful for unit testing: 这些是我认为对单元测试有用的通用指南:

1) Identify Boundary Objects (Win/WebForms, CustomControls etc). 1)识别边界对象(Win / WebForms,CustomControls等)。

2) Identify Control Objects (Business layer objects) 2)识别控制对象(业务层对象)

3) Write Unit tests only for control objects public methods invoked by boundary objects. 3)仅对边界对象调用的控制对象公共方法进行单元测试。 This way you'll be sure you're covering main functional aspects of your app. 这样您就可以确定自己涵盖了应用的主要功能方面。

In your case, if the business rules are tightly coupled with boundary objects you're in trouble - in my opinion you should try to refactor your stuff focusing on hot spots based on functional requirements of your app. 在您的情况下,如果业务规则与边界对象紧密结合,则您遇到了麻烦 - 在我看来,您应该尝试根据应用程序的功能要求重点关注热点。

The feasibilty of this obviosuly highly depends on the specific case. 这种可行性的可行性在很大程度上取决于具体情况。

您需要按原样测试代码,以确保在重构​​时不会破坏任何内容。

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

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