为了测试,写了一个简单的终端模拟类,用来和服务器通讯。然后构造测试的代码,经过半天的摸索,终于差不多了,比较高兴。中间被卡了一段时间,后来把收和发都放到独立与测试程序的线程中,问题解决了。
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.Threading;
namespace ComServer.DA
{
/// <summary>
/// connections的测试
/// </summary>
[TestFixture]
public class Test_Connections
{
int cnt;
Thread th;
Thread th2;
Test_SendClient ts;
DateTime dt0;
TimeSpan alltim; //耗时
public void mynoti(int typ, object[] args)
{
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss")+ args[1].ToString());
cnt++;
}
[SetUp] //初始化,产生接收和发送方
public void testsetup()
{
cnt = 0;
Connections cns = new Connections(5008, 500, 500, 3, new Dele_ConnsNotify(mynoti), 437, 437);
th = new Thread(cns.StartListen);
th.Start();
ts = new Test_SendClient();
th2 = new Thread(ts.DoTest);
th2.Start();
dt0 =new DateTime();
dt0 = DateTime.Now;
}
/// <summary>
/// 测试仅仅发送的效果,在10秒内,如果收到5个包,则测试通过。
/// </summary>
[Test]
public void t1()
{
alltim = DateTime.Now - dt0;
while (alltim.TotalSeconds < 10)
{
Console.WriteLine("time:" + alltim.ToString()+"\r\ncnt:"+cnt.ToString());
Thread.Sleep(1000);
alltim = DateTime.Now - dt0;
}
Assert.AreEqual(cnt,5);
Console.WriteLine("测试(10s内收5个包)结束!");
}
[TearDown]
public void t1teardown()
{
th.Abort();
ts.Exit();
th2.Abort();
}
}
}
几点心得:using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.Threading;
namespace ComServer.DA
{
/// <summary>
/// connections的测试
/// </summary>
[TestFixture]
public class Test_Connections
{
int cnt;
Thread th;
Thread th2;
Test_SendClient ts;
DateTime dt0;
TimeSpan alltim; //耗时
public void mynoti(int typ, object[] args)
{
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss")+ args[1].ToString());
cnt++;
}
[SetUp] //初始化,产生接收和发送方
public void testsetup()
{
cnt = 0;
Connections cns = new Connections(5008, 500, 500, 3, new Dele_ConnsNotify(mynoti), 437, 437);
th = new Thread(cns.StartListen);
th.Start();
ts = new Test_SendClient();
th2 = new Thread(ts.DoTest);
th2.Start();
dt0 =new DateTime();
dt0 = DateTime.Now;
}
/// <summary>
/// 测试仅仅发送的效果,在10秒内,如果收到5个包,则测试通过。
/// </summary>
[Test]
public void t1()
{
alltim = DateTime.Now - dt0;
while (alltim.TotalSeconds < 10)
{
Console.WriteLine("time:" + alltim.ToString()+"\r\ncnt:"+cnt.ToString());
Thread.Sleep(1000);
alltim = DateTime.Now - dt0;
}
Assert.AreEqual(cnt,5);
Console.WriteLine("测试(10s内收5个包)结束!");
}
[TearDown]
public void t1teardown()
{
th.Abort();
ts.Exit();
th2.Abort();
}
}
}
1。有时Testdriven.net的console输出不是很及时,该显示的却不显示,在Nunit GUI中基本上不会出问题。
2。多线程的情况下,Nunit GUI关闭后,有个延时,估计是等待线程结束。
3、Testdriven.net对于Nunit支持的很好,测试时,能够自动运行Test、Teardown等标签。不过我的右键菜单中的Nunit一会儿有一会儿没有,还没搞懂。
4、Testdriven.net的debug很不错,基本上我就很少用vs的调试。