在Visual Studio 2010中安装xUnit进行单元测试(xunit.runner.console)

分类:.NET
 标签:VS2010,xUnit,单元测试,xunit.runner.console
   修改 | 阅读(3062)| 评论(0)


1、在Visual Studio 2010中安装【NuGet程序包管理器】

依次点击菜单栏【工具】→【拓展管理器】,选择左边的【联机库】,在右上角输入框中输入“NuGet”,找到如下图中的“NuGet Package Manager”下载安装,安装完成后重启VS2010。

2、用VS2010创建一个新项目,本例以“xUnitTestProject”为名

创建项目步骤自行脑补。


3、打开NuGet程序包管理器控制台

依次点击菜单栏【工具】→【NuGet程序包管理器】→【程序包管理器控制台】

3、在控制台中选择编写单元测试的项目

本例中整个解决方案只有一个项目,实际项目中请根据项目名称选择。

4、安装xUnit和xUnit.Runner.Console

在程序包管理器控制台中输入以下代码进行xUnit的安装

install-package xunit

如果出现如下图所示错误,请指定较低版本的xUnit进行安装

指定版本安装xUnit和xUnit.Runner.Console

install-package xunit -version 1.9.1

install-package xunit.runner.console -version 2.0.0


5、配置xunit.runner.console到外部工具中

依次点击菜单栏【工具】→【外部工具】,打开外部工具管理窗口

点击【添加】按钮,做如下配置:

标题:xUnit Test  (可自定义)


命令:D:\WorkSpace\xUnitTestProject\packages\xunit.runner.console.2.0.0\tools\xunit.console.x86.exe

(经过步骤4安装后会在项目文件夹下多出一个packages文件夹,安装的包都放在这里,我们找到xunit.runner.console的位置,把“xunit.console.x86.exe”命令添加进去,如果是64位的项目,请添加“xunit.console.exe”)


参数:

如果需要运行单个测试方法,参数为:

$(BinDir)$(TargetName)$(TargetExt) -method "$(TargetName).$(ItemFileName).$(CurText)"

如果需要运行项目中所有的测试方法,参数为:

$(BinDir)$(TargetName)$(TargetExt)


初始目录:$(BinDir)


使用输出窗口:打勾


6、编写单元测试方法

[Fact(DisplayName = "测试xUnit")]
public void test_xunit()
{
	Xunit.Assert.Equal(4, 2+3);
}

注意方法要加上[Fact(DisplayName = "测试xUnit")],xUnit以Fact属性标识为单元测试方法

7、运行单元测试

选中要测试的方法名,依次点击菜单栏上的【工具】→【xUnit Test】运行单元测试。(按照运行单个测试方法的参数配置)

可以在输出窗口中看到测试结果:

由于我们断言4=2+3,所以这个测试是通不过的,修改代码为:Xunit.Assert.Equal(4, 1+3);再次运行测试,我们会看到测试通过:

注意:按照步骤5配置,只需要选中方法名就能测试该方法。修改代码后要重新生成项目才能进行测试。

您还可以为刚才添加的“xUnit Test”菜单设置快捷键以快速运行测试。

xUnit使用方法

usage: xunit.console <assemblyFile> [configFile] [assemblyFile [configFile]...] [options]


Note: Configuration files must end in .config


Valid options:

  -parallel option     : set parallelization based on option

                       :   none - turn off all parallelization

                       :   collections - only parallelize collections

                       :   assemblies - only parallelize assemblies

                       :   all - parallelize assemblies & collections

  -maxthreads count    : maximum thread count for collection parallelization

                       :   0 - run with unbounded thread count

                       :   >0 - limit task thread pool size to 'count'

  -noshadow            : do not shadow copy assemblies

  -teamcity            : forces TeamCity mode (normally auto-detected)

  -appveyor            : forces AppVeyor CI mode (normally auto-detected)

  -nologo              : do not show the copyright message

  -quiet               do not show progress messages

  -wait                wait for input after completion

  -debug               : launch the debugger to debug the tests

  -serialize           : serialize all test cases (for diagnostic purposes only)

  -trait "name=value"  : only run tests with matching name/value traits

                       : if specified more than once, acts as an OR operation

  -notrait "name=value"  : do not run tests with matching name/value traits

                      : if specified more than once, acts as an AND operation

  -method "name"      : run a given test method (should be fully specified;

                      : i.e., 'MyNamespace.MyClass.MyTestMethod')

                      : if specified more than once, acts as an OR operation

  -class "name"       : run all methods in a given test class (should be fully

                      : specified; i.e., 'MyNamespace.MyClass')

                      : if specified more than once, acts as an OR operation

  -xml <filename>     : output results to xUnit.net v2 style XML file

  -xmlv1 <filename>   : output results to xUnit.net v1 style XML file

  -nunit <filename>   : output results to NUnit-style XML file

  -html <filename>    : output results to HTML file



您的昵称:*
QQ登录(无需注册直接登录可进行回复)
您的邮箱:(填写邮箱,如有回复可进行邮件通知)
验证码:
点击刷新