How to deal with confusing usage dependencies to start unit testing?

I have a dirty Delphi 7 legacy system for support and development. I’m already reading Effectively Work with Legacy Code, and I really like this book.

To start following the tips in the book, I created a test project and tried to write one test. To do this, I need to add a part to the test project, but here lies the problem: there are terrible dependencies in the tested system. One device uses another unit that uses some other units and so on and so forth. It seems that most units directly or indirectly use one specific unit, and this unit, in turn, has 170 dependencies in its usage article. There are indirect circular dependencies.

I am currently trying to add all the inherited system units to a test project, but I am facing all types of problems, for example, "unit xxx was compiled with another version of xxx" and others.

So I wonder if I'm not doing something wrong. I used to use unit testing, but in my own projects it was smaller and with better structure and modulation. What are my options in this situation? Did I miss something?

+6
source share
1 answer

There will always be dependencies in your code. Well, if you have code reuse, you will have dependencies. Since you are testing an outdated system, a wholesale restructuring is out of the question.

Therefore, you just need to accept the dependencies. The most convenient and practical approach is a project with one unit testing. This project contains all your unit tests. Use the capabilities of your runner program to run only specific tests at any given time.

This causes your project to have the same list of units in the .dpr file as the main project. This is what you tried now, and this is the right approach.

Your problem is this: you are sharing the DCU directory (module output directory) between the main project and the unit test project. And you have different compiler options for two projects. This is the most likely explanation for the error you are reporting.

There are several obvious solutions:

  • Combine the compiler options for both projects. Then they can share the DCU.
  • You have separate DCU directories for two projects.

Option 2 is much more reliable and is best practice. However, you should try to understand why the compiler options are different. It is possible that your compiler options in the new unit test project will need to be changed so that the modules under test compile and function as desired. In modern Delphi, I would use option sets to ensure consistency of compiler options.

Now there may be other technical problems that you are facing, and my explanation of the error may not be entirely correct, since I need to guess a little. But the bottom line is that having the same list of units in your .dpr files is the way to go.

+4
source

Source: https://habr.com/ru/post/923375/


All Articles