2

If there is more information needed, let me know in the comments.


Automated testing is supposedly supported fairly readily in Azure DevOps, by way of adding one or more tasks to your pipeline, which can be triggered after pushing new commits and having your software built automatically. With Visual Studio, this would generally take the form of DevOps building your Visual Studio solution, then running one or more test projects that are, more than likely, a part of that solution.

My problem is this: There does not seem to be one, single example of how to actually do this. And when I try, I get this error:

This task is supported only on Windows agents and cannot be used on other platforms.

This is after adding the Visual Studio Test task. When using a minimal pipeline, the solution is able to build fine, and the pipeline runs correctly. When adding a very, very basic task to run the unit tests, the error message above is returned.

I have tried searching around for clear instructions or examples of how to set this up, and I have tried searching for that particular error. What results do come up are simply not very informative.

Because clear instructions don't exist elsewhere, I will ask on SO: What are the basic, but clear steps needed to set up an Azure DevOps pipeline which will build a Visual Studio solution which uses .NET Core, then run the test project(s) inside?

Panzercrisis
  • 4,590
  • 6
  • 46
  • 85
  • @PatrickLu-MSFT Good morning. I couldn't get his solution to work as-is. Ultimately I had to resort to the GUI editor, instead of the YAML one. ...However his YAML looks very similar to the GUI solution that was used, and his answer has multiple upvotes. I don't know why it didn't work in my case, but his answer is apparently good, and I've upvoted. – Panzercrisis Mar 16 '20 at 14:36
  • Thanks for your kindly feedback. Always better when you fix it yourself. Then you could share your solution here and *mark it as answer*. Which will also helps others in the community. – PatrickLu-MSFT Mar 17 '20 at 01:23

1 Answers1

3

Visual Studio Test task is required to be run on Windows. Take a look at the example - in pool:vmImage: ubuntuLatest I am specifying to get a concrete machine to run all my steps inside. See the list of default machines hosted by Microsoft. For instance, you can use windows-latest to have your Visual Studio Test step run properly.

However, Azure DevOps introduced a new set of dotnet core CLI tasks to build and test .net core applications (Windows is not required to perform them).

I found really nice description from Scott Hanselman's blog. It would build, test, and publish your solution with .net core projects.

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

variables:
  buildConfiguration: 'Release'

steps:

    - task: UseDotNet@2 
      displayName: ".NET Core 3.1.x"
      inputs:
        version: '3.1.x'
        packageType: sdk
    - script: dotnet build --configuration $(buildConfiguration)
      displayName: 'dotnet build $(buildConfiguration)'

    - task: DotNetCoreCLI@2
      displayName: "Test"
      inputs:
        command: test
        projects: '**/*tests/*.csproj'
        arguments: '--configuration $(buildConfiguration)'

    - task: DotNetCoreCLI@2
      displayName: "Publish"
      inputs:
        command: 'publish'
        publishWebProjects: true
        arguments: '-r linux-x64 --configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
        zipAfterPublish: true

    - task: PublishBuildArtifacts@1
      displayName: "Upload Artifacts"
      inputs:
        pathtoPublish: '$(Build.ArtifactStagingDirectory)' 
        artifactName: 'hanselminutes' 

Later you can play with official MSDN documentation to add code coverage, test results, etc.

Michał Jarzyna
  • 1,836
  • 18
  • 26
  • Thank you! When I try to adapt his example to my configuration (basically just using things similar to his `displayName: "Test"` section), in the best case scenario, it'll complain that I'm targeting .NET Core 3.1, and that it can only handle .NET Core 2.1 and lower. The problem is...my projects and solution are 2.1 and lower. I've checked the csproj files in the repo in DevOps, and they all say 2.0. Why would it think I'm targeting 3.1? – Panzercrisis Mar 05 '20 at 01:20
  • First Step is specifing which net core framework version you would like to use - change 'input:version:3.1.*' to '2.0.*' or '2.1.*'. However this is strange, I never tried it but assumed it has backward compability – Michał Jarzyna Mar 05 '20 at 04:48