3

I am new to asp, C#,nunit. I am trying to write (do my homework/assignment) nunit testcases for a simple web form, that doesn't have any business logic.

The web form has 4 text boxes (Name, emailaddress, password, phone number) , a radio list , drop down list (gender). The form just validates the data and shows the data in an other page as output text (labels). Validation is just regEx validation for email, phone. length validation for name and pwd. requiredField validation for gender.

Please suggest how do I write NUnit test case for this web form

Example, if there a method that calculates a factorial, I call the method first with a parameter given in code (ex: 4). It returns me 4! into a variable 'result' (that is result has 24 now). Then I will call AssertEquals(24,result)

So, in case of web form, which expects input from user. input is not given in the code. What will be the parameters of AssertEquals

Thanks,

1
  • If you want to "unit test" the presentation logic, you'll need to separate your concerns, a pattern like MVP will do this Commented Sep 8, 2012 at 1:07

3 Answers 3

0

Ultimately what you can do (as per jflood.net's comment) is to separate your concerns. So currently you have 2 main concerns (you could probably even break it down further but we should avoid over engineering as well) - Presenting data to the user and validating the users' input.

Your ASP.NET WebForm code handles concern #1. In order to separate the validation concern out you should create a new validation class that handles the validation logic. Something like

public class Validator
{
     public bool ValidateEmail(string email)
     {
          bool valid = false;
          //Run RegEx validation and set the valid flag
          return valid;
     }
}

Now in your page load you can utiliize this new class

 protected void Page_Load(object sender, EventArgs e)
 {
      Validator validator = new Validator();
      string email = Request.Form["EmailInput"];
      bool isEmailValid = validator.ValidateEmail(email);

      if (isEmailValid)
          Response.Write("Email is valid");
 }

Note this is somewhat pseudo code since this is for homework, I didn't want to type up the full solution.

Now you have separated your validation concern into its own class. This makes it easier to understand and easier to unit test.

You will now want to write unit tests for the Validator class, specifically in this case for the ValidateEmail.

Here is an example

  [Test]
  public void ValidateGoodEmail()
  {
     Validator validator = new Validator();
     bool shouldBeTrue = validator.ValidateEmail("[email protected]");

     Assert.AreEqual(true, shouldBeTrue);
  }

  [Test]
  public void ValidateBadEmail()
  {
     Validator validator = new Validator();
     bool shouldBeFalse = validator.ValidateEmail("[email protected]");

     Assert.AreEqual(false, shouldBeFalse);
  }

Remember in unit testing you are just testing the different logical flows. You do not have to test ever input, so here I have tested both a false validation with a given bad email address and a true validation with a good email address.

Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

0

Are you allowed to use web testing frameworks like Selenium or WatiN? If so, you can use them to open the web site in a browser, enter values into the text boxes and look for success for failure text on the page.

1 Comment

i am little familiar with selenium that simulates request, enters user input. but i am thinking how to write test cases without using selenium. and i am not allowed to use selenium or watiN
0

If you cannot use a web testing framework, create a Windows Forms or Windows Presentation Foundation (WPF) application and load the page in a WebBrowser control (Windows Forms Version or WPF version). You can inspect the the DOM and invoke JavaScript on the page that way. A sample for Windows Forms App can be found at http://msdn.microsoft.com/en-us/library/3s8ys666.aspx.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.