多的也不說了授段,直接上代碼,如果對于基礎(chǔ)的斷言什么不了解可以看我之前的C/C++單元測試文章番甩。
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
string FilePath = @"這里寫你自己的ini文件地址";
string Section = "Number";
string Key = "number";
string KeyOfSampleName = "Input";
string KeyOfSampleName1 = "Output";
int thenum = GetPrivateProfileInt(Section, Key, 0, FilePath);
for (int index = 1; index <= thenum; index++)
{
//解析編號
string ExamStr = index.ToString();
//解析字符串Intput后面的內(nèi)容
string s = Read(ExamStr, KeyOfSampleName, "", FilePath);
string[] condition = { " " };
//如果在拆分時不需要包含空字符串侵贵,則可以使用 StringSplitOptions.RemoveEmptyEntries 選項(xiàng)
//例如在上例中將 StringSplitOptions.None 更改成 StringSplitOptions.RemoveEmptyEntries
string[] result = s.Split(condition, StringSplitOptions.None);//以空格拆分
byte[] byteArray = new byte[result.Length];
for (int i = 0; i < result.Length; i++)
{
//將16進(jìn)制的string轉(zhuǎn)換為byte
byteArray[i] = byte.Parse(result[i], System.Globalization.NumberStyles.HexNumber);
}
//解析字符串Output后面的內(nèi)容
string output = Read(ExamStr, KeyOfSampleName1, "", FilePath);
string[] resultOutput = output.Split(condition, StringSplitOptions.None);
byte[] byteArray1 = new byte[resultOutput.Length];
for (int i = 0; i < resultOutput.Length; i++)
{
byteArray1[i] = byte.Parse(resultOutput[i],System.Globalization.NumberStyles.HexNumber);
}
//斷言
Assert.IsTrue(true);
}
}
/// <summary>
/// 為INI文件中指定的節(jié)點(diǎn)取得整數(shù)
/// </summary>
/// <param name="lpAppName">欲在其中查找關(guān)鍵字的節(jié)點(diǎn)名稱</param>
/// <param name="lpKeyName">欲獲取的項(xiàng)名</param>
/// <param name="nDefault">指定的項(xiàng)沒有找到時返回的默認(rèn)值</param>
/// <param name="lpFileName">INI文件完整路徑</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);
/// <summary>
/// 為INI文件中指定的節(jié)點(diǎn)取得字符串
/// </summary>
/// <param name="lpAppName">欲在其中查找關(guān)鍵字的節(jié)點(diǎn)名稱</param>
/// <param name="lpKeyName">欲獲取的項(xiàng)名</param>
/// <param name="lpDefault">指定的項(xiàng)沒有找到時返回的默認(rèn)值</param>
/// <param name="lpReturnedString">指定一個字串緩沖區(qū),長度至少為nSize</param>
/// <param name="nSize">指定裝載到lpReturnedString緩沖區(qū)的最大字符數(shù)量</param>
/// <param name="lpFileName">INI文件完整路徑</param>
/// <returns>復(fù)制到lpReturnedString緩沖區(qū)的字節(jié)數(shù)量缘薛,其中不包括那些NULL中止字符</returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
/// <summary>
/// 讀取INI文件值
/// </summary>
/// <param name="section">節(jié)點(diǎn)名</param>
/// <param name="key">鍵</param>
/// <param name="def">未取到值時返回的默認(rèn)值</param>
/// <param name="filePath">INI文件完整路徑</param>
/// <returns>讀取的值</returns>
public static string Read(string section, string key, string def, string filePath)
{
StringBuilder sb = new StringBuilder(1024);
GetPrivateProfileString(section, key, def, sb, 1024, filePath);
string s = sb.ToString();
return s;
}
}
}