自己动手定制NUnit(三):整合ncover生成代码覆盖率报表(zhuan)

 

作为软件质量保证的重要一环,测试代码覆盖率是评判单元测试的重要标准。既然已经为你的代码编写了自动回归的单元测试,你当然想知道是不是所有的代码都测到了,完整的测试代码覆盖率可以说是零缺陷软件的决定性因素之一。

说到.net下的代码覆盖统计软件,当属NCover大名鼎鼎了,我们将其和NAant,Nunit一起配合使用,在每日构建和自动化测试领域是非常方便的,正如我前面所说的,对许多程序员而言,还是集成到一起比较方便使用,方便快捷地调用我想要的功能。因此,我将Ncover集成到了我的Nunit中。

由于现在的开发还是基于fw1.1,所以我也只用版本较低的NCover1.3.3,目前使用倒是没发现什么问题。不知道什么原因,在NCover的网站上没有找到1.3版的源代码,我只有反编译一下用,毕竟直接分析和修改源码还是方便很多。在NCover体系内,最重要的组件是Coverlib.dll,他是一个com组件,需要用regsvr32进行注册使用,当然如果你使用安装包,会自动进行安装的。

那么.net部分做了些什么呢?看看我调用Ncover实现功能的代码:

None.gifProfilerSettings ps = new ProfilerSettings();
None.gif
None.gif                   ps.CommandLineExe = "nunit-console.exe";
None.gif
None.gif                   ps.CommandLineArgs = this._testLoader.TestFileName;
None.gif
None.gif                   ps.CoverageFile = Path.Combine(new FileInfo(this._testLoader.TestFileName).Directory.FullName,"coverage.xml");
None.gif
None.gif                   ProfilerDriver driver1 = new ProfilerDriver(ps);
None.gif
None.gif                   driver1.Start();
None.gif
None.gif                   driver1.WaitForExit();
None.gif
None.gif              System.Diagnostics.Process.Start(ps.CoverageFile);
None.gif

NCover必须要传入一个可执行文件来进行检查哪些代码被调用了,被检查的引用组件必须有pdb调试文件,否则是不分析。在上面的代码中,我传入nunit的控制台程序,让他跑一遍当前的测试dll,然后设置输出文件到被测试的组件的目录下,开启nunit-console进程,待进程结束后,就会有一个converage.xml文件产生,浏览此文件就能看到统计结果。

这中间都发生了什么,究竟是谁完成了这一切?.net部分的代码也没什么特别的地方:

None.gifpublic void Start()
None.gif
ExpandedBlockStart.gif {
InBlock.gif
InBlock.gif              ProcessStartInfo info1 = new ProcessStartInfo(this._ps.CommandLineExe, this._ps.CommandLineArgs);
InBlock.gif
InBlock.gif            info1.UseShellExecute = false;
InBlock.gif
InBlock.gif            info1.ErrorDialog = false;
InBlock.gif
InBlock.gif              info1.CreateNoWindow = true;
InBlock.gif
InBlock.gif              info1.RedirectStandardOutput = true;
InBlock.gif
InBlock.gif              info1.RedirectStandardInput = true;
InBlock.gif
InBlock.gif              info1.RedirectStandardError = true;
InBlock.gif
InBlock.gif if (this._ps.WorkingDirectory != null)
InBlock.gif
ExpandedSubBlockStart.gif {
InBlock.gif
InBlock.gif                info1.WorkingDirectory = this._ps.WorkingDirectory;
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif this.SetEnvironment(info1.EnvironmentVariables);
InBlock.gif
InBlock.gif this._proc = Process.Start(info1);
InBlock.gif
ExpandedBlockEnd.gif         }
None.gif
None.gif

我对ncover的代码作了些改动,使新进程在后台运行,否则一运行nunit-console就弹出来一个命令行,很是不爽。从上面的代码可以看出系统仅仅设置了下进程初始状态和环境变量,就完成了覆盖统计和xml文件生成。来看看环境变量的配置代码:

None.gif private void SetEnvironment(StringDictionary env)
None.gif
ExpandedBlockStart.gif {
InBlock.gif
InBlock.gif            env["Cor_Enable_Profiling"] = "1";
InBlock.gif
InBlock.gif            env["Cor_Profiler"] = "CvrLib.CoverageProfiler";
InBlock.gif
InBlock.gif            env["CoverageAssemblies"] = this._ps.Assemblies;
InBlock.gif
InBlock.gif            env["CoverageLog"] = this._ps.LogFile;
InBlock.gif
InBlock.gif            env["CoverageXml"] = this._ps.CoverageFile;
InBlock.gif
InBlock.gif if (this._ps.Debug)
InBlock.gif
ExpandedSubBlockStart.gif {
InBlock.gif
InBlock.gif                env["CoverageDebug"] = "1";
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif if (this._ps.VerboseLog)
InBlock.gif
ExpandedSubBlockStart.gif {
InBlock.gif
InBlock.gif                env["CoverageVerbose"] = "1";
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif if (this._ps.DisableSetIL)
InBlock.gif
ExpandedSubBlockStart.gif {
InBlock.gif
InBlock.gif                env["CoverageDisableSetIL"] = "1";
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif if (this._ps.JITLog)
InBlock.gif
ExpandedSubBlockStart.gif {
InBlock.gif
InBlock.gif                env["CoverageLogJit"] = "1";
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif if (this._ps.NoLog)
InBlock.gif
ExpandedSubBlockStart.gif {
InBlock.gif
InBlock.gif                env["CoverageNoLog"] = "1";
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
ExpandedBlockEnd.gif        }
None.gif
None.gif

其实这部分代码就是把相关变量存起来,好让真正的负责人-ConverLib.dll来进行处理。ConverLib.dll负责根据环境变量的值来进行代码的跟踪和记录,生成相关的XML文件。为了使xml更可读,需要把一个xsl文件也自动拷过去。

最后,我们在tools菜单上添加一项查看代码覆盖率报表:

转载于:https://www.cnblogs.com/pursue/archive/2009/08/25/1553267.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值