Sdk: Question: How to run the test task for multiple projects?

Created on 11 May 2017  路  6Comments  路  Source: dotnet/sdk

Excuse me if here isn't the best place to do this question, but I searched on the Internet and I didn't found a solution.

When dotnet core was "project.json" based I could configure the test task (in VsCode) this way:

    {
        "taskName": "test",
        "args": [
            "tests/unit/model/project.json",
            "tests/unit/domainService/project.json"
        ],
        "isTestCommand": true,
        "showOutput": "silent",
        "problemMatcher": "$msCompile"
    }

But in the .csproj based version, when I try to do the same thing I get this message: Specify a single project file to run tests from.

Actually is there any way to do this? Dotnet-test's docs says that only one ".csproj" is permitted.
Is there a way to do that with ".sln" target?

Thank you!

Most helpful comment

@bernardbr to unblock the scenario you can also use a custom msbuild project that executes the test target on all test projects. E.g. if you have a test.proj like this:

<Project>
  <ItemGroup>
    <TestProject Include="test\**\*.*proj" />
  </ItemGroup>
  <Target Name="Build">
    <MSBuild Projects="@(TestProject)" Targets="VSTest" Properties="VSTestLogger=trx" ContinueOnError="ErrorAndContinue" />
  </Target>
</Project>

you can test all your projects using

dotnet msbuild /m test.proj

All 6 comments

Is this what you are looking for: https://github.com/Microsoft/vstest/issues/411?

Thanks, @livarcocc!

@livarcocc
Nowadays doesn't have a solution to this issue.

In my case, I solved it with a python script. I've tried to do it with a PowerShell script, but I got restriction error.

Python solution:

import subprocess
from argparse import ArgumentParser

parser = ArgumentParser()
parser.add_argument("-f", "--files", nargs="+", dest="files", help="Define a .csproj file list.")

args = parser.parse_args()

for f in args.files:
    subprocess.call(r"C:\Program Files\dotnet\dotnet.exe" +  " test " + f)

PowerShell solution:

param(
    [Parameter(Mandatory = $true, Position = 0)]
    [string[]]$csProjFiles
)    

foreach ($csProjFile in $csProjFiles)
{
    dotnet test $csProjFile
}

My task.json (VsCode):

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "isShellCommand": true,
    "args": [],
    "tasks": [
        {
            "taskName": "build",
            "command": "dotnet",
            "args": [
                "build",
                "solution.sln"
            ],
            "isBuildCommand": true,
            "showOutput": "silent",
            "problemMatcher": "$msCompile"
        },
        {
            "taskName": "test",
            "command": "py",
            "args": [
                "${cwd}/run_tests.py",
                "-f",
                "${cwd}/test/unit/model/model.csproj",
                "${cwd}/test/unit/repository/repository.csproj"
            ],
            "isTestCommand": true,
            "showOutput": "silent"
        }
    ]
}

@bernardbr to unblock the scenario you can also use a custom msbuild project that executes the test target on all test projects. E.g. if you have a test.proj like this:

<Project>
  <ItemGroup>
    <TestProject Include="test\**\*.*proj" />
  </ItemGroup>
  <Target Name="Build">
    <MSBuild Projects="@(TestProject)" Targets="VSTest" Properties="VSTestLogger=trx" ContinueOnError="ErrorAndContinue" />
  </Target>
</Project>

you can test all your projects using

dotnet msbuild /m test.proj

Thanks a lot @dasMulli!
Your solution works just fine!

:+1: :smile:

The problem with the above solution is that it produces multiple test result files instead of one which is problematic in DevOps.

Was this page helpful?
0 / 5 - 0 ratings