How do I define a C# Unit Test Project using CMake? - c#

I'm converting some existing C# projects to be defined in CMake -- moving from the previous include_external_msproject() directive to the newer full support for C#.
But I'm not seeing how to convert projects of the Visual C# Unit Test Project type. I'm able to build them as libraries, and compile them successfully -- but Visual Studio doesn't show them as unit test projects, just as regular libraries. Most crucially, the tests aren't visible to the Test Explorer.
Things I've already tried include:
Adding TestProjectType=UnitTest as a target property:
<TestProjectType>UnitTest</TestProjectType>
Adding a reference path, as follows, as a target property:
<ReferencePath>$(ProgramFiles)/Common Files/microsoft shared/VSTT/$(VisualStudioVersion)/UITestExtensionPackages</ReferencePath>
Adding Microsoft.VisualStudio.QualityTools.UnitTestFramework as a project reference (using CMake's VS_DOTNET_REFERENCES property).
I'm using Microsoft Visual Studio Professional 2015, CMake 3.13.2, .NET Framework 4.5.2 (but I suspect the issue isn't specific to my particular version combination).

Manually adding <TestProjectType>UnitTest</TestProjectType>, made my unit test project show up as unit test. Trying to figure out how to do that using cmake.
Update: the following shows the project as unittest
set_target_properties(${target_name}
PROPERTIES
VS_GLOBAL_PROJECT_TYPES "{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"
)
Documentation for VS_GLOBAL_PROJECT_TYPES is here.
I found this documentation for Project Guid: Visual Studio project type guids

Thanks #bhardwajs, your answer helped me.
You can also add TestProjectType to global properties:
set_target_properties(${target_name} PROPERTIES
VS_GLOBAL_TestProjectType "UnitTest"
VS_GLOBAL_PROJECT_TYPES "{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}")

Related

Visual Studio adds a duplicate reference when creating NUnit Test project

I have a simple .sln with 2 projects:
-- .NET standard Library
-- NUnit Test Project
The issue presents itself when I add the NUnit Test Project to the .sln because VisualStudio adds another reference to the test inside the .NET standard Library project (see picture).
Screenshot from VS Explorer:
This folder is not physically present on my laptop but still is causing several build errors for duplicate references.
To add more information:
Deleting the NUnit Test from the Library project will make the Test unavailable.
enter image description here
I've found the culprit even though I'm not sure why it behaves like that.
My solution structure was
-- sln
-- Folder
-- .Net Library
-- NUnit Test
Eliminating the folder will eliminate this strange behavior and adding the NUnit test will not create a duplicate inside the .Net Library.

How have test project reference Maui project?

I've recently jumped into cross-platform development and wanted to just bite the bullet and go straight into the newest framework that the dotnet team has started working on, MAUI.
The issue I've run into is trying to add another test project in the solution and referencing the MAUI project. As I'm pretty new to Visual Studio as well, this may just be a rookie mistake and I just don't know what I'm doing, but here I am asking away.
What I've done:
Created MAUI project with the default template in Visual Studio 2022 Preview 17.2.0, with target frameworks (net6.0, net6.0-android & net6.0-ios)
Added XUnit test project on the side with target framework (net6.0)
Added UseMaui=true as a property of the test project
I've also tried to add all the target frameworks into the project, but that causes more issues than it solves. I've tried to follow this open source project where they got it to work, but I'm unsure how to achieve it.
Some screenshots
Test Project .csproj
Alt+Enter of class not being imported
Manually added import because suggestions did not add them
Error in Test Project
Test Project
This link may be helpful.
https://www.softwaremeadows.com/posts/net_maui_progressing_from_a_default_project_part_6_-_revisiting_unit_testing/
Summary:
In your .csproj file, add 'net6.0' to TargetFrameworks as below.
<TargetFrameworks>net6.0;net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks>
Modify OutputType to use Condition.
<OutputType Condition="'$(TargetFramework)' != 'net6.0'">Exe</OutputType>
Its probably because of mismatch in project target frameworks. Easy fix is to add Target Framework to your maui project that matches the one used in test project. For example if your Test poject is base on net6.0, just add net6.0 as a target framework to your maui app project.
Edit 1:
Also sometimes you need to add empty start point for netX.X build, looking like this
public static void Main(string[] args)
{
}

Version conflict - SpecFlow Visual Studio extension attempted to use SpecFlow code-behind generator 1.9, but project 'WpfApp3' references SpecFlow 3.1

I have installed latest version of Specflow(3.1.97), Nunit (3.1.97), MS Build Generator(3.1.97) in my visual studio 2017, I have created a feature file but its class file throwing errors.
I am getting below errors:
Version conflict - SpecFlow Visual Studio extension attempted to use SpecFlow code-behind generator 1.9, but project 'WpfApp3' references SpecFlow 3.1.
We recommend migrating to MSBuild code-behind generation to resolve this issue.
For more information see https://specflow.org/documentation/Generate-Tests-from-MsBuild/
Any help would be really appreciated.
Please let me know if this works. I think I have had the same problem and it was hard to resolve.
Try clearing these two property fields of your feature file
And then rebuild

How to control NuGet packages in Android Class Library?

The setup is like this:
A Xamarin.Android application, which depends on Android class library
(at least that's what the template is called in VS)
Said class
library, the purpose of which (not entirely relevant, but FYI) is
interfacing with a REST service and has a dependance on the famous
Newtonsoft.Json NuGet package.
An NUnit test project for said
library.
IDE is Visual Studio 2017, latest version.
If you build and deploy the app on the phone, everything is fine.
However, if you try to run tests from the tests project, it says that it can't find the Newtonsoft library.
I've even managed to find a sort of reason: when the library gets built, it's dependancies aren't packed inside, and they are not copied to build directory.
When .apk is built for the phone, the dependancies ARE getting packed inside.
However, when NUnit project builds itself, it only takes the library, and the dependancies are nowhere to be found.
However, there's no interface to control the behaviour of NuGet "Package Reference" type dependencies (blue ones), the properties window is empty for them. And I found no way to add NuGet packages to this kind of project as a ".config" type of dependancy (grey one).
There is a workaround - you can add the Newtonsoft package to the NUnit test project, then it gets copied to the build directory and the Android library works with it, however it doesn't feel right to me. Tests don't need that reference and it has no business in that project.
How to control NuGet packages in Android Class Library?
Your workaround is the correct solution, you don't need to worry too much about it.
That is because the Newtonsoft package is not used directly in your NUnit test project, so Visual Studio / MSBuild doesn't know if your test project needs this Newtonsoft library. In order to avoid reference pollution in NUnit test project, Visual Studio / MSBuild tries to only bring references over into NUnit test project that it detects as being required by project Xamarin.Android application.
So, to resolve this issue, we often add Newtonsoft to the test project or give a copy task to copy it to the test project.
See This Thread for some more details.
Looking for an answer to another question I now found info that my described behaviour is a known problem, described by .NET developers here:
https://github.com/dotnet/standard/issues/481

Difference between Microsoft.VisualStudio.TestPlatform.TestFramework and Microsoft.VisualStudio.QualityTools.UnitTestFramework

I noticed a change in one of our solutions in VS 2015 today.
It seems the test projects that are generated for the solution use a different namespace than the existing test projects in the same solution.
Only the test projects that reference
Microsoft.VisualStudio.QualityTools.UnitTestFramework
are recognized as test projects within Visual Studio 2015.
However there are now some test projects referencing
Microsoft.VisualStudio.TestPlatform.TestFramework
Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions
It's possible that these test project where generated from VS 2017 RC. This would explain the different namespace. I didn't find any docs about backward compatibility.
Question is, which namespace to use? As we can't use both namespaces at the same time.
What are the differences between the two namespaces?
VisualStudio 2015 is able to build the test projects with either reference. To switch the newer namespace back to the older namespace only because of the test project generation is not reason enough I think.
MSTest has been released as a new NuGet package solution that is no longer tightly coupled to the Visual Studio version. Your new projects are using MSTest 2.0.
MSTest V2 release annoucement: https://devblogs.microsoft.com/devops/taking-the-mstest-framework-forward-with-mstest-v2/

Categories

Resources