简体   繁体   中英

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.

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.

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)
    • 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. It uses JUnit for the example and gives a very good introduction how good testing is done.

There you will learn things like the FIRST principle, that Unit Tests should be:

Fast, Independent, Repeatable, Self-Validating and Timly

Some clarifications, JUnit is the most used and accepted test framework itself. Mockito and Powermock are mocking frameworks, they are used together with JUnit when you want to do integration tests .

For code coverage I can only recommend Cobertura , but there are many more .

科贝图拉

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.. ". 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. 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. Note that the frameworks Mockito and Powermock are not testing frameworks themselves, but they can be used within JUnit.

For acceptance tests, there are also a variety of frameworks to help. For web UI testing, Selenium is pretty standard. There are also tools like Fitnesse for more table driven testing.

There are also some common frameworks to help with code coverage - Cobertura, Emma, Clover come to mind.

I would also set up an automated build (Jenkins build server is pretty simple to set up). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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