Not able see test cases in Text Explorer for the .net core 2 based Test project - nunit

I have created a test project based on .Net Core 2 and wrote some NUnit test cases. After installing necessary NuGet packages i.e. NUnit3TestAdapter, I was able to see all test cases in "Test Explorer" and able to execute those. Now, when I looked into the project directory, I found that it's creating "obj" folder and some json files in it. So I tried to change the path of "obj" folder by modifying ".csproj" file. I provided some different path in the parameter "BaseIntermediateOutputPath" and that way, I was able to get rid of "obj" folder. The reason for providing different path was, I wanted to keep json files separate from source code.
However, after modifying that I am not able to see or execute any test cases from Test Explorer.
Is this a Microsoft bug?
Is any packages having dependency on "obj" folder?
P.S.
I am using "NUnit" and "NSubstitute" packages for my test project.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<TargetFramework>netcoreapp2.0</TargetFramework>
<OutputPath>..\..\build\$(Configuration)\UnitTests\</OutputPath>
<BaseIntermediateOutputPath>..\..\work\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
</PropertyGroup>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<ItemGroup>
<PackageReference Include="Castle.Core" Version="4.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="NSubstitute" Version="2.0.3" />
<PackageReference Include="NUnit" Version="3.8.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0" />
</ItemGroup>
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
<ItemGroup>
<ProjectReference Include="..\UtilityLibrary\UtilityLibrary.csproj" />
</ItemGroup>
</Project>

When .NET Core projects build, they do not copy all referenced files into the bin folder. When you add Microsoft.NET.Test.Sdk to your test project, one of the things it does is add an AssemblyResolve event handler which loads other dependent assemblies from a list of searchDirectories.
BaseIntermediateOutputPath not working was reported against the VSTest project and is an issue with MSBuild. The workaround is noted in the dotnet sdk repository. From that, you need to use Sdk imports in your csproj instead of the Sdk attribute on the Project element.
<Project>
<PropertyGroup>
<BaseIntermediateOutputPath>obj\XXX\</BaseIntermediateOutputPath>
</PropertyGroup>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<!-- Body of project -->
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>

Related

Substitute for $(SolutionDir) in Azure DevOps CI Pipeline

I have the following situation:
A SQL project in which i have a nuget package installed. This package (merely a PS script) is responsible for unpacking the DACPACs that the DB requires, using references to paths relative to the solution folder (to find the packages/ and dacpacs/ folders and to browse through the projects which it extracts from the .sln file). This is called as a pre-build event.
When building the entire solution, the $(SolutionDir) is defined, as expected (locally and ADO).
When building the test project, the $(SolutionDir) is either '' or '*Undefined*'. Again, as expected, because msbuild has no knowledge about the solution when building a single project. I can live with this caveat locally, no problem.
The question is this: is there something "magical" out there that i can use to make this work in Azure DevOps?
I can try various hacks, if anyone is aware of such methods, although i would like a clean solution.
Tried so far:
1) Adding the following PropertyGroup:
<PropertyGroup>
<SolutionDir Condition="'$(SolutionDir)' == '' Or '$(SolutionDir)' == '*Undefined*'">.\</SolutionDir>
</PropertyGroup>
to the test project.
2) Following these sugestions: Prebuild event in Visual Studio replacing $(SolutionDir) with *Undefined*
No effect.
If your folder structure is something like:
1.The Azure Devops Repos contains the solution folder(where exists the xx.sln file).
2.And those projects are under same solution folder.
You can try my script:
<PropertyGroup>
<ProjectFolder>$([System.IO.Directory]::GetParent($(ProjectDir)))</ProjectFolder>
<MySolutionDir>$([System.IO.Directory]::GetParent($(ProjectFolder)))\</MySolutionDir>
</PropertyGroup>
The $(MySolutionDir) represents the path where your sln file and project folders exists. Same like $(SolutionDir), it also has the \. So it's format looks like SomePath\.
And it's recommended to insert my script above the script of PreBuild event. Something like:
<PropertyGroup>
<ProjectFolder>$([System.IO.Directory]::GetParent($(ProjectDir)))</ProjectFolder>
<MySolutionDir>$([System.IO.Directory]::GetParent($(ProjectFolder)))\</MySolutionDir>
It's recommended to add my script and PreBuildEvent in same propertyGroup, and mine should be in the first.
<PreBuildEvent>echo $(MySolutionDir)</PreBuildEvent>
</PropertyGroup>
Edit1:
You can also add condition on that:
<PropertyGroup>
<ProjectFolder>$([System.IO.Directory]::GetParent($(ProjectDir)))</ProjectFolder>
<MySolutionDir>$([System.IO.Directory]::GetParent($(ProjectFolder)))\</MySolutionDir>
<SolutionDir Condition="xxxx">$(MySolutionDir)</SolutionDir>
It's recommended to add my script and PreBuildEvent in same propertyGroup, and mine should be in the first.
<PreBuildEvent>echo $(MySolutionDir)</PreBuildEvent>
</PropertyGroup>
Edit2:
Hmm, I now can reproduce the issue on my side. It's quite a strange behavior and I'm not sure about the root cause of this one. But a quick workaround is to create a new PropertyGroup to insert our custom script instead inserting it into existing PropertyGroup from default template:
It used to be:
....
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
<!-- Default to the v11.0 targets path if the targets file for the current VS version is not found -->
<SSDTExists Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')">True</SSDTExists>
<VisualStudioVersion Condition="'$(SSDTExists)' == ''">11.0</VisualStudioVersion>
<ProjectFolder>$([System.IO.Directory]::GetParent($'(ProjectDir)'))</ProjectFolder>
<ParentFolder>$([System.IO.Directory]::GetParent($'(ProjectFolder)'))\</ParentFolder>
<SolutionDir Condition=" '$(SolutionDir)' == '' Or '$(SolutionDir)' == '*Undefined*' ">$(ParentFolder)</SolutionDir>
<PreBuildEvent>echo $(SolutionDir)</PreBuildEvent>
</PropertyGroup>
<Import Condition="'$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<Import Condition="'$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<ItemGroup>
<Folder Include="Properties" />
</ItemGroup>
<ItemGroup>
<Build Include="test.sql" />
</ItemGroup>
<PropertyGroup>
<PreBuildEvent>echo $(SolutionDir)</PreBuildEvent>
</PropertyGroup>
</Project>
Now change it to be:
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
<!-- Default to the v11.0 targets path if the targets file for the current VS version is not found -->
<SSDTExists Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')">True</SSDTExists>
<VisualStudioVersion Condition="'$(SSDTExists)' == ''">11.0</VisualStudioVersion>
</PropertyGroup>
<Import Condition="'$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<Import Condition="'$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<ItemGroup>
<Folder Include="Properties" />
</ItemGroup>
<ItemGroup>
<Build Include="test.sql" />
</ItemGroup>
<PropertyGroup>
<ProjectFolder>$([System.IO.Directory]::GetParent($(ProjectDir)))</ProjectFolder>
<ParentFolder>$([System.IO.Directory]::GetParent($(ProjectFolder)))\</ParentFolder>
<SolutionDir Condition=" '$(SolutionDir)' == '' Or '$(SolutionDir)' == '*Undefined*' ">$(ParentFolder)</SolutionDir>
<PreBuildEvent>echo $(SolutionDir)</PreBuildEvent>
</PropertyGroup>
Also, remove the extra ' in $(ProjectDir). It should be $(ProjectDir) instead of $'(ProjectDir)' and $'(ProjectFolder)'. I also see you have two PreBuildEvent properties, just keep the one in our custom script. After above steps, you project now works well on my side:

Including referenced projects in NuGet package

I have the following project in my solution which I am trying to use to create a NuGet package:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>ExpressionTreeTestObjects</PackageId>
<Authors>Zev Spitz</Authors>
<Company />
<Product>ExpressionTreeTestObjects</Product>
<Description>A set of expression trees, and instances of other types from System.Linq.Expressions, for testing code against a variety of expression trees. The objects are generated by the C# compiler, by the VB.NET compiler, or using the factory methods at System.Linq.Expressions.Expression.</Description>
<Copyright>Copyright (c) 2019 Zev Spitz</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/zspitz/ExpressionTreeToString</PackageProjectUrl>
<RepositoryUrl>https://github.com/zspitz/ExpressionTreeToString</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>expression-tree code-generation visual-basic.net vb.net csharp test-data tostring</PackageTags>
<IncludeBuildOutput>true</IncludeBuildOutput>
<IncludeReferencedProjects>true</IncludeReferencedProjects>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ExpressionTreeTestObjects.VB\TestObjects.VB.vbproj" />
<ProjectReference Include="..\ExpressionTreeTestObjects\TestObjects.csproj" />
</ItemGroup>
</Project>
Build and pack both seem to work. However, the .nupkg file doesn't seem to include the referenced DLLs.
How can I troubleshoot this? How can I resolve it?
I think the solution can be found here:
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
<ItemGroup>
<BuildOutputInPackage Include="#(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
</ItemGroup>
</Target>

Get the location of NuGet packages

In my company we have some home made tools that are used in the build process when building other projects.
I need to use these tools in VS2017 BeforeBuild and AfterBuild scripts and it must work in MS Build as well.
The tools are distributed as NuGet packages and most of our projects are ported to PackageReference instead of Packages.config
I know that the current installation of MyTool (version X.Y.Z) is at C:\Users\Me\.nuget\packages\MyTool\X.Y.Z, but how do I reference it in my project file, so it also works when the next version is released?
I think C:\Users\Me\.nuget\packages can be replaced with $(NuGetPackageRoot), but what to do to always reference the version installed in the project?
Some Nuget packages seem to put contributions into MyProject.csproj.nuget.g.props and MyProject.csproj.nuget.g.targets in the obj folder, but I can find very little useful information about these files.
Inside a target, you can use this to create a property based on an item:
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
</ItemGroup>
<Target Name="PrintStuff" AfterTargets="AfterBuild">
<PropertyGroup>
<NewtonsoftJsonVersion Condition="'%(PackageReference.Identity)' == 'Newtonsoft.Json'">%(PackageReference.Version)</NewtonsoftJsonVersion>
<NewtonsoftJsonPath>$(NuGetPackageRoot)newtonsoft.json\$(NewtonsoftJsonVersion)\</NewtonsoftJsonPath>
</PropertyGroup>
<Message Importance="high" Text="JSON.NET version: $(NewtonsoftJsonVersion)" />
<Message Importance="high" Text="JSON.NET path: $(NewtonsoftJsonPath)" />
<Exec Command="ls" WorkingDirectory="$(NewtonsoftJsonPath)" Condition="'$(OS)' != 'Windows_NT'" />
<Exec Command="dir" WorkingDirectory="$(NewtonsoftJsonPath)" Condition="'$(OS)' == 'Windows_NT'" />
</Target>

Get rid of stylecop.json from nuget package

I've setup building nuget package from my project using package tab in project options. Everything seems to work except that my nuget package contains stylecop.json file. Build action for this file is set to "C# analyzer additional file". How to get rid of this file from nuget package?
This is my csproj file content
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseUrl>https://github.com/aixasz/ImageShareTemplate/blob/develop/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/aixasz/ImageShareTemplate/</PackageProjectUrl>
<Description>ImageShareTemplate is image template library to share an image to social like facebook, twitter, etc.</Description>
<RepositoryUrl>https://github.com/aixasz/ImageShareTemplate/</RepositoryUrl>
<PackageTags>image socialmedia</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0001" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0001" />
</ItemGroup>
<ItemGroup>
<Folder Include="Fonts\" />
</ItemGroup>
</Project>

TeamCity MSBuild, remap folders during deployment

We have a solution with a website project which is hosted on a load balanced environment. At the moment no CI is being used, and deployments are manual using zip-files >_< however I'm looking on setting it up, and have run into some difficulties.
The solution requires a App_Config folder containing all the configurations for the site in the root, however these configurations differ from each of the hostingserver, where one is the management server and another is the delivery server.
Each individual server configurations is stored in a separate folder at /Configs/servername/ containing a web.config file and the App_Config folder. These have been manually copied from this folder to the root to overwrite those that already existed.
Also deployment of the /Configs/ folder is not wanted.
Preferably no changes should have to be done to the Visual Studio solution.
Is it possible to automate this before deployment in TeamCity?
regards
You may want to set properties with different values based on the computer name.
That's one trick of the trade.
<Choose>
<When Condition=" '$(Computername)'=='MyManagementServer01' ">
<PropertyGroup>
<MyCustomProperty001>Red</MyCustomProperty001>
<MyCustomProperty002>Yellow</MyCustomProperty002>
</PropertyGroup>
</When>
<When Condition=" '$(Computername)'=='MyDeliveryServer01' ">
<PropertyGroup>
<MyCustomProperty001>Black</MyCustomProperty001>
<MyCustomProperty002>White</MyCustomProperty002>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<MyCustomProperty001>NoMatchMyCustomProperty001</MyCustomProperty001>
<MyCustomProperty002>NoMatchMyCustomProperty002</MyCustomProperty002>
</PropertyGroup>
</Otherwise>
</Choose>
You could setup a property called
<ConfigurationSourceFolder>/Configs/MyManagementServer01/</ConfigurationSourceFolder>
Or setup a "DeploymentType"
<DeploymentType>ManagementServerType</DeploymentType>
You can also put Conditions on "Targets" and even Tasks.
<MakeDir Directories="C:\MyCoolDirectory" Condition="('$(MyCustomProperty001)'!='')"/>
//////Preferably no changes should have to be done to the Visual Studio solution.//////
So here is an "in-general" tip.
Instead of putting alot of custom sometimes-hard-to-follow changes in the csproj files....use a basic .msbuild file.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapped">
<PropertyGroup>
<!-- Always declare some kind of "base directory" and then work off of that in the majority of cases -->
<WorkingCheckout>.</WorkingCheckout>
</PropertyGroup>
<Target Name="AllTargetsWrapped">
<CallTarget Targets="BuildItUp" />
</Target>
<Target Name="BuildItUp" >
<MSBuild Projects="$(WorkingCheckout)\MySolution.sln" Targets="Build" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="TargetOutputsItemName"/>
</MSBuild>
<Message Text="BuildItUp completed" />
</Target>
</Project>