单元测试表格c#
我已经设计了一个表单应用程序,它本可以通过使用业务逻辑等更好地进行单元测试,但在这个阶段我不想修改我的代码。这是一款执行隐写术的应用程序,使用单元测试表格c#,c#,winforms,unit-testing,C#,Winforms,Unit Testing,我已经设计了一个表单应用程序,它本可以通过使用业务逻辑等更好地进行单元测试,但在这个阶段我不想修改我的代码。这是一款执行隐写术的应用程序,使用LSB算法将信息嵌入图像中。我目前正在尝试为按钮2单击事件编写单元测试。按下按钮2时;它将从另外两个文本框获取文本,并从图片框获取图像,然后运行LSB算法。下面是测试函数。我为相关的文本框创建测试值。运行测试时,我得到:System.NullReferenceException:Object reference未设置为对象的实例。这是否指对象发送者=nul
LSB算法将信息嵌入图像中。我目前正在尝试为按钮2单击事件编写单元测试。按下按钮2
时;它将从另外两个文本框
获取文本,并从图片框
获取图像,然后运行LSB算法
。下面是测试函数。我为相关的文本框创建测试值。运行测试时,我得到:System.NullReferenceException:Object reference未设置为对象的实例
。这是否指对象发送者=null代码>EventArgs e=null代码>。或者我正在做的事情可能吗?我必须求助于NUnitForms
?我在测试功能后添加了按钮2\u clic
k:
public void button2_ClickTest()
{
StegApp_Accessor target = new StegApp_Accessor();
// TODO: Initialize to an appropriate value
object sender = null; // TODO: Initialize to an appropriate value
EventArgs e = null; // TODO: Initialize to an appropriate value
target.textBox4.Text = "123456";
target.textBox5.Text = "test message";
target.button2_Click(sender, e);
//Assert.Inconclusive("A method that does not return a value cannot
// be verified.");
//target.textBox4.Text = "123456";
//target.textBox5.Text = "test message";
/*
if (target.textBox4.Text.Length > 6 || target.textBox4.Text.Length < 0)
{
Assert.Fail("Key is out of range");
}*/
//Assert.IsInstanceOfType(target.b1,typeof(byte[]));
if(target.b1.Length != target.temp4.Length)
{
Assert.Fail("B1 array does not have the correct lenght");
}
Assert.IsInstanceOfType(target.image1,typeof(Bitmap));
Assert.IsInstanceOfType(target.sb,typeof(StringBuilder));
if(target.sb.Length != target.tmp3.Length)
{
Assert.Fail("Issue with Stringbuilder sb. Lenght not equal to 'tmp3'!");
}
Assert.Equals(target.z,target.StringLenght);
Assert.Equals(target.c, target.textBox5.Text.Length);
}
`private void button2_Click(object sender, EventArgs e)
{
//int x1, y1, z = 0;
try
{
// Convert String Into Byte Array
//byte[] sourceData = System.Text.ASCIIEncoding.ASCII.GetBytes(a);
// Convert Each Byte Into A Binary String
//foreach (byte thisByte in sourceData)
// binaryString.Append(Convert.ToString(thisByte, 2));
while (!key)
{
if (textBox4.Text == "")
{
//b1 = ASCIIEncoding.ASCII.GetBytes(textBox5.Text);
key = false;
MessageBox.Show("Error, enter your six digit key!");
return;
}
else if (textBox4.Text.Length > 6)
{
MessageBox.Show("Error, Key too long, try again!");
return;
}
else
{
//temp4 = textBox4.Text[0] + textBox4.Text[1] + textBox4.Text[2] + textBox4.Text[3] + textBox4.Text[4] + textBox4.Text[5] + textBox5.Text;
c = textBox5.Text.Length;
temp5 = c.ToString();
if (c <= 9)
{
temp5 = "000" + temp5;
}
else if (c <= 99)
{
temp5 = "00" + temp5;
}
else if (c <= 999)
{
temp5 = "0" + temp5;
}
else if (c <= 9999)
{
}
else
{
MessageBox.Show("Message too long for this tool,try again");
return;
}
temp4 = textBox4.Text + temp5 + textBox5.Text;
b1 = ASCIIEncoding.ASCII.GetBytes(temp4);
key = true;
}
}
//byte[] b1 = ASCIIEncoding.ASCII.GetBytes(textBox5.Text);
//b1 = Encoding.Unicode.GetBytes(a);
//Create the array to be returned.
tmp2 = new string[b1.Length];
//Interate through each byte
for (int i = 0; i < b1.Length; i++)
{
int x = b1[i];
tmp = "";
while (true)
{
if ((x % 2) == 1)
{
tmp = "1" + tmp;
}
else
{
tmp = "0" + tmp;
}
x /= 2;
if (x < 1) break;
}
//Make sure the value is 8 chars long.
tmp2[i] = tmp.PadLeft(8, '0');
}
//string a="";
for (int i = 0; i < b1.Length; i++)
{
//a = tmp2[i];
tmp3 = tmp3 + tmp2[i];
}
if (key)
{
tmp3 = "00" + tmp3;
}
else
{
tmp3 = "10" + tmp3;
}
sb.Append(tmp3);
//temp5 = c.ToString();
//z= c+1;
StringLenght = sb.Length;
byte Mask0 = 254;
byte Mask1 = 1;
byte NewRed = 0, NewGreen = 0, NewBlue = 0;
// Loop through the images pixels to reset color.
for (x1 = 0, y1 = 0; x1 < image1.Width && z < StringLenght; x1++)
{
for (y1 = 0; y1 < image1.Height && z < StringLenght; y1++)
{
Color pixelColor = image1.GetPixel(x1, y1);
//byte NewRed, NewGreen, NewBlue;
if (sb[z] == '0')
{
NewRed = Convert.ToByte(pixelColor.R & Mask0);
Color newColor = Color.FromArgb(NewRed, pixelColor.G, pixelColor.B);
image1.SetPixel(x1, y1, newColor);
pixelColor = image1.GetPixel(x1, y1);
z++;
if (z == StringLenght)
{
break;
}
}
else
{
NewRed = Convert.ToByte(pixelColor.R | Mask1);
Color newColor = Color.FromArgb(NewRed, pixelColor.G, pixelColor.B);
image1.SetPixel(x1, y1, newColor);
pixelColor = image1.GetPixel(x1, y1);
z++;
if (z == StringLenght)
{
break;
}
}
if (sb[z] == '0')
{
NewGreen = Convert.ToByte(pixelColor.G & Mask0);
Color newColor = Color.FromArgb(pixelColor.R, NewGreen, pixelColor.B);
image1.SetPixel(x1, y1, newColor);
pixelColor = image1.GetPixel(x1, y1);
z++;
if (z == StringLenght)
{
break;
}
}
else
{
NewGreen = Convert.ToByte(pixelColor.G | Mask1);
Color newColor = Color.FromArgb(pixelColor.R, NewGreen, pixelColor.B);
image1.SetPixel(x1, y1, newColor);
pixelColor = image1.GetPixel(x1, y1);
z++;
if (z == StringLenght)
{
break;
}
}
if (sb[z] == '0')
{
NewBlue = Convert.ToByte(pixelColor.B & Mask0);
Color newColor = Color.FromArgb(pixelColor.R, pixelColor.G, NewBlue);
image1.SetPixel(x1, y1, newColor);
pixelColor = image1.GetPixel(x1, y1);
z++;
if (z == StringLenght)
{
break;
}
}
else
{
NewBlue = Convert.ToByte(pixelColor.B | Mask1);
Color newColor = Color.FromArgb(pixelColor.R, pixelColor.G, NewBlue);
image1.SetPixel(x1, y1, newColor);
pixelColor = image1.GetPixel(x1, y1);
z++;
if (z == StringLenght)
{
break;
}
}
//string binary1 = Convert.ToString(pixelColor.R, 2);
//char last1 = binary1[binary1.Length - 1];
}
}
MessageBox.Show("Message embedded");
//Color newColor = Color.FromArgb(NewRed, NewGreen, NewBlue);
//image1.SetPixel(x, y, newColor);
// Set the PictureBox to display the image.
//pictureBox1.Image = image1;
// Display the pixel format in Label1.
//label1.Text = "Pixel format: " + image1.PixelFormat.ToString();
}
catch (ArgumentException)
{
MessageBox.Show("There was an error." +
"Check the path to the image file.");
}
//pictureBox2.Image = image1;
//Byte[] buf = Encoding.Unicode.GetBytes(RetreivedMessage.ToString());
//Byte[] buf = Encoding.Unicode.GetBytes(RetreivedMessage.ToString());
//string result = System.Text.Encoding.Unicode.GetString(buf);
//String result = Encoding.Unicode.GetString(buf);
//StringBuilder r2 = new StringBuilder();
//foreach (Byte b in Encoding.Unicode.GetBytes(FinalRetreivedMessage))
//{
// r2.Append(Convert.ToString(b));
// }
//int v = 0;
//for (int i = 0; i < FinalRetreivedMessage.Length; i++)
// {
// v = v * 2 + (FinalRetreivedMessage[i] == '0' ? 0 : 1);
// }
//string result = v.ToString();
// copy the string as UTF-8 bytes.
// byte[] utf8Bytes = new byte[FinalRetreivedMessage.Length];
// for (int i = 0; i < FinalRetreivedMessage.Length; ++i)
// {
//Debug.Assert( 0 <= utf8String[i] && utf8String[i] <= 255, "the char must be in byte's range");
// utf8Bytes[i] = (byte)FinalRetreivedMessage[i];
// }
//Encoding.UTF8.GetString(utf8Bytes, 0, utf8Bytes.Length);
// utf8Bytes = new byte[]{1,1,1,0,1,0,0,0};
// string result1 = Encoding.UTF8.GetString(utf8Bytes, 0, utf8Bytes.Length);
//string result1 = Encoding.UTF8.GetString(utf8Bytes);
// UTF8Encoding enc = new UTF8Encoding();
// string str = enc.GetString(utf8Bytes);
// Byte[] encodedBytes = enc.GetBytes(FinalRetreivedMessage);
// string message = encodedBytes.ToString();
// int count = FinalRetreivedMessage.Length / 8;
// var bollox = new byte[count];
// for (int i = 0; i < count; i++)
// bollox[i] = Convert.ToByte(FinalRetreivedMessage.Substring(i * 8, 8), 2);
// var bollox1 = new byte[count];
//for (int i = 0; i < count; i++)
//bollox1[i] = Encoding.Unicode.GetBytes(FinalRetreivedMessage.Substring(i * 8, 8));
// string result2 = bollox.ToString();
// string result3 = enc.GetString(bollox);
// string result4 = System.Convert.ToString(bollox);
// string StringIWant = BitConverter.ToString(bollox);
// string result5 = BitConverter.ToString(encodedBytes);
// string result6 = BitConverter.ToString(utf8Bytes);
// string result7 = BitConverter.ToString(Encoding.Unicode.GetBytes(FinalRetreivedMessage));
// string result8 = System.Convert.ToString(Encoding.Unicode.GetBytes(FinalRetreivedMessage));
// string result9 = Encoding.Unicode.GetString(Encoding.Unicode.GetBytes(FinalRetreivedMessage));
// string result10 = Encoding.Default.GetString(Encoding.Unicode.GetBytes(FinalRetreivedMessage));
}`
public void按钮2\u ClickTest()
{
StegApp_访问器目标=新的StegApp_访问器();
//TODO:初始化为适当的值
object sender=null;//TODO:初始化为适当的值
EventArgs e=null;//TODO:初始化为适当的值
target.textBox4.Text=“123456”;
target.textBox5.Text=“测试消息”;
目标。按钮2\u单击(发件人,e);
//Assert.Inconclusive(“不返回值的方法不能
//被核实。”);
//target.textBox4.Text=“123456”;
//target.textBox5.Text=“测试消息”;
/*
如果(target.textBox4.Text.Length>6 | | target.textBox4.Text.Length<0)
{
Assert.Fail(“密钥超出范围”);
}*/
//Assert.IsInstanceOfType(target.b1,typeof(byte[]);
if(target.b1.Length!=target.temp4.Length)
{
Assert.Fail(“B1数组没有正确的长度”);
}
Assert.IsInstanceOfType(target.image1,typeof(位图));
Assert.IsInstanceOfType(target.sb,typeof(StringBuilder));
if(target.sb.Length!=target.tmp3.Length)
{
断言失败(“与Stringbuilder sb的问题长度不等于'tmp3'!”;
}
Assert.Equals(target.z、target.StringLength);
Assert.Equals(target.c、target.textBox5.Text.Length);
}
`私有无效按钮2\u单击(对象发送者,事件参数e)
{
//int-x1,y1,z=0;
尝试
{
//将字符串转换为字节数组
//byte[]sourceData=System.Text.ascienceoding.ASCII.GetBytes(a);
//将每个字节转换为二进制字符串
//foreach(源数据中的字节thisByte)
//Append(Convert.ToString(thisByte,2));
while(!key)
{
如果(textBox4.Text==“”)
{
//b1=ascienceoding.ASCII.GetBytes(textBox5.Text);
key=false;
Show(“错误,输入您的六位数密钥!”);
返回;
}
else if(textBox4.Text.Length>6)
{
Show(“错误,键太长,请重试!”);
返回;
}
其他的
{
//temp4=textBox4.Text[0]+textBox4.Text[1]+textBox4.Text[2]+textBox4.Text[3]+textBox4.Text[4]+textBox4.Text[5]+textBox5.Text;
c=textBox5.Text.Length;
temp5=c.ToString();
如果(cprivate void按钮2\u单击(对象发送者,事件参数e)
{
嵌入();
}
public void嵌入(字符串嵌入键、字符串嵌入消息、位图图像3)
{
//在图像中嵌入消息
}
公开测试()
{
StegApp target=new StegApp();//TODO:初始化为适当的值
字符串Embedkey=“123456”//TODO:初始化为适当的值
字符串EmbedMessage=“test2”//TODO:初始化为适当的值
位图image3=null;//TODO:初始化为适当的值
image3=新位图(@“C:\Users\Admin\Documents\dt265\Project\Sky\Sky and cloud.bmp”,true);
字符串a=“123456”,b=“test2”;
嵌入(Embedkey、EmbedMessage、image3);
//Assert.Inconclusive(“无法验证未返回值的方法”);
如果(Embedkey.Length>6 | | Embedkey.Length<0)
{
Assert.Fail(“密钥超出范围”);
}
//Assert.IsInstanceOfType(target.b1,typeof(byte[]);
if(target.b1.Length!=target.temp4.Length)
{
Assert.Fail(“B1数组没有正确的长度”);
}
Assert.IsInstanceOfType(target.image1,typeof(位图));
Assert.IsInstanceOfType(target.sb,typeof(StringBuilder));
if(target.sb.Length!=target.tmp3.Length)
{
断言失败(“与Stringbuilder sb的问题长度不等于'tmp3'!”;
}
if(target.z!=target.StringLength)
{
Assert.Fail(“z!=StringLenght”);
}
if(target.c!=embeddemessage.Length)
{
Assert.Fail(“c不是消息的长度!”);
}
}
在什么时候会出现异常?无法确定,因为我们看不到实际引发异常的代码。如果您使用调试器进行单元测试,您将知道哪行引发异常。您也没有说明如何创建表单,一个问题是您需要打开表单以使某些事情正常工作Ydoe看起来不重要,可能是你在那里的无数个参考资料中的一个,在其他地方贴花。从一个名为key的开始。如果你进行第一次测试,测试textbox4是否为空字符串,你会学到一些东西。
private void button2_Click(object sender, EventArgs e)
{
Embed();
}
public void Embed(string Embedkey, string EmbedMessage,Bitmap image3)
{
// embed message in image
}
public void EmbedTest()
{
StegApp target = new StegApp(); // TODO: Initialize to an appropriate value
string Embedkey = "123456"; // TODO: Initialize to an appropriate value
string EmbedMessage = "test2"; // TODO: Initialize to an appropriate value
Bitmap image3 = null; // TODO: Initialize to an appropriate value
image3 = new Bitmap(@"C:\Users\Admin\Documents\dt265\Project\Sky\sky-and-cloud.bmp",true);
string a="123456",b="test2";
target.Embed(Embedkey, EmbedMessage, image3);
//Assert.Inconclusive("A method that does not return a value cannot be verified.");
if (Embedkey.Length > 6 || Embedkey.Length < 0)
{
Assert.Fail("Key is out of range");
}
//Assert.IsInstanceOfType(target.b1,typeof(byte[]));
if(target.b1.Length != target.temp4.Length)
{
Assert.Fail("B1 array does not have the correct lenght");
}
Assert.IsInstanceOfType(target.image1,typeof(Bitmap));
Assert.IsInstanceOfType(target.sb,typeof(StringBuilder));
if(target.sb.Length != target.tmp3.Length)
{
Assert.Fail("Issue with Stringbuilder sb. Lenght not equal to 'tmp3'!");
}
if(target.z != target.StringLenght)
{
Assert.Fail("z != StringLenght");
}
if (target.c != EmbedMessage.Length)
{
Assert.Fail("c is not the lenght of the Message!");
}
}