单元测试表单 c#

本文关键字:表单 单元测试 | 更新日期: 2023-09-27 17:54:29

我设计了一个表单应用程序,通过使用业务逻辑等可以更好地设计用于单元测试,但在这个阶段我不想更改我的代码。 它是一个执行隐写术的应用程序,其中消息使用LSB algorithm嵌入图像中。我目前正在尝试为button2 click event编写单元测试。按下button2时;它将从另外两个textboxes中获取文本,从picturebox中获取图像并运行LSB algorithm 。下面是测试功能。 我为相关textboxes创建测试值。当我运行测试时,我得到:System.NullReferenceException: Object reference not set to an instance of an object。这是指object sender = null; EventArgs e = null;. 或者我正在做的事情甚至可能吗? 我必须求助于NUnitForms吗? 我在测试函数后添加了 button2_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));
    }`

单元测试表单 c#

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!");
        }
    }