简体   繁体   English

从零开始对未经测试的代码执行单元和集成测试的方法

[英]Approach to perform unit and integration tests from Scratch for untested code

The basic question is " How should one start with writing unit and integration testing for a untested project? Especially considering the fact that the person is not familiar with the code and has not done integration testing before. " 一个基本的问题是“ 一个人应该如何从一个未经测试的项目开始编写单元和进行集成测试?特别是考虑到人们对该代码不熟悉并且之前没有进行过集成测试的事实。

Consider the scenario where unit tests and integration tests have to be written for a project. 考虑必须为项目编写单元测试和集成测试的场景。 The project uses Java/J2EE technology does not have any tests written at all. 使用Java / J2EE技术​​的项目根本没有编写任何测试。

The dilemma that I face is since I have not written the code, I don't want to refactor the code immediately to write tests. 我面临的困境是因为我没有编写代码,所以我不想立即重构代码以编写测试。 I also have to select a testing framework. 我还必须选择一个测试框架。 I am thinking of using Mockito and Powermock. 我正在考虑使用Mockito和Powermock。

I also have to estimate code coverage for the tests. 我还必须估计测试的代码覆盖率。 And then perform integration testing. 然后执行集成测试。 I will have to research on integration testing tools and select one. 我将不得不研究集成测试工具并选择其中一种。 I have not done any integration testing or estimated acceptable level of code coverage for a project before. 之前我没有做过任何集成测试或估计的项目代码接受水平。

Since I am working independently, if there are some strategies, tips, suggestions on what should I start with and tools that one can recommend, I will appreciate it. 由于我是独立工作,因此如果我有一些策略,技巧,建议以及应该推荐的工具,我将不胜感激。

First comes first: 先到先得:

  1. Understand the architecture, what are the main components? 了解架构,主要成分是什么?
  2. If you have no good overview of the features and functions the program offers, make a list of them and create a hierarchy of them 如果您对该程序提供的功能没有很好的了解,请列出它们并创建它们的层次结构
  3. Get familiar with the code, I recommend the following approach: 熟悉代码后,我建议采用以下方法:
    • after you understand where the code of its different components are started, try to figure out the method invocation hierarchy (in Eclipse you can easily jump the source code definitions by pressing F3) 在了解了其不同组件的代码从何处开始之后,请尝试弄清楚方法调用的层次结构(在Eclipse中,您可以通过按F3轻松跳转源代码定义)
    • later you can do the same, while debugging the code, this way it will jump automatically to the definitions, plus you can observe how the state of the program changes 稍后,您可以在调试代码时执行相同的操作,这样它将自动跳至定义,此外您还可以观察程序状态的变化

For Unit Testing itself, I can recommend Clean Code Chapter 9 (circa 12 pages ) for starters. 对于单元测试本身,我可以向初学者推荐清洁代码第9章(约12页 )。 It uses JUnit for the example and gives a very good introduction how good testing is done. 它使用JUnit作为示例,并且很好地介绍了如何进行良好的测试。

There you will learn things like the FIRST principle, that Unit Tests should be: 在这里,您将学习诸如FIRST原则之类的东西,即单元测试应为:

Fast, Independent, Repeatable, Self-Validating and Timly 快速,独立,可重复,自我验证且及时

Some clarifications, JUnit is the most used and accepted test framework itself. 需要澄清的是,JUnit是最常用和公认的测试框架本身。 Mockito and Powermock are mocking frameworks, they are used together with JUnit when you want to do integration tests . Mockito和Powermock是模拟框架,当您要进行集成测试时,它们与JUnit一起使用。

For code coverage I can only recommend Cobertura , but there are many more . 对于代码覆盖率,我只能推荐Cobertura ,但还有更多内容

科贝图拉

Start with unit tests before you dive into integration tests (bottom-up), you can also do it the other way around (top-bottom), but since you say you are not so much experienced I would stay stick to the first. 从单元测试开始,然后再进行集成测试(自下而上),也可以采用其他方法(自下而上),但是由于您说的没有那么多经验,我将坚持第一个。

Finally, just go for it and get started. 最后,就去开始吧。 You will learn the most and fastest while actually writing the test code. 在实际编写测试代码时,您将学到最多,最快的东西。

  • Stop. 停止。 " ..not familiar with the code.. ". ..not familiar with the code.. First get familiar with the code and most importantly its expected functionality. 首先熟悉代码,最重要的是熟悉其预期功能。 You can't refactor or unit test a code that you are not comfortable with. 您不能重构或单元测试您不满意的代码。
  • Since you have not done unit-tests before, I would suggest learning and getting convenient with unit-tests . 由于您之前没有进行过单元测试,因此建议您学习并方便使用单元测试

Important: Bad/Wrong unit-tests are worse than no unit-tests. 重要提示:错误/错误的单元测试比没有单元测试更糟糕。 This is because the next guy who will maintain your code will misinterpret the functionality. 这是因为下一个将维护您的代码的人将误解该功能。

Adding tests to legacy code that has no tests is a difficult task. 向没有测试的旧代码中添加测试是一项艰巨的任务。 As @Suraj has mentioned, get familiar with the code base and the expected functionality. 正如@Suraj所提到的,熟悉代码库和预期的功能。 You can't test it if you don't know what it is supposed to do. 如果您不知道它应该做什么,就无法测试它。

In terms of choosing which areas of the code to test. 在选择要测试的代码区域方面。 Start with the high business value areas. 从高商业价值领域开始。 Which functionality is most important? 哪个功能最重要? You want to make sure you have a strong test set for that code. 您想确保您对该代码有一个强大的测试集。

Since you don't have any unit/integration tests, I would start with some high level end to end tests that at least ensure that given some inputs to the system you get some expected outputs. 由于您没有任何单元/集成测试,因此我将从一些高级的端到端测试开始,这些测试至少可以确保在给系统一些输入的情况下,您可以获得预期的输出。 This doesn't ensure correctness but at least ensures consistency. 这不能确保正确性,但至少可以确保一致性。

Then as you develop a test suite you can be confident that the refactorings you are doing are not changing the behavior of the code (unless you find bugs of course that are being fixed). 然后,在开发测试套件时,您可以确信正在执行的重构不会改变代码的行为(除非您发现了已修复的错误)。

For testing frameworks, JUnit is the standard unit testing framework. 对于测试框架,JUnit是标准的单元测试框架。 Note that the frameworks Mockito and Powermock are not testing frameworks themselves, but they can be used within JUnit. 注意,框架Mockito和Powermock本身并不是测试框架,但是可以在JUnit中使用它们。

For acceptance tests, there are also a variety of frameworks to help. 对于验收测试,还有各种框架可以提供帮助。 For web UI testing, Selenium is pretty standard. 对于Web UI测试,Selenium是非常标准的。 There are also tools like Fitnesse for more table driven testing. 还有诸如Fitnesse之类的工具,可以进行更多的表驱动测试。

There are also some common frameworks to help with code coverage - Cobertura, Emma, Clover come to mind. 还有一些通用框架可帮助覆盖代码-我想到了Cobertura,Emma和Clover。

I would also set up an automated build (Jenkins build server is pretty simple to set up). 我还将设置一个自动构建(Jenkins构建服务器的安装非常简单)。 This will allow you to run your tests on every checkin. 这将允许您在每个签入中运行测试。 Even though your code coverage is going to be low to start, getting in this habit is a good one. 即使您的代码覆盖范围开始时很少,但养成这种习惯也是一个好习惯。

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

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