2017-01-31 112 views
10

VS代碼的最新版本已經提供了運行單個測試的簡單方法,如Tyler Long's answer指向Debugging xunit tests in .NET Core and Visual Studio Code的問題。如何在Visual Studio代碼中運行所有測試代碼

但是,我正在尋找如何運行包含在VS代碼中的測試套件類中的所有測試

我發現這是增加launch.json具體配置如下一個唯一的辦法,但我只能在調試運行(我想沒有調試運行):

{ 
    "name": ".NET Core Xunit tests", 
    "type": "coreclr", 
    "request": "launch", 
    "preLaunchTask": "build", 
    "program": "/usr/local/share/dotnet/dotnet", 
    "args": ["test"], 
    "cwd": "${workspaceRoot}/test/MyProject.Tests", 
    "externalConsole": false, 
    "stopAtEntry": false, 
    "internalConsoleOptions": "openOnSessionStart" 
} 

回答

8

您可以運行通過在終端上執行dotnet test來執行項目中的所有測試。如果您已經打開終端,這很方便,但您也可以將其添加到Visual Studio代碼中。

如果按Cmd的 - - P打開命令面板,並鍵入「測試」,您可以運行運行測試任務命令。默認情況下,這並不做任何事情,但你可以編輯tasks.json告訴它如何爲您運行dotnet test

tasks.json

{ 
    "version": "0.1.0", 
    "command": "dotnet", 
    "isShellCommand": true, 
    "args": [], 
    "tasks": [ 
    { 
     "taskName": "build", 
     "args": [ ], 
     "isBuildCommand": true, 
     "showOutput": "silent", 
     "problemMatcher": "$msCompile" 
    }, 
    { 
     "taskName": "test", 
     "args": [ ], 
     "isTestCommand": true, 
     "showOutput": "always", 
     "problemMatcher": "$msCompile" 
    } 
    ] 
} 

這兩個任務定義將鏈接運行構建任務運行測試任務 Visual Studio代碼中的命令分別爲dotnet builddotnet test

8

有運行所有測試更簡單的方法:

  1. 安裝.NET Core Test Explorer擴展
  2. 打開在VS守則.NET核心測試項目,或者設置dotnet-test-explorer.testProjectPath到.NET核心的文件夾路徑在settings.json
  3. 在Explorer視圖中的.NET測試瀏覽器的測試項目,所有的測試將被自動檢測到,並且你能夠運行所有測試或某個測試

test-explorer

0

與@Nate Barbettini的答案類似,但是對於.Net Core Standard 2.0(netcoreapp2.0)。

{ "version": "2.0.0", "tasks": [ { "label": "test", "command": "dotnet test path/to/test-project.csproj", "type": "shell", "group": "test", "presentation": { "reveal": "silent" }, "problemMatcher": "$msCompile" } ] }

2

要建立在GraehamF的回答,在tasks.json爲DOTNET 2.0所需的配置是不同的。

{ 
"version": "2.0.0", 
"tasks": [ 
    { 
     ... 
    }, 
    { 
     "label": "test", 
     "command": "dotnet", 
     "type": "shell", 
     "group": "test", 
     "args": [ 
      "test", 
      "${workspaceFolder}/testprojectfolder/testprojectname.csproj" 
     ], 
     "presentation": { 
      "reveal": "silent" 
     }, 
     "problemMatcher": "$msCompile" 
    } 
] 

我發現,當Visual Studio和VS代碼同時安裝,投入命令屬性的csproj參考(如GraehamF的答案),導致在Visual Studio中被打開,而不是VS代碼中運行測試。

(我會把它放在評論中,但我沒有足夠的聲望點。)