Click here to Skip to main content
15,749,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

C#
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

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.
 
Share this answer
 
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.
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
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900