How to determine if an existing class can be tested?

I recently claimed responsibility for some C ++ code. I am going to save this code and add new features later. I know that many people say that it’s usually not worth adding unit tests to existing code, but I would still like to add some tests that at least partially cover the code. In particular, I would like to add tests that reproduce the errors that I fixed.

Some classes are built with some rather complex state, which makes unit testing difficult.

I also want to reorganize the code to make testing easier.

Is there any good article that you recommend in guidelines that help identify classes that are easier to test? Do you have any tips?

+8
c ++ unit-testing
source share
6 answers

While Martin Fowler's refactoring book is a treasure trove of information, why not take a look at Effectively Work with Outdated Code .

In addition, if you are going to deal with classes where there are a ton of global variables or a huge number of state transitions, I would put a lot of integration checks. Separate most of the code that interacts with the code you are refactoring to ensure that all expected inputs in the order in which they are received continue to produce the same results. This is very important, since it is very easy to “fix” a subtle error that could be addressed elsewhere.

Take notes. If you find that there is an error that another function / class expects and processes correctly, you will want to change both at the same time. This is difficult if you do not keep detailed records.

+6
source share

Presumably, the code was written for some purpose, and the unit test will check if the task is running, i.e. pre-conditions and post-conditions are retained for these methods.

If the methods of the public class are such that you can externally check the state, which can be easily checked by the module (black box check). If the class state is invisible or you need to test complex private methods, your test class may need to be a friend (white box).

A class that cannot be unit test will be such that

  • It has huge dependencies, i.e. closely related
  • Designed to work in a multi-threaded or multi-threaded environment. There you would use a system test, not a unit test, and the actual output could not be fully determined.
+1
source share

I wrote quite a few blog posts about unit testing, non-trivial C ++ code: http://www.lenholgate.com/blog/2004/05/practical-testing.html

I also wrote a lot about adding tests to existing code: http://www.lenholgate.com/blog/testing/

+1
source share

Almost everything can and should be checked on the module. If not directly, then using mock classes.

Since you decide to reorganize your classes, try using the BDD or TDD approach.

To prevent breaking existing functionality, the only way is to have good integration tests, but it usually takes time to complete them for a complex system.

Without additional information about what you are doing, it is not easy to give more implementation details. Some of them:

  • use MVP or presenter first for gui development
  • use appropriate design patterns
  • use member functions and pointers or an observer design pattern to break dependencies
0
source share

I think that if you need to come up with some kind of "measure" to check if the class is checked, you are already fscked. You should be able to say just by looking at this: can you write an independent program that refers only to this class and makes sure that it works?

If the class is too large, so you cannot be sure just by looking at it ... most likely it is not being tested. People who don’t know how to make small, different interfaces generally don’t know how to adhere to any other principle.

In the end, however, in order to find out if a class is checked, you need to try to put it in a harness. If you need to do half of your program to do this, try refactoring. If you find that you can’t even perform the most basic refactoring without rewriting the entire program, analyze the costs of this.

0
source share

We published a document in the IPL. He is testing Jim, but not in the way we know him , which explores the practical problems of C ++ testing and offers some methods to solve them that may be useful given your question. These methods are also well supported in Cantata ++, our C / C ++ tool and integration testing.

0
source share

All Articles