Why Automated Code Testing is Important
Building an automated code test is essential to knowing if your code works and to catch all the bugs in your program. It also saves time in the long run to prevent future crashes in your code. Moreover, errors are better seen in the beginning when you are creating your software and easier to correct rather than sifting through the whole program at the end.When should you test
I've learned that it is better to test right after creating your source code. After you have built your single class and properties it is a best practice to test your methods to avoid problems later. For example, I have built a customer class with several methods, which include: a first name, last name, customer id, email address, address, instance count, retrieve method, save method, and validate method. I have tested for both valid and invalid data that could be entered by the user. See example below. The tests are a valuable and essential part of creating any type of software.Source code
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using LMS.BL;
namespace LMS.BLTest
{
[TestClass]
public class CustomerTest
{
[TestMethod]
public void FullNameValidTest()
{
//Arrange
var customer = new Customer();
customer.FirstName = "Bobby";
customer.LastName = "Baggins";
string expected = "Baggins, Bobby";
//Act
string actual = customer.FullName;
//Assert
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void FirstNameEmptyInValidTest()
{
//Arrange
var customer = new Customer();
customer.LastName = "Baggins";
string expected = "Baggins";
//Act
string actual = customer.FullName;
//Asset
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void LastNameEmptyInValidTest()
{
//Arrange
var customer = new Customer();
customer.FirstName = "Bobby";
string expected = "Bobby";
//Act
string actual = customer.FullName;
//Assert
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void StaticTest()
{
//Arrange
var c1 = new Customer();
c1.FirstName = "Jan";
Customer.InstanceCount += 1;
var c2 = new Customer();
c2.FirstName = "Bob";
Customer.InstanceCount += 1;
var c3 = new Customer();
c3.FirstName = "Dave";
Customer.InstanceCount += 1;
//Act
//Assert
Assert.AreEqual(3, Customer.InstanceCount);
}
[TestMethod]
public void ValidateValid()
{
//Arrange
var customer = new Customer();
customer.LastName = "Baggins";
customer.EmailAddress = "FBaggins@gmail.com";
var expected = false;
//Act
var actual = customer.Validate();
//Assert
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ValidateMissingLastName()
{
//Arrange
var customer = new Customer();
customer.EmailAddress = "BritneySpears@gmail.com";
var expected = false;
//Act
var actual = customer.Validate();
//Assert
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ValidateMissingEmailAddress()
{
//Arrange
var customer = new Customer();
customer.LastName = "Spears";
var expected = false;
//Act
var actual = customer.Validate();
//Assert
Assert.AreEqual(expected, actual);
}
}
}
Very well and good interview question answers of c# asp.net
ReplyDelete