要利用VS进行Code Coverage,你必须安装VSTS版本,找到VSTS安装目录Microsoft Visual Studio 8/Team Tools/Performance Tools,将这个目录加到你的环境变量PATH里。
第一步,进行Code Coverage,首先要instrument你的native binary文件。
vsintr -coverage target.exe
运行之后,该命令将会在目标binary文件中加入一些instrumentation代码用来收集运行信息。
第二步,然后启动coverage monitor来收集信息,并将输出结果到一个文件。
start vsperfmon -coverage -output:mytestrun.coverage
接下来,你可以运行你的程序代码或测试代码,并且运行结束后,你得告诉monitor你运行或测试结束,运行命令:
vsperfcmd -shutdown
这个命令将会等到目标进程结束,这时将有一个.coverage文件产生,如果用VS打开这个文件,你将会看到一个code coverage result窗口,里面有你刚才运行的结果。
现在我们简单结合Unit Test一个例子说明,在这里使用CxxTest测试框架,有关CxxTest测试框架,可以访问http://cxxtest.sourceforge.net/。
以下为UnitTest的Makefile文件:
TESTS = ./*.h 
PERL=perl 
# Where the CxxTest distribution is unpacked 
CXXTESTDIR = cxxtestinstallpath 
# Check CXXTESTDIR 
!if !exist($(CXXTESTDIR)cxxtestgen.pl) 
!error Please fix CXXTESTDIR 
!endif 
!include <win32.mak> 
# cxxtestgen needs Perl or Python 
!if defined(PERL) 
CXXTESTGEN = $(PERL) $(CXXTESTDIR)/cxxtestgen.pl 
!elseif defined(PYTHON) 
CXXTESTGEN = $(PYTHON) $(CXXTESTDIR)/cxxtestgen.py 
!else 
!error You must define PERL or PYTHON 
!endif 
all: runner.cpp runner.obj runner.exe binplace run 
# The arguments to pass to cxxtestgen 
# - ParenPrinter is the way MSVC likes its compilation errors 
# - --have-eh/--abort-on-fail are nice when you have them 
#CXXTESTGEN_FLAGS = --gui=Win32Gui --runner=ParenPrinter --have-eh --abort-on-fail 
CXXTESTGEN_FLAGS = --runner=ParenPrinter --have-eh --abort-on-fail 
# How to generate the test runner, "runner.cpp" 
runner.cpp: $(TESTS) 
$(CXXTESTGEN) $(CXXTESTGEN_FLAGS) -o $@ $(TESTS) 
runner.obj: runner.cpp 
$(CC) -EHsc $(cdebug) $(cflags) $(cvars) runner.cpp 
runner.exe: runner.obj 
$(link) $(ldebug) $(conflags) $*.obj target.lib /out:runner.exe 

binplace: runner.exe 
xcopy *.dll . /C /Y 

# Command-line arguments to the runner 
RUNNER_FLAGS = -title "CxxTest Runner" 
# How to run the tests, which should be in runner.exe 
run: binplace 
set PATH="C:/Program Files/Microsoft Visual Studio 8/Team Tools/Performance Tools";PATH 
vsinstr.exe -coverage target.dll 
start vsperfmon.exe -coverage -output:runner.coverage 
runner.exe $(RUNNER_FLAGS) 
vsperfcmd.exe -shutdown 
clean: 
del runner.* 
del *.dll
这样我们在每次build时候就能知道自己Unit Test的Code Coverage结果。
本文介绍如何使用Visual Studio Team System (VSTS) 进行代码覆盖率(CodeCoverage)测试。通过安装VSTS并设置环境变量,可以对本地二进制文件进行Instrumentation,收集测试过程中的代码覆盖数据。文章还提供了一个结合CxxTest单元测试框架的例子,展示了如何配置Makefile以自动获取单元测试的代码覆盖率。
1101

被折叠的 条评论
为什么被折叠?



