MsBuild task does not place native C++ output in TargetOutputs ItemGroup - c++

I just migrated a software containing VC++ (native and managed) projects as well as C# projects from .NET 3.5 to 4.0. We build with MsBuild and we now enjoy the benefits of MsBuild 4.0 which now treats C# and C++ projects the same way. There is one issue however I was unable to solve so far.
I am using TargetOutputs ItemGroup to further process the resulting binaries. This works fine for C# and managed C++ (clr support), but native C++ assemblies (No Common Language Runtime Support) are not placed in this ItemGroup even though the projects are built properly.
This is the statement in use, after that #(MyProjectsOutput) does not contain the native C++ assemblies.
<MSBuild Projects="#(MyProjects)" Targets="Rebuild" Properties="Platform=x64;Configuration=Release">
<Output TaskParameter="TargetOutputs" ItemName="MyProjectsOutput" />
</MSBuild>
Any idea what might be the cause? I am pretty stuck here...
Thanks in advance.
Erik

After some more investigation, I created an empty C++ console application and this simple MsBuild script:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Build" Outputs="" Condition="">
<ItemGroup>
<MyProjects Include="test\test.vcxproj">
</MyProjects>
</ItemGroup>
<MSBuild Projects="#(MyProjects)">
<Output TaskParameter="TargetOutputs" ItemName="MyProjectsOutput" />
</MSBuild>
<Message Importance="high" Text="MyProjects: #(MyProjects)"></Message>
<Message Importance="high" Text="MyProjectsOutput: #(MyProjectsOutput)"></Message>
</Target>
</Project>
test.exe was successfully built, but still the ItemGroup #(MyProjectOutput) did not contain the file. When I change test.vcxproj to "Common Language Runtime Support (/clr)" the output is properly displayed. It appears to be a bug in MsBuild.

You can use the GetNativeTargetPath target name on the MSBuild task to get the output file path of a native C++ project:
<MSBuild Projects="my.vcxproj" Targets="GetNativeTargetPath">
<Output ItemName="MyOutput" TaskParameter="TargetOutputs"/>
</MSBuild>

Related

X Unit test not compatible with .net core 2.2

I have web application with .net core 2.2.
I want to test my web application with XUnit or MSUnit but o have problem with it.
The version of the Microsoft.NETCore.App package in XUnit project is 2.1.0 and version of the Microsoft.NETCore.App in api project is 2.2.
I search many hours and do everything that i see but dosent work.
what should i do ?
Just Add
<PackageReference Include="Microsoft.AspNetCore.App"/>
To your test project, rebuild and it shall be fine.
Here's a sample of my .csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Moq" Version="4.13.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
...
</Project>
you can check the issue from this
As you cans see on this link, the same issue was opened on Github and seems like no solution is delivered by XUnit. I am using Nunit with my .NET Core v2.2 project and it works fine. To have look at the library you can check this page out. The syntax is very similar to XUnit and I guess it won't take you so long to update your tests. So, I would suggest you to migrate NUnit.

.NET Core with NUnit test

I'm using .NET Core and am creating a unit test.
To do this, I'm running "dotnet new nunit -n tests" to create a project with the first class to test another project.
The problem is: I can run the simple code created with "dotnet test", but I can't code because I'm receiving 'could not be found NUnit' error.
PS.: I'm using Visual Studio Code (last version).
My .csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
</ItemGroup>
</Project>
The error:
The type or namespace name 'NUnit' could not be found (are you missing a using directive or an assembly reference?)
I ran into this issue too and was perplexed by it. It turns out the problem is with the solution, not the dotnet new nunit project that was created.
The solution is auto-including the sub directory and attempting to build it as source. The directory can be excluded by right clicking the new test project in the solution and selecting "Exclude from Project". Rebuild and the errors go away.
I solved the problem using Visual Studio 2017 (not Visual Studio Code). It is better to architect a Microsoft solution.
EDIT: If you are using Visual Studio (complete version) you can see better the solution, projects and references.
If you are using Visual Studio Code and include new class or files, the reference is not added in you project file (.csproj) automatic. Maybe have another way to do that, but in the moment, I just use the complete version.

How to disable "warning MSB8051: Support for targeting Windows XP is deprecated" in VS2017?

Since about one of the latest updates to Visual Studio 2017 I started getting the following warning during the build of my MFC project:
1>C:\Program Files (x86)\Microsoft Visual
Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\x64\PlatformToolsets\v141_xp\Toolset.targets(39,5):
warning MSB8051: Support for targeting Windows XP is deprecated and
will not be present in future releases of Visual Studio. Please see
https://go.microsoft.com/fwlink/?linkid=2023588 for more information.
How do I disable this warning?
Here's the project configuration:
Add this to your .vcxproj file either into an existing PropertyGroup or one of its own.
<PropertyGroup>
<XPDeprecationWarning>false</XPDeprecationWarning>
</PropertyGroup>
or via command line
msbuild [project file] /p:XPDeprecationWarning=false
Another possibility is to go to your Property Manager window and "Add a new property sheet..." to your project. Right click on the new sheet and select "common Properties"->"User Macros"->"Add Macro" and use the name XPDeprecationWarning and a value of false. Sadly you can't just do this on your project as Visual Studio doesnt allow you to use the GUI to edit UserMacros on the root project file (I have always wondered why as the node is there in the file ).
These should all do exactly the same thing so if one is not working for you then I'm not sure why any of the others would be any more successful.
For those using separate property sheets (found in View --> Other Windows --> Property Manager) to combine properties from multiple projects into a single file, where mine is AllCommon.props, I was able to add replace a null <PropertyGroup /> with
<PropertyGroup>
<XPDeprecationWarning>false</XPDeprecationWarning>
</PropertyGroup>
So the whole property file now looks like:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<XPDeprecationWarning>false</XPDeprecationWarning>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<AdditionalIncludeDirectories>..\MyCommonLibrary</AdditionalIncludeDirectories>
<CallingConvention>StdCall</CallingConvention>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup />
</Project>
This works, and I curiously didn't have to do anything with User Macros. This file has to be edited by hand, as I have not found a way to do it with the GUI.

.NET Standard 1.5 XUnit console runner - How to run my unit tests?

I have a test project configured with .NET Standard 1.5
IDE: Visual Studio 2017 Community
I have included xunit and xunit.runner.console but I cannot run the tests.
I tried dotnet test on the test project but I get the following error
Could not find testhost.dll for source 'D:\Dev\Visual Studio
2017\CSharpSeven.NewFeatures\CSharpSeven.NewFeatures\CSharpSeven.Tests\bin\Debug\netstandard1.5\CSharpSeven.Tests.dll'.
Make sure test project has a nuget reference of package
"microsoft.testplatform.testhost".
My .csproj is the following:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.5</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.TestPlatform.TestHost" Version="15.0.0" />
<PackageReference Include="System.ValueTuple" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.console" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CSharpSeven\CSharpSeven.csproj" />
</ItemGroup>
</Project>
I've been struggling for quite sometime to get the tests running, can anyone please help?
Test projects needs to be runable applications. You need to target a specific runtime. For example:
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
See this answer for more information
Can't get XUnit tests to run with .NET Core
Have you tried to do a restore and build first ?
dotnet restore
dotnet build
This should add any missing packages then you may run the tests.

VS 2017 not discovering tests on .NET Core

can anyone help me get VS2017 to work with .NET Core test projects?
I tried creating both MSTest and xUnit unit test projects for .NET Core from the VS 2017 templates. Neither of them works in the test explorer (not discovered), however running dotnet test from the project folder works fine.
Steps to reproduce:
Create new project in VS 2017
Choose either the Unit Test Project (.NET Core) or xUnit Test Project (.NET Core) template
Implement some arbitrary unit test
Build the solution
Go to the text explorer and try to Run All
At this point the Output window should tell you that 0 test were discovered
.csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
</Project>
If I tried to create a MSTest unit test project from the template that targets .NET Framework (full, not .NET Core), it worked.
Any ideas?
In the end, the problem was solved by changing my system PATH env. variable from C:\Program Files\dotnet\ to C:\Progra~1\dotnet\, as answered in Can't get XUnit tests working with Visual Studio 2017 RC
Thanks to Alexey for suggesting it in the comments :)
I think I experienced same behavior. Try to build your solution so that VS can discover your tests. Otherwise please share you tests csproj file to ensure you reference correct packages. Mine is:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
</Project>
UPDATE: I have played around a bit and it looks like VS cannot find the tests without <OutputType>Exe</OutputType>
UPDATE 2: Try also add following to csproj as I see VS adds in in some cases.
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
I hit this issue with VS 2017 not discovering MSTest unit tests on .NET Core.
The Tests output only reported that it had discovered zero tests.
On running dotnet test MyProject.csproj -t in a command prompt, it advised that I did not have the correct .NET Core runtime installed to match the project's RuntimeFrameworkVersion.
Installing the latest VS 2017 update and .NET Core runtime resolved the issue.
I had the same issue. I resolved it by installing the "Microsoft.NET.Test.Sdk" package from nuget.
I had the same issue after migration from project.json to csproj.
And resolved it by removing net451 target framework and leave only netcoreapp1.1. Now it's works perfect and discover every time.
Of course if you need many framework targets you shouldn't do it, just use CLI to test them...
It may due to inconsistent project settings for Processor Architecture. The Test project target must match the test default Processor Architecture.
Check https://stackoverflow.com/a/47454276/2700303
I also had this issue and it was remedied by ensuring my project and test project had the same target framework version in the .csproj file
<TargetFramework>netcoreapp1.0</TargetFramework>
Also make sure you don't have mixed PackageReference versions in the *.csproj files when having multiple test projects and the same output directory and also update the packages of test project especially MSTest.TestAdapter as well as MSTest.TestFramework