您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > Nunit
使用NUnit進(jìn)行并發(fā)測試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2013/3/26 17:02:30 ] 推薦標(biāo)簽:

下面的代碼將演示如何使用NUnit進(jìn)行并發(fā)測試,啟動多線程插入多個數(shù)據(jù),并在插入完數(shù)據(jù)后刪除數(shù)據(jù)

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using WYEng.SQLServerDAL;
using System.Threading;
using System.Data;
using System.Data.SqlClient;

namespace WYEng.TestUnit
...{
[TestFixture]
public class ExamComputerInfo
...{
SQLServerDAL.ExamComputerInfo computerInfo;
WYEng.Model.ExamComputerInfo mCompuerInfo;
private int j = 0;
private int computerID = 0;
private string PARAM_COMPUTERID = "@ComputerID";
[SetUp]
public void CreateObject()
...{
computerInfo = new WYEng.SQLServerDAL.ExamComputerInfo();
mCompuerInfo = new WYEng.Model.ExamComputerInfo();
TestGetComputerID();
}

[TearDown]
public void DeleteObject()
...{
TestDeleteComputerInfo();
computerInfo = null;
mCompuerInfo = null;

}
private void TestGetComputerID()
...{
computerID = XPWY.DBUtility.DBHelperSQL.GetMaxID("ComputerID", "ExamComputerInfo");
}
private void TestDeleteComputerInfo()
...{
Console.WriteLine(computerID.ToString());
string strSql = "DELETE FROM ExamComputerInfo WHERE ComputerID>=@ComputerID";
SqlParameter pram = new SqlParameter(PARAM_COMPUTERID, SqlDbType.Int);
pram.Value = computerID;
XPWY.DBUtility.DBHelperSQL.ExecuteSql(strSql,pram);
}
[Test]
[Ignore("為了測試并發(fā)插入的問題,屏蔽該方法")]
public void GetComputerInfo()
...{
Thread[] threads = new Thread[1000];
for (int i = 0; i < 1000; i++)
...{
Thread t = new Thread(new ThreadStart(Func));
threads[i] = t;
}
for (int i = 0; i < 1000; i++)
...{
threads[i].Start();
}


}
private void Func()
...{
mCompuerInfo = computerInfo.GetComputerInfo(1);
Assert.AreEqual(mCompuerInfo.ComputerName, "YMF-XPWY");
}
[Test]
public void GetComputerInfoByMAC()
...{
mCompuerInfo = computerInfo.GetComputerInfoByMAC("00:1B:FC:64:6C:A3");
Assert.AreEqual(mCompuerInfo.ComputerName, "YMF-XPWY");//正常條件
mCompuerInfo = computerInfo.GetComputerInfoByMAC("00:sdfsdfsdf");
Assert.AreEqual(mCompuerInfo, null);//邊界條件
}

[Test]
public void TestInsertCPInfoWhileMorePeoper()
...{
Thread[] threads = new Thread[1000];
for (int i = 0; i < 1000; i++)
...{
j++;
threads[i] = new Thread(new ThreadStart(TestInsertCPInfo));
threads[i].Start();
}
}
private void TestInsertCPInfo()
...{
mCompuerInfo.AudioDapater = j.ToString();
mCompuerInfo.BrowserInfo = j.ToString();
mCompuerInfo.ComputerName = "fw";
mCompuerInfo.EthernetAddress = "asdsad";
mCompuerInfo.ExamSeatNo = j;
mCompuerInfo.IpAddress = "192.168.0.1";
mCompuerInfo.IsTestPassed = true; ;
mCompuerInfo.NotPassedError = "yes";
mCompuerInfo.OSName = "OSName";
mCompuerInfo.OSVersion = "1.0.1";
computerInfo.InsertCPInfo(mCompuerInfo);
}
}
}

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd