Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reference a project without linking to it in a csproj?

I have a test project that runs a program (a .exe) and ensures that the results come out as expected.

I'd like to declare that the test project depends on the project that builds the .exe, and place the .exe drop into a subdirectory of $(OutDir) (or $(OutputPath)).

  • $(OutDir)MyTests.dll (test project)
  • $(OutDir)\Product\Foo.exe (tested project)
  • $(OutDir)\Product\xxx.dll (dependencies of the tested project)

This way I can write a test of the form:

[TestClass]
[DeploymentItem("Product", "Product")]
public class RunFoo
{
    [TestMethod]
    public void HelpTextMatches()
    {
        // System.Diagnostics.Process.Start("Product\foo.exe" ...)
    }
}

Is there a way to declare something like a <ProjectReference in my test .csproj file which will allow me to achieve this?

(I don't want to just have a <Copy task grabbing the outputs of the program under test because then there's nothing to force MSBuild to build the program under test before the test project.)

like image 250
Billy ONeal Avatar asked Apr 11 '14 02:04

Billy ONeal


People also ask

How do I add a project reference in Csproj?

In this article If you see a Dependencies node in Solution Explorer, you can use the right-click context menu to choose Add Project Reference. You can also right-click the project node and select Add > Project Reference.

How do you reference a shared project?

Referencing Shared Projects You add references to a shared project just as you would a normal project reference. In Visual Studio or Fire, you right-click the "References" node of the real project and choose "Add Reference", and then pick the shared project from the list.

What is the difference between project reference and DLL reference?

Well, project references are helpful when you are building and testing in both debug and release mode. If you directly add a DLL then you are locked into whatever that particular DLL was built as. The project reference allows this to be a build time decision. This is correct what you are saying.


2 Answers

I'd like to declare that the test project depends on the project that builds the .exe...

Set ReferenceOutputAssembly to false on the project reference (source):

<ProjectReference Include="..\ProjectA\ProjectA.csproj">
  <Project>{b402782f-de0a-41fa-b364-60612a786fb2}</Project>
  <Name>ProjectA</Name>
  <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>

...and place the .exe drop into a subdirectory of $(OutDir) (or $(OutputPath))

Post-build action. Or just run the exe under test in-place.

like image 159
Ron MacNeil Avatar answered Nov 14 '22 22:11

Ron MacNeil


I don't quite understand your reasons for not using ProjectReference, but this essentially does what adding ProjectReference does except without the added assembly references.

<ItemGroup>
    <ExtraDependencies include="ExeProject.csproj" />
</ItemGroup>

<Target Name="GetExtraDependenciesTargets">
    <MSBuild Projects="@(ExtraDependencies)" Targets="GetTargetPath">
        <Output TaskParameter="TargetOutputs" ItemName="ResolvedDependencyTargets" />
    </MSBuild>
</Target>

<Target Name="BuildExtraDependencies"
        BeforeTargets="BeforeBuild"
        DependsOnTargets="GetExtraDependenciesTargets"
        Inputs="@(ExtraDependencies)"
        Outputs="@(ResolvedDependencyTargets)">
    <MSBuild Projects="@(ExtraDependencies)" Targets="Build">
        <Output TaskParameter="TargetOutputs" ItemName="BuiltDependencyTargets" />
    </MSBuild>
</Target>

Alternatively, you could keep the ProjectReference but just hijack the build process right after the ResolveProjectReferences target and remove the exe as a dependency (from _ResolvedProjectReferencePaths).

like image 28
makhdumi Avatar answered Nov 14 '22 21:11

makhdumi