How to share units between projects in the same project group?

Viewed 972

I am learning TDD in Delphi and I have a project group in which there are two projects: MyProject and MyProjectsTests. The last one will be responsible for the storage of all unit tests and it is using DUnitX's project template. Below is the current structure of my project group:

\MyProjectGroup
   \MyProject
      \Classes
         MyClass.pas
   \MyProjectTests
      \Tests
         MyClassTest.pas

In MyClassTest unit, how can I use MyClass unit from MyProject? While using CTRL+SHIFT+A hotkey, my IDE finds the unit and adds it to the uses clauses, but MyProjectTests won't compile and reports

Unit 'MyClass' not found

Obviously I need to import it to MyProjectTests, but how?

2 Answers

While adjusting the search path of MyProjectTests is a valid approach, I prefer to add the unit in question to the project directly. Either use the "Add Unit" command for that or simply drag'n'drop the unit from one project to the other.

I don't advise sharing units between projects.

If multiple projects need to use the same unit, then put that unit in a third project. That third project should be a package project. Add that project's output directory to the library paths of the other two projects. They should now be able to use that unit as normal. You can choose whether you wish to build your other projects with run-time packages.

That third project should be the only project responsible for compiling that unit. To ensure that, don't allow the unit's directory to appear in the library path or search path of any other project. No other project should have access to the source code while compiling. (Go ahead and include the source directory in their browsing paths so that you can still Ctrl+Click or step through the source in the debugger.) Set the package's output directory to be distinct from the source directory, and then add that output directory to other projects' library paths.

Related