.NET Core with NUnit test - unit-testing

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.

Related

Missing file NuGet.targets on existing project

The scenario is:
1. Created a NEW solution
2. ENABLED download missing packages
3. Add projects to solution that already exists and depends on nuget packages
4. Building the solution generates an error:
... The missing file is <solution folder>\.nuget\NuGet.targets.
Why? Have missing something?
I'm using Visual Studio 2017 Pro on Windows 10
All my searches answer about the scenario where create a new solution and adds a new project that depends on nuget package, that's ok, but when have an existing project, nothing.
<solution folder>\.nuget\NuGet.targets is a file that NuGet's Visual Studio extension used to add in Visual Studio 2010, 2012 and 2013 when you right click on the solution and select "Enable NuGet Package Restore". It would then add an import in all of your project files to import the targets file. I was recently investigating a customer issue and as part of that investigation I found it made the following changes. Near the top of the csproj, it adds something like this:
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
and near the end of the csproj it adds somthing like this:
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
So, the problem is that you're creating a new solution, but using existing project files that have been modified in this way. One option is to edit your csrpoj files and remove these changes. Another option is to create new projects, in addition to the new solution, and then copy all your code and content files.
The only disadvantage is if you build on a CI server and use packages.config, your build script needs to run nuget.exe restore, whereas projects that use NuGet.targets could just build the solution and msbuild would execute nuget restore as needed. One advantage of no longer using NuGet.targets is that restoring the whole solution is faster than restoring project by project. Visual Studio automatically restores packages on build, even in VS2010, VS2012 and VS2013, so personally I discourage the use of using this feature, even if you use those old versions of Visual Studio. The benefit of reducing your build script by one step is not worth the issues it brings, in my opinion.

In Visual Studio 2017 (new csproj format), how can I set the Build Action for an entire folder?

Basically, I want everything in my "Resources" folder to be an EmbeddedResource.
This question is perfect, but the question there is about the old csproj format. In Visual Studio, how can I set the Build Action for an entire folder?
This seems to work, in the .csproj file:
<ItemGroup>
<EmbeddedResource Include="Resources\**" />
</ItemGroup>

All Unit Test projects in TeamCity does not compile failing to reference Microsoft.VisualStudio.QualityTools.UnitTestFramework

All our unit test projects (About 8 of them) just stopped compiling and I can't find any good reason for it.
"error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)"
From the TeamCity (v2017.2.2) build log the projects are missing the Microsoft.VisualStudio.QualityTools.UnitTestFramework DLL with error codes CS0234 and CS0246. I can't see any code changes that could have caused it
there was however a server restart that might have kicked in some pending changes.
What has been checked
Checked both with a Visual Studio (SLN) and MSBuild runner step, both gave the same results (MSBuild was previously working fine while the Visual Studio was failing on the C# 6/7 code, the reason why we changed to a newer MSBuild version)
These Test projects build fine on the same build server if the solution is opened in Visual Studio 2017 Enterprise (Installed on Build server)
Project Reference configuration
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
</ItemGroup>
</Otherwise>
</Choose>
MSBuild Version
We use the MSbuild v15.6.82.30579 in Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin not the default one in C:\Windows\Microsoft.NET\Framework64\v4.0.30319 (Added as such to environmental variables)
Any recommendations will be appreciated. I really don't want to start adding DLL's to the GAC if it can be helped.
If you are using Build Tools 2017 on your server, and you don't have Visual Studio 2017 installed, then you have to switch to MS Test v2:
https://blogs.msdn.microsoft.com/devops/2017/09/01/upgrade-to-mstest-v2/
https://www.nuget.org/packages/MSTest.TestAdapter
https://www.nuget.org/packages/MSTest.TestFramework
Old MS Test framework is not supported with Build Tools.
Then in TeamCity in Build Step you have to change your runner type to Visual Studio Tests, Test engine type from MSTest to VSTest, and Test engine Version to VSTest 2017
Install "VC++ 2017 version 15.7 v14.14 toolset" component under individual components, seems that the default toolset does not have the unit test binaries.
References:
Unit test files not installed by Visual Studio Build Tools 2017
Okay so the issue seemed to be broader than just Unit Tests. In short even though MSbuild and Visual Studio build (from the build server) running MSBuild step from Visual Studio does not utilize the same 'shared' folders.
I had to make use of a couple of different strategies to sort out version and missing common DLL's:
Update the Visual Studio Build Tools to the latest version i.e. get the latest version of MSBuild to the build server and ensure it is added to the OS environmental PATH (Set the default MSBuild to be the new one). In my case it was changing it from 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319' to 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin'
Add the missing/newer DLL to the MSBuild BIN folder and mark it as read-only so that it is not overridden again
As last resort you add the DLL to the GAC using the gacutil.exe. This is not recommended because it obfuscates the relationship (Rather try to find where it is used with Fusion logging and add it there).

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

How can I convert a Project from Visual C++ 6.0 to Visual Studio Express 2010?

I have issues compiling Integrating Vision Toolkit which comes with a Microsoft Visual C++ 6.0 workspace. Visual Studio Express 2010 fails to convert the workspace.
I tried VCUpgrade as described here without success.
Unable to convert project.
Please make sure this is a valid Visual C++ 6.0 project.
I tried to start over with a fresh project and add the files manually, the vcxproj contains the files like:
<ItemGroup>
<ClInclude Include="..\..\..\src\Helpers\BasicFileIO.h" />
<ClInclude Include="..\..\..\src\Helpers\Configuration.h" />
<ClInclude Include="..\..\..\src\Helpers\helpers.h" />
...
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\src\Helpers\BasicFileIO.cpp" />
<ClCompile Include="..\..\..\src\Helpers\Configuration.cpp" />
<ClCompile Include="..\..\..\src\Helpers\helpers.cpp" />
...
These files do not compile since the (already contained) header files cannot be resolved.
How should I setup the project to include cpp and h files from different directories?
I hope that there is a better way than copying the files into a flat directory.
(I didn't use C++ for many years, so please explain it for a beginner)
You need VC++ 2008 Express (or retail version of VC++ 2010, like Professional edition) to convert from VC++ 6.0. Take a look at this thread: Visual C++ 2010 Express cannot open Visual C++ 6.0 projects
You need to set the include directory in the IDE. Right click your project and select Properties. Navigate to C/C++ -> General -> Additional Include Directories and add the path to your include folder (the folder that contains the Helpers folder with your helpers.h file in it). Something like this:
$(SolutionDir)your_project\include