VS 2017 not discovering tests on .NET Core - unit-testing

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

Related

Import targets during build process with MSBuild

I am using vcpkg as package manager with Visual Studio C++ project. I have created initial target to basically set up vcpkg (clone vcpkg repo and download vcpkg.exe):
<Target Name="EnsureVcpkgBuild" BeforeTargets="PrepareForBuild">
<Exec Command="git clone $(VcpkgRepo).git ..\packages\vcpkg" Condition="!Exists('..\packages\vcpkg\.git')" ContinueOnError="true" />
<Exec Command="git --git-dir=..\packages\vcpkg\.git --work-tree=..\packages\vcpkg pull --ff-only" Condition="Exists('..\packages\vcpkg\.git')" ContinueOnError="true" />
<Exec Command="..\packages\vcpkg\bootstrap-vcpkg.bat" Condition="!Exists('..\packages\vcpkg\vcpkg.exe')" ContinueOnError="true" />
<PropertyGroup>
<ErrorText>This project uses vcpkg that is missing on this computer. Manually download from $(VcpkgRepo) and place the contents in {0}. Or install git and add it to your path, for more information see https://git-scm.com. The missing file is {1}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\vcpkg\vcpkg.exe')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\vcpkg\', '..\packages\vcpkg\vcpkg.exe'))" />
<Error Condition="!Exists('..\packages\vcpkg\scripts\buildsystems\msbuild\vcpkg.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\vcpkg\', '..\packages\vcpkg\scripts\buildsystems\msbuild\vcpkg.targets'))" />
</Target>
In my project I am importing the vcpkg targets file:
<ImportGroup>
<Import Project="..\packages\vcpkg\scripts\buildsystems\msbuild\vcpkg.targets" Condition="Exists('..\packages\vcpkg\scripts\buildsystems\msbuild\vcpkg.targets')" />
</ImportGroup>
This setup ensures build works smoothly in Visual Studio without any additional work. However when working with CI/CD pipeline I have to first invoke EnsureVcpkgBuild first before trying to build the project. Can I set up this project in such way that I don't have to invoke EnsureVcpkgBuild and initiating build automatically sets up vcpkg and imports vcpkg.targets?
You can directly use nuget package to realize them all automatically.
What I think is that you wrote a target to download the reop project vcpkg into the local and then manually import the core target from the dowloadeded vcpkg project.
It could be done easily on the local area but on the cl pipeline, it is not suitable.
So use nuget could be more easier.
Just install the nuget package Vcpkg.Nuget into your c++ project.
All the import targets are in the nuget package and it could be imported into your project automatically with the installation of nuget.
When you are on the CI/CD pipeline, just add a nuget restore task to restore the nuget packages on the pipeline.

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.

.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.

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

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>