Click here to Skip to main content
14,362,927 members
Rate this:
Please Sign up or sign in to vote.
See more:
i have an assignment to unit test a login page. iam new to programming please help me
this is my code. i need to unit test in visualstudio

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Display();
        }


        private void Display()
        {
            if (textBox1.Text == "admin" && textBox2.Text == "admin")
                MessageBox.Show("login success");
            else
                MessageBox.Show("try again");
        }
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

Well, this is a fairly large question and goes to how you have put together your application. I'm afraid this is going to get advanced fairly quickly.

The big question is what do you mean by unit testing? Are you talking about automation testing, whereby you run the program and a script puts values into textboxes and presses buttons (and the like)? If you are, then I would suggest that you use NUnitForms[^] to manage this?

If, however, you are talking about testing the logic internally, then you really need to change the architecture considerably. As that's a very large topic, I won't go into it here unless you indicate that this is the approach you want to take - in which case, I'll expect to see a comment from you to that effect.
   
Comments
Pete O'Hanlon 2-Nov-12 7:10am
   
You know, I got that from when you stated that you were testing in Visual Studio in your question. I understand you're using Visual Studio, but do you mean you want to test the GUI or the business logic?
Member 9553202 2-Nov-12 7:16am
   
dont know
Member 9553202 2-Nov-12 7:11am
   
[TestMethod()]
[DeploymentItem("WindowsFormsApplication2.exe")]
public void button1_ClickTest()
{
Form1_Accessor target = new Form1_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.button1_Click(sender, e);
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}
Member 9553202 2-Nov-12 7:12am
   
this is wat iam getting in test
what are the values to b initialised
Pete O'Hanlon 2-Nov-12 7:18am
   
As you aren't actually using those values anywhere, you don't actually need to set them to anything. However, you are attempting to show a message box in there, and this is going to cause you a problem when testing. Basically, your test will wait for you to click the message box. Also, you will need to set the textbox values.
Member 9553202 2-Nov-12 7:41am
   
how should i set
Member 9553202 2-Nov-12 7:58am
   
thank u
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi Friend,

In Above code nothing is there to test.

when you click on button, "Display()" method will be called.

In Display method they are cheeking the details entered by user.

if Values entered in two text boxes are "admin" - display a message "Login Success"

else "Login failed".


Regards,
Prasad
   
Comments
Pete O'Hanlon 2-Nov-12 6:27am
   
Of course there's something to test. There is "business" logic behind the button, so he's right to think it should be tested. I am apalled that you think this isn't worthy of testing. What about if the rules change to expect "admin" to be "flurble"? If he's forgotten to change this code then the application logic is no longer right.
Prasad Guduri 2-Nov-12 6:53am
   
Hi,

My words mean that "its not difficult to test"....!!!!
Member 9553202 2-Nov-12 7:12am
   
[TestMethod()]
[DeploymentItem("WindowsFormsApplication2.exe")]
public void button1_ClickTest()
{
Form1_Accessor target = new Form1_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.button1_Click(sender, e);
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}
Member 9553202 2-Nov-12 7:12am
   
this is wat iam getting in test
what are the values to b initialised

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100