Build all csproj files, except Test projects - .net-core

In DevOps you can create a dotnet build task where "Path to project(s)" can be set to **/*.csproj to build all the projects.
This works just fine, but it (obviously) also builds my *.Test.csproj projects.
I found some posts mentioning exclude patterns, so I tried doing the same as in those posts and tried the following combinations:
**/*.csproj;-**/*.Test.csproj
**/*.csproj;!**/*.Test.csproj
**/*.csproj;-:**/*.Test.csproj
**/*.csproj;!:**/*.Test.csproj
For all attempts I get the following error in the DevOps log:
Project file(s) matching the specified pattern were not found.
So, how can I create a dotnet build task to build all my projects except the *.Test.csproj projects?

here's what I've been using to run all tests except for some of them:
Test/**/*.csproj
!**/*.Billing.Test.csproj
!**/*.Queues.Test.csproj
!**/*.WidgetDataProvider.Test.csproj
so by the same token you need to use:
**/*.csproj
!**/*.Test.csproj
as far as I understand, order matter, so this will not work:
!**/*.Test.csproj
**/*.csproj

Related

How to exclude folders/projects from dotnet core publish task in Azure Build Pipeline (Classic version)?

I have an Azure DevOps GIT Repo with three different types of projects. Structure looks as follows;
Repository Structure
I have a Build Pipeline (Classic, not YAML) to build only the .Net Core Project as below.
Pipeline
The pipeline settings is pointed to consider only the NetCoreProject. The Restore, Build & Test tasks executes successfully. However, when it comes to the 'Publish' task the pipeline fails as it considers the entire repository (all projects) and not just NetCoreProject.
Actual cause of failure is in NetCPPProject folder there are some files without any extension (makefile, shortcut files, etc.) which the Publish task is not identifying and failing with below error.
##[error]Error: Failed find: ENOENT: no such file or directory, stat 'D:\a\1\s\NetCPPProject\inc'
Note: inc is a shortcut file (without any extension) and it is required to be present in the folder.
I tried following which did not work;
Added .artifactignore to ignore other folders but the .Net Core 'Publish' task seems not to consider that.
Tried specifying the arguments to Publish tasks to consider only the NetCoreProject .sln file and also .csproj file, but issue still occurs with same error message.
Reconfirmed the file exists in Build.SourceDirectory with a command line task.
Question is, how to make .Net Core 'Publish' task to exclude NetCPPProject and only consider NetCoreProject?
how to make .Net Core 'Publish' task to exclude NetCPPProject and only consider NetCoreProject?
To achieve this goal, you could try the following steps:
Step1: Disable Publish web projects option in Dotnet Publish task.
Step2: Set the target csproj file in Path to project field
For example:
Repo:
Result:

SonarQube: Unable to import test coverage

I have a SonarQube installation and am running it against a .Net core application with multiple xUnit projects. The SonarQube picks up the bugs and smells fine but like so many people, I can't get the code coverage to complete.
At present, I am generating cobertura coverage XML files using the following command:
dotnet test --collect:"XPlat Code Coverage"
I then copy these to a centralized directory from each of the test projects and use the following command to run/import the files:
dotnet SonarScanner begin /k:"my-project" /d:sonar.cs.vscoveragexml.reportsPaths=".\TestResults\*.xml"
dotnet build
dotnet SonarScanner end
According to the logs, the files are found but are not ingested.
INFO: Parsing the Visual Studio coverage XML report
C:...\TestResults\5.coverage.cobertura.xml WARN: Could not import
coverage report '..\TestResults\5.coverage.cobertura.xml' because
'Missing root element in
C:...\TestResults\5.coverage.cobertura.xml at line 2'
I've confirmed that the files contain valid XML that looks to be correct, but I'm not even sure that SonarQube is supposed to accept cobertura reports.
Is my approach the way others have gone when trying to get xUnit coverage reports into SonarQube?
Is there a better way? I'm happy to use OpenCover or similar if that is easier.
In case anyone comes across this in the future, I gave up trying to use the built in coverage in VS and used DotCover. Bit of an easier setup and now seem to have it working to a degree.
Download/extract dotCover and add the folder to Path
dotCover.exe --output=AppCoverageReport.html --reportType=HTML dotnet -- test
dotnet SonarScanner begin /k:"my-project" /d:sonar.cs.dotcover.reportsPaths=AppCoverageReport.html
dotnet build
dotnet SonarScanner end
Still having an issue where the test coverage gathering seems to be pulling in too much and as a result is causing an error. Investigating but not related to this question.

Getting vsbuild error while pushing asp.net code into azure devops

Iam trying to push my asp.net(mvc) code azure devops using Git bash, but I am getting VSBuild error saying:
Error: Preparing the test sources file failed.
Error: No test sources found matching the given filter '**\*test*.dll,!**\*TestAdapter.dll,!**\obj\**'
VsTest task failed.
That error should be separate from a git push.
Meaning:
you are pushing your sources
but your Azure pipeline might not be correctly configured
See this thread for instance:
From this error (No test sources found matching the given filter '**\*,!**\obj\*'), we can see that you release pipeline do not get the build Artifacts sources.
So you need add a "Copy Files" task before your publish task in the build pipeline:
Or add a "Copy Files" task before your "VsTest" task in the release pipeline.
If it still an issue for you, please share your Organization name and log in account.(please choose "viewable by Microsoft Only")
See also "Publishing test assemblies with artifacts in order to use them during functional tests on VSTS", and this thread, which illustrates a correct pipeline configuration:
From the error we know that Vstest task failed to find the test assemblies.
It may because the test files you defined in Test Files field is incorrect, or you specified a wrong search folder where the test assemblies are not exist. For below example. Vstest task will search for all the files that matching *test*.dll in folder $(System.DefaultWorkingDirectory)(eg. c:/agent/_work/1/s). Check here to find more predefined variables.
The fix is simple if you know where the test assemblies is copied by VsBuild task. You can check the log of Visual Studio Build task, to find out where the test.dll files are located.
Hope you find above helpful.

Nothing to do. None of the projects specified contain packages to restore

I have a web app in dot net along with other projects. when i open the .sln file and publish the web project, it does. But, when I try to publish the web proj using command line and .csproj, it gives an error. "Nothing to do. None of the projects specified contain packages to restore."
I got this after converting my csproj to vs2019 format and running dotnet test. The solution was to ensure i had the right test adapter and framework. My csproj files had both the old Microsoft.VisualStudio.QualityTools.UnitTestFramework reference and the new MSTest.TestAdapter + MSTest.TestFramework + Microsoft.NET.Test.Sdk.
I kept the new ones and my tests were run.
I had this problem in a batch file I use to build a UWP component for release. The build command is
%MSBUILD% CartoType\src\main\single_library\uwp\CartoType\CartoType.sln -p:Configuration=Release;Platform=x64
where %MSBUILD% is the full path to msbuild. I first added an msbuild command with the -t:restore option and got the message reported by the original poster ('Nothing to do. None of the projects specified contain packages to restore').
The solution was to add this command before the command given above:
%MSBUILD% CartoType\src\main\single_library\uwp\CartoType\CartoType.sln -p:RestorePackagesConfig=true -t:restore

How to run and publish .NETCore Xunit tests on VSTS (Vs2017)?

I had two build steps in VSTS:
To run tests (VSTS cmd task): DOTNET test -xml TEST-results.xml
To publish test results step (VSTS test publish task): format=XUnit and the file name from previous step
But after I upgraded to VS2017 the -XML tag is not working anymore. I changed step one to use this:
test --logger "trx;LogFileName=TEST-results.xml"
but the second step throws an error "Invalid results file. Please make sure the Test Result Format field in the task matches the result format of the file"
Is there another way to run .NetCore tests on VSTS? or am I doing something wrong?
Thanks,
starain-MSFT's answer will work, unless you want/need the xunit tests to be logged using an xunit logger. In that case, you'll need to do two things.
Add https://www.nuget.org/packages/XunitXml.TestLogger/1.0.2-pre-rtm as package ref to your test project, either through 'Manage NuGet Packages' in VS, or by adding the ref in your csproj file manually, i.e.
<PackageReference Include="xunitxml.testlogger" Version="1.0.2-pre-rtm" />
Modify the VSTS dotnet test build step to use this logger:
dotnet test -a:. -l:xunit
The -a:. switch, which specifies the adapter path, is only necessary for CLI tools V15.0, in 15.1 that can be removed (as discussed here). As of today, the VS2017 Hosted Queue is using 15.0, so you'll need the -a:. on VSTS for now.
The -l:xunit uses the friendlyname, which I think isn't so friendly since you have to dig into the source code for the particular logger to find the attribute where it is specified (as seen here for xunit and here for trx)
The docs for the -l switch are spotty to say the least, but in the github for vstest, there is a document which talks about test loggers and links to their repositories and nuget packages, which after you look at the source for the friendlyname, gets you all the way there for whichever logger you need. If you need a custom logger, those are great examples to help understand how to implement.
Finally, the publish step that you used originally should be fine, since the output file is still called TestResults.xml
Change "Test Result Format" to "VSTest of Publish Test" result step/task, it reads the result file correctly.
Use dotnet xunit instead of dotnet test. See Getting Started with .NET Core.

Resources