在TFS上配置ASP.Net Core所需的DNX环境
这里通过PowerShell实现,在解决方案中增加一个PowerShell脚本,这里名为 Prebuild.ps1
# bootstrap DNVM into this session. &{$Branch='dev';iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.ps1'))} # load up the global.json so we can find the DNX version $globalJson = Get-Content -Path $PSScriptRoot\global.json -Raw -ErrorAction Ignore | ConvertFrom-Json -ErrorAction Ignore if($globalJson) { $dnxVersion = $globalJson.sdk.version } else { Write-Warning "Unable to locate global.json to determine using 'latest'" $dnxVersion = "latest" } # install DNX # only installs the default (x86, clr) runtime of the framework. # If you need additional architectures or runtimes you should add additional calls # ex: & $env:USERPROFILE\.dnx\bin\dnvm install $dnxVersion -r coreclr & $env:USERPROFILE\.dnx\bin\dnvm install $dnxVersion -Persistent # run DNU restore on all project.json files in the src folder including 2>1 to redirect stderr to stdout for badly behaved tools Get-ChildItem -Path $PSScriptRoot\分销系统 -Filter project.json -Recurse | ForEach-Object { & dnu restore $_.FullName 2>1 } Get-ChildItem -Path $PSScriptRoot\MVC6.UnitTest -Filter project.json -Recurse | ForEach-Object { & dnu restore $_.FullName 2>1 }
其中 分销系统 和 MVC6.UnitTest 为我测试的项目,即project.json所在的位置,请自行修改为自己对应的项目
然后登录TFS修改生成定义,添加一个PowerShell的生成步骤,把脚本文件指向 Prebuild.ps1
如果你在TFS上找不到这个文件,请先把这个文件签入TFS
添加XUnit项目
如果你的VS中有XUnit的模板,直接添加就可以了。如果没有,可以先创建一个ASP.Net Core Class Library的项目,然后参照下面的 project.json 内容,来添加xunit package的引用
{ "version": "1.0.0-*", "description": "MVC6.UnitTest Class Library", "authors": [ "lishewen" ], "tags": [ "" ], "projectUrl": "", "licenseUrl": "", "frameworks": { "dnx451": { "dependencies": { "xunit.runner.visualstudio": "2.1.0" } }, "dnxcore50": { "dependencies": { "Microsoft.CSharp": "4.0.1-beta-23516", "System.Collections": "4.0.11-beta-23516", "System.Linq": "4.0.1-beta-23516", "System.Runtime": "4.0.21-beta-23516", "System.Threading": "4.0.11-beta-23516" } } }, "dependencies": { "xunit": "2.1.0-*", "xunit.runner.dnx": "2.1.0-*" }, "commands": { "test": "xunit.runner.dnx" } }
在单元测试中添加一个简单的测试方法
[Fact] public void PassingTestDnx() { Assert.Equal(1, 1); }
写一个运行xunit单元测试的PowerShell脚本,RunDnxTest.ps1
Set-ExecutionPolicy unrestricted -Scope CurrentUser -Force dnx -p $PSScriptRoot\MVC6.UnitTest test -xml $PSScriptRoot\MVC6.UnitTest\testresults.xml
最后参照 Prebuild.ps1的步骤,在生成定义中,再加一项PowerShell的生成步骤,并指向脚本 RunDnxTest.ps1
此步骤完成后,我们还需要添加一项 发布测试结果 的步骤,已便将XUint的结果与原来Visual Studio 的单元测试结果合并
具体参照下图配置:
至此,ASP.Net Core的自动生成和测试的配置就完成了,我们可以运行下生成定义来看看测试结果