김씨의 IT 이야기

cloud

Posts Tagged ‘팀빌드

Team Build + Teamprise + Ant + Junit를 이용한 테스트 결과 수집

leave a comment »

TFS와 Visual Studio를 이용한 단위 테스트는 많은 예제를 찾아 볼 수 있다.(그런가 -_-? 없던데)
상대적으로 Java와 TFS의 연계 부분에 대한 이야기가 없는 것 같아서 포스팅을 한다.

이번에 진행하는 것은 팀빌드를 통해서 Ant와 Junit을 통해서 자바 프로젝트를 빌드하고 단위 테스트 하는 것이다. 물론 단위 테스트에 대한 결과를 TFS에 반영 시키는 것도 포함된다.

먼저 준비해야 할 것 들이 있다.

– 팀프라이즈 빌드 익스텐션 : http://www.teamprise.com/products/build/
– JDK : https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewProductDetail-Start?ProductRef=jdk-6u14-oth-JPR@CDS-CDS_Developer
-이클립스 : http://www.eclipse.org/downloads/
-이클립스에서 Junit 설치 방법 : http://www.kkangsdoc.com/zbxe/?mid=eclipse&page=2&document_srl=966
-TFS 2008 sp1 (team build)

위와 같이 설치를 합니다. 설치 순서 등은 이번 게시물의 범위에서 벗어나므로 생략 하도록 하겠습니다.
설치 시 주의 사항은 JAVA_HOME을 path에 등록해 주는 것입니다. 이클립스 설치 및 Junit 사용 시에 필요한 부분이니 놓치치 않도록 하세요.

팀빌드가 설치된 머신에 팀프라이즈 빌드 익스텐션(이하 TBE)를 설치 한다.
이클립스에서 자바 프로젝트를 생성한다.
이클립스 메뉴에서 window >> Preferences 에서 Ant를 선택하고 Names에 build.xml을 입력한다.
자바 프로젝트에서 build.xml을 생성하여서 빌드 스크립트를 입력한다.
빌드 스크립트 내에서 junit가 실행된 뒤에 리포트가 생성되는 경로와 파일명 등을 정의 한다.

<target name=”test” depends=”compile”>
<mkdir dir=”report/html”/>
<junit printsummary=”on” errorproperty=”testsfailed”>
<classpath refid=”test.classpath”> </classpath>
<formatter type=”xml”/>
<batchtest todir=”report”>
<fileset dir=”dst”> <include name=”test/UnitTest.class”/> </fileset>
</batchtest>
</junit>
<junitreport todir=”report”>
<fileset dir=”report”> <include name=”TEST-*.xml”/> </fileset>
<report format=”frames” todir=”report/html”/>
</junitreport>
<copy todir=”${TestResultsRoot}”>
<fileset dir=”report” />
</copy>

</target>

위와 같이 테스트 타겟을 입력하면 ant가 실행 될 때 junit가 실행되고 해당 테스트 결과를 report 폴더에 TEST-*.xml이란 이름으로 생성한다. 추가로 입력한 부분은 테스트가 끝난 뒤 테스트 결과가 있는 report폴더의 값들을 ${TestResultRoot}로 복사하도록 한 것이다. 이렇게 해야지 TFS에서 Junit에서 생성된 테스트 결과를 인식할 수 있다.
report_dir
빌드를 실행하면 위 그림처럼 파일들이 생성된다.
팀프라이즈에서 자바 프로젝트의 팀빌드 스크립트 정의를 생성한다.

<?xml version=”1.0″ encoding=”utf-8″?>
<Project DefaultTargets=”DesktopBuild” xmlns=”http://schemas.microsoft.com/developer/msbuild/2003&#8243; ToolsVersion=”3.5″>
<Import Project=”$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets” />
<Import Project=”$(MSBuildExtensionsPath)\Teamprise\v2\Teamprise.Build.Ant.targets” />
<ProjectExtensions>
<ProjectFileVersion>2</ProjectFileVersion>
<Description></Description>
<BuildMachine>tb</BuildMachine>
</ProjectExtensions>
<PropertyGroup>
<TeamProject>Demo</TeamProject>
<BuildDirectoryPath>UNKNOWN</BuildDirectoryPath>
<DropLocation>\\UNKNOWN\drops</DropLocation>
<SkipWorkItemCreation>false</SkipWorkItemCreation>
<WorkItemType>Bug</WorkItemType>
<WorkItemFieldValues>System.Reason=Build Failure;System.Description=Start the build using Team Build</WorkItemFieldValues>
<WorkItemTitle>Build failure in build:</WorkItemTitle>
<DescriptionText>This work item was created by Team Build on a build failure.</DescriptionText>
<BuildlogText>The build log file is at:</BuildlogText>
<ErrorWarningLogText>The errors/warnings log file is at:</ErrorWarningLogText>
<UpdateAssociatedWorkItems>true</UpdateAssociatedWorkItems>
</PropertyGroup>
<ItemGroup>
<AntBuildFile Include=”$/Demo/junit4/build.xml”>
<debug>true</debug>
<Targets>test</Targets>
<Properties>BinariesRoot=$(BinariesRoot);BuildDefinitionName=$(BuildDefinitionName);BuildDefinitionUri=$(BuildDefinitionUri);BuildDirectory=$(BuildDirectory);BuildNumber=$(BuildNumber);DropLocation=$(DropLocation);LogLocation=$(LogLocation);SourceGetVersion=$(SourceGetVersion);TestResultsRoot=$(TestResultsRoot);TeamProject=$(TeamProject);WorkspaceName=$(WorkspaceName);WorkspaceOwner=$(WorkspaceOwner)</Properties>
<Lib></Lib>
</AntBuildFile>
<JUnitLogFiles Include=”$(TestResultsRoot)\TEST-*.xml” />
</ItemGroup>

</Project>

위와 같이 빌드 스크립트가 생성된다. 팀프라이즈에서 빌드 스크립트를 생성하게 되면 ant와 연동되는 빌드 스크립트를 생성할 수 있다.
자세한 빌드 스크립트 만들기는 여기를 참조하기 바란다.

자세히 볼 것은 JUnitLogFiles 의 경로이다. build.xml에서 정의한 것처럼 테스트 결과를 TestResultRoot로 복사하게 한 뒤에, 이 결과값을 JUnitLogFiles 라는 부분에서 이를 인식하여서 Junit의 결과를 TFS가 읽을 수 있도록 변환해 주는 작업을 담당하게 되는 것이다.

팀프라이즈의 빌드 스크립트는 Junit와 연동되는 부분을 고려하여 만들어 졌기 때문에 위와 같은 것이 가능합니다. 물론 이를 위해서는 팀빌드가 설치된 머신에 팀프라이즈 빌드 익스텐션이 설치되어서 Junit의 테스트 결과값을 컨버팅해 줄 수 있어야 합니다.

참고자료
http://download.cdn.teamprise.com/tbe/1.2.0.397/TeampriseBuildExtensions.pdf

Written by moris Kim

2009/08/05 at 10:47