How to change “Class Library Project” into “Test Project"? - asp.net-mvc-3

While working on ASP.NET MVC3 application, by mistake I have added a Class library as a Unit Test project. But unfortunately I don’t see the "Run Tests" from context menu to test the methods which are created for unit testing .
Is there any way to convert the “Class Library Project” into a “Test Project” ?

There is a property type guid in the project file.
Look at this post: How does Visual Studio /mstest identify test projects?

add Microsoft.NET.Test.Sdk from nuget

What worked for me was to do add this <PropertyGroup> tag with this libraries to my project (.csproj or .vbproj) file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="1.3.0" />
</ItemGroup>
<!--Here goes any other configuration-->
</Project>

Related

ASP.NET Core 2.0 all the references inside the `startup.cs` are raising errors

I use to have any ASP.NET Core MVC web project version 2.0 project working well inside visual studio 2019,.
But when i opened the project inside a new development machine using visual studio 2022 >> i started getting these errors inside the startup.c,and all the references inside the startup.cs are raising errors:-
any advice on this please?
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<UserSecretsId>****</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\file\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
</ItemGroup>
</Project>

Unable to reference PCL Project in Unit Test Project in VS2019 - Xamarin

How do I fix this issue ? Unable to add PCL reference to my Unit Test Proj.
PCL proj is using framework NetStandardLib v2.1
Testing this, NUnit Test project doesn't let you switch between Framework and Core once you have created it. Perhaps because different PackageReferences are needed.
Safest fix is to create a new NUnit Test project. But this time, when it asks for target Framework, pick a .Net core one. Probably pick the highest number it offers (Core 3.1).
Then copy or move all your test source files into it.
Looking at a Framework Test Project vs a Core one, these are the lines that are different.
Framework:
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
...
<ItemGroup>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0"/>
</ItemGroup>
...
Core:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
...
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>
...

How can I include the entire build output in a nuget package programatically using GeneratePackageOnBuild

I'm using <GeneratePackageOnBuild>True</GeneratePackageOnBuild> to get Visual Studio to automatically build a Nuget package when I build my project. But this package is being used as a plugin to another project. So I've included <EnableDynamicLoading>True</EnableDynamicLoading>. This causes all of the dependency DLLs to be copied to the build output folder. So far so good.
The problem is that when Visual Studio builds the Nuget package, only the main project dll is included in the package. How do I get Visual Studio to include all of the build output files in the Nuget package?
Here is my project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Version>1.2.6</Version>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<EnableDynamicLoading>True</EnableDynamicLoading>
<PackageId>ProjectName</PackageId>
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Package1" Version="3.1.0">
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="Package2" Version="1.0.1" PrivateAssets="All" />
<PackageReference Include="Package3" Version="1.0.4" PrivateAssets="All" />
</ItemGroup>
</Project>
I want my nuget package to include: ProjectName.dll, Package2.dll, and Package3.dll

How to run T4 template in Blazor project on build

I have a Blazor project with a T4 template I wrote for scaffolding some code automatically. It works great from within Visual Studio, but I have to modify & save the template to get it to run again (as documented and expected).
I also want to run the template when building the project, so instead of VS running the template, it has to be MSBuild. I went through a bunch of articles about the topic and it looks like I have to re-import the default targets, as explained here.
I added the following to the top of my .csproj file, and this is when things went south:
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
This is the error I'm getting:
The TargetFramework value 'netstandard2.1' was not recognized. It may
be misspelled. If not, then the TargetFrameworkIdentifier and/or
TargetFrameworkVersion properties must be specified
explicitly. TestProject C:\Program
Files\dotnet\sdk\3.1.401\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets 93
Full .csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
</ItemGroup>
</Project>
What am I missing/doing wrong?
You should note the info from the article:
Fortunately, there’s a workaround: you can import the default targets
file explicitly, and import the text templating targets after that:
Solution
You should import those targets after netstandard 2.1 node.
In my side, I use these:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets"/>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build"
Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer"
Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0"/>
</ItemGroup>
</Project>
Then,

How to mark files of a folder as embedded resource with wildcards with the new project format?

My scenario is simple.
I have test project where i want all files within a folder to be marked as embedded resource by default. To prevent someone from doing mistakes here i want this to be automatic through wildcards
I looked at this question, which looked very promising.
MSBuild: Include a custom resource file as embedded resource
However that does not seem to work with the new csproj format. Does anyone know what i should be doing different for it to work with the new format?
My current code is this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.2.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.2.1" />
</ItemGroup>
<Target Name="BeforeBuild">
<CreateItem Include="TestContent\*.cs">
<Output ItemName="EmbeddedResource" TaskParameter="Include" />
</CreateItem>
</Target>
</Project>
You can try this script:
<Target Name="MyCustomStep" BeforeTargets="BeforeBuild">
<CreateItem Include="TestContent\*.cs">
<Output ItemName="EmbeddedResource" TaskParameter="Include" />
</CreateItem>
</Target>
There exists difference between the BeforeBuild Target in old and new csproj format. (Or maybe the difference between .net core and .net framewrok, not sure about this point)
Some discoveries when I set the msbuild verbosity to Detailed:
1.For projects that target .net framework using the old csproj format:
The BeforeBuild target will exactly execute the CreateItem Task. So it works for old-format project files.
2.For projects that target .net core using new sdk format:
The BeforeBuild target seems not to execute the task as what we expected.
After defining the Custom target which executes before the BeforeBuild target, it works in my machine:

Resources