Running Nunit on console through restoring NuGet Package - c#

I am currently working on a project in C#, and trying to get NUnit to run on my project in a Jenkins Build through restoring the package in NuGet. We do not want to install NUnit on all of our Jenkins machines so installing it directly on the box is not an option.
We've investigated the use of NUniter Runners as suggested in this link: https://peteris.rocks/blog/running-nunit-with-msbuild-on-windows-and-mono/
But, we receive an error that the tools do not exist
"C:\Users\Name\NewBranch\project\project.Tests\project.Tests.csproj(121,3): error MSB6003
: The specified task executable "cmd.exe" could not be run. The working directory "C:\Users\Name\NewBranch\project\
packages\NUnit.Runners.3.4.1\tools" does not exist."
How do I execute NUnit tests from the command line without having NUnit installed directly on the machine?

the easiest way is to dump there 2 lines in the packages.config which should be in the same folder as your project.Tests.csproj
<package id="NUnit" version="2.6.4" targetFramework="net45" />
<package id="NUnit.Runners" version="2.6.4" />
Assuming you have a target in your .csproj which is like this:
<Target Name="NugetRestore">
<Exec Command="nuget.exe restore" />
</Target>
if you use a solution level build.config, then add this instead
<Target Name="NugetRestore">
<Exec Command="nuget.exe restore $(YourSolution).sln" />
</Target>

Related

code coverage in gitlab ci/cd for .net applications?

I recently started working with C# and I am working on one of the legacy system we have. I am trying to figure out what is the code coverage for this legacy system. Here is my Sample.UnitTests.csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoFixture.AutoMoq" Version="4.2.1" />
<PackageReference Include="AutoFixture.NUnit3" Version="4.2.1" />
<PackageReference Include="coverlet.msbuild" Version="2.9.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="nunit" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="WireMock.Net" Version="1.0.4.17" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="System.Buffers" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Sample/Sample.csproj" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="dotnet-reportgenerator-cli" Version="4.2.10" />
</ItemGroup>
</Project>
I did some research and found out we can use coverlet which can generate cobertura style report. I followed exactly as mentioned here on my mac box and everything works fine and I can see the report being generated correctly on my console and also it generates index.html file which we can use to visualize as well.
dotnet add package coverlet.msbuild
dotnet restore
dotnet build
dotnet test /p:CollectCoverage=true
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:Exclude="[xunit*]\*" /p:CoverletOutput="./TestResults/"
dotnet reportgenerator "-reports:TestResults/coverage.cobertura.xml" "-targetdir:TestResults/html" -reporttypes:HTML;
Now since we use gitlab ci/cd pipeline for our project - Is there any way I can make this part of my .gitlab-ci.yml file so that it can generate report automatically for me whenever build happens and everybody in my team can see it successfully. Since as of now it's all manual as I need to run those above commands on my local mac box and I can see it from my console only by clicking index.html file.
These are my stages of .gitlab-ci.yml file as shown below. If needed I can provide my yml file as well but any simple example where it can demonstrate how can I do this then it will be of great help. I tried searching a lot and couldn't find this at all on how can I do it through gitlab pipeline which uses coverlet and cobertura style report for .net applications..
stages:
- test
- publish
- increment
- deploy
- integrationTests
- release
Can this be done through webhook as well if needed?
May need to use the actual GUID instead of *.
stages:
- test
- publish
- increment
- deploy
- integrationTests
- release
build-and-test:
stage: test
image: mcr.microsoft.com/dotnet/sdk:6.0
script:
- dotnet add package coverlet.msbuild
- dotnet restore
- dotnet build
- 'dotnet test --collect:"XPlat Code Coverage"'
artifacts:
reports:
cobertura: TestResults/*/coverage.cobertura.xml
GitLab can diff with previous Cobertura reports.
If you want HTML instead, simply include it among the artifacts. May also publish it to GitLab Pages.
The other answer didn't work for me because of folder confusion. Coverlet puts the test results in folders relative to the respective unit test projects. So
Either you have to tell gitlab to search for TestResults folders everywhere via wildcards, by setting path to something like ./**/TestResults/**/coverage.cobertura.xml.
Or you provide the --results-directory option (short version -r), to tell dotnet test where to put those files in the first place.
I went for the second option, gathering all results in a cobertura folder in the repo root. Here is a full, valid .gitlab-ci.yml for running tests for merge requests:
image : mcr.microsoft.com/dotnet/sdk:6.0
stages:
- test
test:
stage: test
only:
- merge_requests
script:
- 'dotnet test DotNetSolution
--collect:"XPlat Code Coverage"
-r cobertura'
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: cobertura/*/coverage.cobertura.xml

Xunit Unit Tests will not run

I am completely stuck on this issue. So my team has a unit test project in a services test project. The tests are discovered in the test explorer pane however when I try and run the tests I get these errors:
'Multiple test adapters with the same uri
'executor://xunit/VsTestRunner2' were found. Ignoring adapter
'Xunit.Runner.VisualStudio.TestAdapter.VsTestRunner'. Please uninstall
the conflicting adapter(s) to avoid this warning'
'[xUnit.net 00:00:00.0251250] Skipping: (could not find
dependent assembly 'Microsoft.Extensions.DependencyModel,
Version=1.1.0')'
'No test is available in C:\. Make sure that test
discoverer & executors are registered and platform & framework version
settings are appropriate and try again.'
Context information:
Xunit 2.2.0
Visual Studio 15.5.2
Windows 10 1709 Build: 16299.125
My Test Projects project.json:
{
"version": "1.0.0-*",
"testRunner": "xunit",
"dependencies": {
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Microsoft.AspNetCore.Mvc.ViewFeatures": "1.1.3",
"Microsoft.DiaSymReader": "1.0.8",
"Microsoft.DiaSymReader.Native": "1.4.1",
"Microsoft.Extensions.Logging.Abstractions": "1.1.2",
"Microsoft.Extensions.Testing.Abstractions": "1.0.0-preview2-003121",
"Newtonsoft.Json": "9.0.1",
"WebServices": "1.0.0-*",
"xunit": "2.2.0",
"xunit.abstractions": "2.0.1",
"xunit.assert": "2.2.0",
"xunit.core": "2.2.0",
"xunit.extensibility.core": "2.2.0",
"xunit.extensibility.execution": "2.2.0",
"xunit.runner.utility": "2.2.0"
},
"frameworks": {
"net461": {
"dependencies": {
"Microsoft.NETCore.Platforms": "1.1.0"
}
}
}
The Weird thing is that it works for my team members. But not me. The differences in our environments are: the latest update from Windows and Visual Studio, while they are an update or two behind.
Does anyone know of a workaround?
Installing or updating "xunit.runner.visualstudio" package did the trick for me. Until that, it was not working with framework 462.
I had the same problem and I fixed it by updating Visual Studio 2017 from 15.5.2 to 15.5.4 and updating my test project references from:
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
to:
<ItemGroup>
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
And restarting Visual Studio (apparently VS caches and uses the previous version even after you've updated).
Not sure if one of the 3 things or the combination of them fixed it.
I encountered the same issue, but I have already had Visual Studio 2017 15.5.4 installed. In order to make it work I updated all xunit related references and now my .csproj relevant references look like this:
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
Not really sure what was not working, but my advice is to make sure that these reference are within csproj and have these minimum versions.
I was having a similar issue as yours, stating could not find dependent assembly; when in fact, that assembly was there and available to the xUnit VS runner. However, what the actual issue was, I had Visual Studio set to use the 32bit test runner by default. The 'dependent assembly' it couldn't find was simply a different processor architecture than the xUnit test runner being used.
To fix, as mentioned here, from the Visual Studio menu, go to Test -> Test Settings -> Default Processor Architecture and set that to X64 if you are using 64bit assemblies, or X86 if you are using 32bit assemblies. The error that the xUnit VS runner gives is very misleading to try to debug this issue.
This issue I saw when installing Visual Studio on a new machine. The tests would run on my desktop maching in Visual Studio, but not on my laptop.
Close and reopen Visual Studio works for me.
If you have a project running on the same Visual Studio (or any other instance of Visual Studio), please stop it. Although I know it's awkward, it solved the problem.
If your issue in Visual Studio is that the Test Runner output seems empty, check that the relevant buttons (white boxes, below) are all selected.
I had same issue, I installed below nuget packages
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Encountered this problem on VS 2019 recently.
Installing VS.QualityTools.UnitTestFramework nuget fixed the issue

.net core 2.0 error running console app on ubuntu

I'm trying to run my first .net core 2.0 console app on ubuntu 16.04-x64.
I followed the steps to publish my app for ubuntu:
dotnet publish -c release -r ubuntu.16.04-x64
and also tried it from Visual Studio by changing my .csproj file like so:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RuntimeIdentifiers>ubuntu.16.04-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="sharpadbclient" Version="2.1.0" />
<PackageReference Include="System.IO.Ports" Version="4.4.0" />
</ItemGroup>
</Project>
and then publish it with a publish profile.
I followed the instruction from Microsoft to install .net core on ubuntu.
I copied the published output to the PC running ubuntu ans when I'm trying to run the .dll file of my console app I'm getting this error:
Unhandled Exception: System.IO.FileLoadException:
Could not load file or assembly
'System.Console, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
The located assembly's manifest definition does not match the assembly reference.
(Exception from HRESULT: 0x80131040)
at LinuxVersion.Program.InitializeComponent()
at LinuxVersion.Program.Main(String[] args)
Aborted (core dumped)
When I'm running dotnet restore I'm getting a message saying:
MSBUILD : error MSB1003: Specify a project or solution file.
The current working directory does not contain a project or solution file.
Am I missing a step here in the process?
Well, turns out there is a difference between publishing the app using Visual Studio publish profile (right clicking on the project and selecting "publish") and using the command line.
When I used the Visual Studio publish profile I got this error, then I switched to using the command line like so: dotnet publish -c release -r ubuntu.16.04-x64
but to run it I went into the publish folder of the output: cd /home/MyApp/publish and then run the app using dotnet MyAppName.dll.
This solved it for me.

MSBuild command line with C# 6.0 code fails to build

I am having an issue getting MSBuild to compile c# 6.0 code, it reports a failure when trying to build expression body syntax, etx.
So I've been creating build project like this for years. I have a powershell script that kicks of an msbuild with some parameters:
msbuild.exe _build\build.proj
/p:Build_Number=1.2.0
/p:Configuration=QA
/p:SolutionName=MPGCS-Api.sln
/ToolsVersion:14.0
I verified that msbuild.exe is coming from C:\Program Files (x86)\MSBuild\14.0\Bin , i did this by going into that folder and specifying the full physical path to the .proj file. I get the same exact errors.
I have been looking for an example on how to setup a MSBuild project file with C# 6.0 with no luck, so this is my basic setup (this is the build.proj file). If I had to guess I am not importing the correct targets, etc. But I am a little lost. Here is my proj file:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Root>$(MSBuildStartupDirectory)</Root>
<NugetExe>$(Root)\_build\lib\nuget\nuget.exe</NugetExe>
<Build_Number>0.0.0</Build_Number>
<SolutionName></SolutionName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>
<Target Name="Clean">
<!-- Clean up -->
<ItemGroup>
<FilesToDelete Include="$(Root)\_build\Artifacts\**\*.*" />
<FilesToDelete Include="$(Root)\_build\Published\**\*.*" />
</ItemGroup>
<Delete Files="#(FilesToDelete)" ContinueOnError="false" />
<!-- Ensure directories exists -->
<MakeDir Directories="$(MSBuildProjectDirectory)\Artifacts" Condition="!Exists('$(MSBuildProjectDirectory)\Artifacts')" />
<MakeDir Directories="$(MSBuildProjectDirectory)\Published" Condition="!Exists('$(MSBuildProjectDirectory)\Artifacts')" />
</Target>
<Target Name="Debug" AfterTargets="Clean">
<!-- Diagnostics -->
<Message Text="Diagnostics:"/>
<Message Text="Build Number: $(build_number)" />
<Message Text="Configuration: $(Configuration)" />
<Message Text="VisualStudioVersion: $(VisualStudioVersion)" />
<Message Text="Project root: $(Root)" />
<!-- Restore Nuget Packages -->
<Message Text="Restoring nuget..."/>
<Exec Command="$(NugetExe) restore $(Root)\$(SolutionName)" />
</Target>
<Target Name="GenerateOctopackAPI" AfterTargets="Debug">
<ItemGroup>
<ProjectToBuild Include="$(Root)\MPG.CS.Api\MPG.CS.Api.csproj" />
</ItemGroup>
<MSBuild Projects="#(ProjectToBuild)" ContinueOnError="false" Targets="Rebuild" Properties="
RunOctoPack=true;
Configuration=$(ProjectBuildMode);
Platform=AnyCpu;
TargetFrameworkVersion=$(TargetFrameworkVersion);
VisualStudioVersion=$(VisualStudioVersion);
OctoPackPublishPackageToFileShare=$(Root)\_build\Artifacts;
OctoPackPackageVersion=$(Build_Number);
OctoPackProjectName=UI;
OutputPath=bin\$(ProjectBuildMode)" />
</Target>
</Project>
Here is some info from my "Debug" target, as you can see I am setting the VisualStudioVersion to 14.0 as per some suggestions I read online.
Diagnostics:
Build Number: 1.2.0
Configuration: QA
VisualStudioVersion: 14.0
Project root: C:\dev\mpg\MPGCS-Api
Here is an example error, it's basically failing on c# 6.0 code, if I were to remove c# 6 code, everything will compile:
TicketType.cs(19,28): error CS1002: ; expected [C:\dev\mpg\MPGCS-Api\MPG.CS.Model\MPG.CS.Model.csproj]
TicketType.cs(19,44): error CS1519: Invalid token '(' in class, struct, or interface member declaration [C:\dev\mpg\MPGCS-Api\MPG.CS.Model\MPG.CS.Model.cs proj]
TicketStatus.cs(16,36): error CS1002: ; expected [C:\dev\mpg\MPGCS-Api\MPG.CS.Model\MPG.CS.Model.csproj]
TicketStatus.cs(16,53): error CS1519: Invalid token '(' in class, struct, or interface member declaration [C:\dev\mpg\MPGCS-Api\MPG.CS.Model\MPG.CS.Model. csproj]
TicketStatus.cs(16,82): error CS1519: Invalid token '(' in class, struct, or interface member declaration [C:\dev\mpg\MPGCS-Api\MPG.CS.Model\MPG.CS.Model. csproj]
Here is a line it's failing on (C# 6 code):
public bool IsAbbreviated => (Title.ToLower() == "open" || Title.ToLower() == "closed");
I'm at a loss, I thought just using the proper msbuild.exe will allow me to take advantage of c# 6.0. Any help would be greatly appreciated.
If you're running the powershell script on a build server, separate from your dev environment, you will need to make sure that the machine itself can handle C# 6.0. Since it uses a different compiler all together, it wouldn't work if VS 2015 wasn't installed. Alternately, you can add the Microsoft.Net.Compilers nuget package as a dependency to allow VS 2012 and VS 2013 to compile it.

How to get SpecUnit to run within a TeamCity CI build

I am trying to get SpecUnit to run in a continuous integration build using Nant. At the moment the files are in the correct place but no output is generated from SpecUnit.Report.exe. Here is the relevant task from the nant build script:
<echo message="**** Starting SpecUnit report generation ****" />
<copy file="${specunit.exe}" tofile="${output.dir}SpecUnit.Report.exe" />
<exec program="${output.dir}SpecUnit.Report.exe" failonerror="false">
<arg value="${acceptance.tests.assembly}" />
</exec>
Please note:
${specunit.exe} is the full path to where “SpecUnit.Report.exe” is located.
${output.dir} is the teamcity output directory for the current build agent.
${acceptance.tests.assembly} is "AcceptanceTests.dll"
Anyone tried this before?
You need to specify the full path to the assembly argument I think...
<exec program="${output.dir}SpecUnit.Report.exe" verbose="true">
<arg value="${output.dir}${acceptance.tests.assembly}" />
</exec>

Categories

Resources