实践篇(2)--自动化集成学习
下面我举个例子来教大家在实际项目中开展自动化集成(本人也在学习中,还有一些问题):
1) 下载Nant0.85,在环境变量Path中增加Nant\bin的实际目录。
2) 写一个类及其测试类,放到src目录下
自动化编译和测试,建立default.build:
运行结果:除了单元测试的没有显示外,其他都可以,包括最后的打包工作。
我注解掉的自动CVS操作和邮件发送,才具有真正的价值,留给读者去练习吧。
下载练习包:
执行:在DOS窗口下进入练习目录,打入nant .
好了,可以去喝茶轻松一下,回来可以看到结果。
alex
1) 下载Nant0.85,在环境变量Path中增加Nant\bin的实际目录。
2) 写一个类及其测试类,放到src目录下
using System;
namespace Auto
{
public class Account {
private string _name;
private int _balance;
public Account(string name,int bal)
{
_name=name;
_balance=bal;
}
public void draw(int m)
{
if(m>_balance) throw new Exception("not money!");
_balance-=m;
}
public int Balance
{
get {return _balance;}
}
}
}
namespace Auto
{
public class Account {
private string _name;
private int _balance;
public Account(string name,int bal)
{
_name=name;
_balance=bal;
}
public void draw(int m)
{
if(m>_balance) throw new Exception("not money!");
_balance-=m;
}
public int Balance
{
get {return _balance;}
}
}
}
using System;
using NUnit.Framework;
namespace Auto
{
[TestFixture]
public class AccountTest {
private Account acc;
[SetUp]
public void Init() {
acc=new Account("lsj",100);
}
[TearDown]
public void Finish() {
}
[Test] public void OKTest() {
acc.draw(50);
Assert.AreEqual(50,acc.Balance);
}
[Test] public void ErrorTest() {
acc.draw(150);
Assert.AreEqual(100,acc.Balance);
}
}
}
using NUnit.Framework;
namespace Auto
{
[TestFixture]
public class AccountTest {
private Account acc;
[SetUp]
public void Init() {
acc=new Account("lsj",100);
}
[TearDown]
public void Finish() {
}
[Test] public void OKTest() {
acc.draw(50);
Assert.AreEqual(50,acc.Balance);
}
[Test] public void ErrorTest() {
acc.draw(150);
Assert.AreEqual(100,acc.Balance);
}
}
}
自动化编译和测试,建立default.build:
<?xml version="1.0"?>
<project name="NUnit Integration" default="test">
<property name="src_dir" value="src" />
<property name="basename" value="account"/>
<property name="debug" value="false"/>
<target name="clean" >
<delete>
<fileset>
<include name="bin/*.dll"/>
</fileset>
</delete>
</target>
<target name="buildPrd" depends="clean" >
<mkdir dir="bin" />
<csc target="library" output="bin/${basename}.dll" debug="${debug}">
<sources>
<include name="${src_dir}/${basename}.cs" />
</sources>
</csc>
</target>
<target name="test" depends="buildPrd">
<csc target="library" output="bin/${basename}-test.dll" debug="${debug}">
<sources>
<include name="${src_dir}/${basename}-test.cs" />
</sources>
<references>
<include name="bin/${basename}.dll" />
<include name="C:\Program Files\NUnit 2.2\bin\nunit.framework.dll" />
</references>
</csc>
<zip zipfile="backup.zip">
<fileset basedir="${src_dir}" prefix="src">
<include name="**/*" />
</fileset>
</zip>
<nunit2>
<formatter type="Plain" />
<test assemblyname="bin/${basename}-test.dll" />
</nunit2>
</target>
<!--
<zip zipfile="backup.zip">
<fileset basedir="${bin.dir}" prefix="bin">
<include name="**/*" />
</fileset>
<fileset basedir="${doc.dir}" prefix="doc">
<include name="**/*" />
</fileset>
</zip>
<mail
from="nant@sourceforge.net"
tolist="recipient1@sourceforge.net"
cclist="recipient2@sourceforge.net"
bcclist="recipient3@sourceforge.net"
subject="Msg 7: With attachments"
mailhost="smtpserver.anywhere.com">
<files>
<include name="*.txt" />
</files>
<attachments>
<include name="*.zip" />
</attachments>
</mail>
<cvs-checkout
destination="c:\src\nant"
cvsroot=":pserver:anonymous@cvs.sourceforge.net:/cvsroot/nant"
password=""
module="nant"
revision="0_85"
overridedir="v0.85">
</cvs-checkout>
-->
</project>
注意要修改C:\Program Files\NUnit 2.2\bin\nunit.framework.dll成你自己的实际引用<project name="NUnit Integration" default="test">
<property name="src_dir" value="src" />
<property name="basename" value="account"/>
<property name="debug" value="false"/>
<target name="clean" >
<delete>
<fileset>
<include name="bin/*.dll"/>
</fileset>
</delete>
</target>
<target name="buildPrd" depends="clean" >
<mkdir dir="bin" />
<csc target="library" output="bin/${basename}.dll" debug="${debug}">
<sources>
<include name="${src_dir}/${basename}.cs" />
</sources>
</csc>
</target>
<target name="test" depends="buildPrd">
<csc target="library" output="bin/${basename}-test.dll" debug="${debug}">
<sources>
<include name="${src_dir}/${basename}-test.cs" />
</sources>
<references>
<include name="bin/${basename}.dll" />
<include name="C:\Program Files\NUnit 2.2\bin\nunit.framework.dll" />
</references>
</csc>
<zip zipfile="backup.zip">
<fileset basedir="${src_dir}" prefix="src">
<include name="**/*" />
</fileset>
</zip>
<nunit2>
<formatter type="Plain" />
<test assemblyname="bin/${basename}-test.dll" />
</nunit2>
</target>
<!--
<zip zipfile="backup.zip">
<fileset basedir="${bin.dir}" prefix="bin">
<include name="**/*" />
</fileset>
<fileset basedir="${doc.dir}" prefix="doc">
<include name="**/*" />
</fileset>
</zip>
from="nant@sourceforge.net"
tolist="recipient1@sourceforge.net"
cclist="recipient2@sourceforge.net"
bcclist="recipient3@sourceforge.net"
subject="Msg 7: With attachments"
mailhost="smtpserver.anywhere.com">
<files>
<include name="*.txt" />
</files>
<attachments>
<include name="*.zip" />
</attachments>
</mail>
<cvs-checkout
destination="c:\src\nant"
cvsroot=":pserver:anonymous@cvs.sourceforge.net:/cvsroot/nant"
password=""
module="nant"
revision="0_85"
overridedir="v0.85">
</cvs-checkout>
-->
</project>
运行结果:除了单元测试的没有显示外,其他都可以,包括最后的打包工作。
我注解掉的自动CVS操作和邮件发送,才具有真正的价值,留给读者去练习吧。
下载练习包:
执行:在DOS窗口下进入练习目录,打入nant .
好了,可以去喝茶轻松一下,回来可以看到结果。
alex