jsunit ,jsunit test,jsunit testcases

mahesh.Alladi

Description: jsunit mainly used for javascript test cases.

download the jsunit zip folder and extarct the folder.

example: i am testing the validation of name ,password,email.
http://localhost/jsunit/testRunner.ht

testcase run path:

http://localhost/jsunit/tests/profilefieldstest.html

tests folder we will create the testcases.

i created  profilefieldstest.html file.

//html file code start //

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”&gt;
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>

<script type=”text/javascript” src=”../app/testcasesController.js”></script>
<script type=”text/javascript”>
function testUserRegistration()
{
assertEquals(“Incorrect Email Address or Format”,validateEmail(“mahesh@yahoo.com”),true);
// assertEquals(“Incorrect phone number or format “,validatePhone(“1234567891”),true);
assertEquals(“Incorrect password number or format “,validatePassword(“1234567”),true);

}

function testphone()
{

assertEquals(“Incorrect phone number or format “,validatePhone(“1234567891″),true);
}

</script>
</head>

<body>
<h1>JsUnit Failing/Erroring Tests</h1>

<p>This page contains failing and erroring tests. It exists for developers of the framework to experiment with.</p>
</body>
</html>

//end html code//

2) create one js file in app folder  :

created sample file testcasesController.js

code:

function validateEmail(email) {
var error=””;
var tfld = email;                        // value of field with whitespace trimmed off
var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
var illegalChars= /[\(\)\<\>\,\;\:\\\”\[\]]/ ;

if (email == “”) {
error = “You didn’t enter an email address.\n”;
} else if (!emailFilter.test(tfld)) {              //test email for illegal characters

error = “Please enter a valid email address.\n”;
} else if (email.match(illegalChars)) {

error = “The email address contains illegal characters.\n”;
}
else if(error == “”){
return true;
}
else{
return false;
}

}

function validatePhone(phone) {
var error = “”;
var stripped = phone.replace(/[\(\)\.\-\ ]/g, ”);

if (phone == “”) {
error = “You didn’t enter a phone number.\n”;

} else if (isNaN(parseInt(stripped))) {
error = “The phone number contains illegal characters.\n”;

} else if (!(stripped.length == 10)) {
error = “The phone number is the wrong length. Make sure you included an area code.\n”;

}
//return error;
else if(error == “”){
return true;
}
else{
return false;
}
}

function validatePassword(Password) {
var error = “”;
var illegalChars = /[\W_]/; // allow only letters and numbers

if (Password == “”) {
error = “You didn’t enter a password.\n”;
} else if ((Password.length < 7) || (Password.length > 15)) {
error = “The password is the wrong length. \n”;
} else if (illegalChars.test(Password)) {
error = “The password contains illegal characters.\n”;
} else if (!((Password.search(/(a-z)+/)) && (Password.search(/(0-9)+/)))) {
error = “The password must contain at least one numeral.\n”;

}
// return error;
else if(error == “”){
return true;
}
else{
return false;
}