In server-side unit testing, there are various testing frameworks and some excellent frameworks in JavaScript, but in this article, we will do our best to implement a simple unit test framework step by step.
At present, in software development, unit testing is increasingly valued by developers. It can improve the efficiency of software development and ensure the quality of development. In the past, unit tests were often used in server development. However, with the gradual division of work in the Web programming field, related unit tests can also be carried out in the front-end Javascript development field, to ensure the quality of front-end development.
In server-side unit testing, there are various testing frameworks and some excellent frameworks in JavaScript, but in this article, we will implement a simple unit test framework step by step.
JS unit testing involves many aspects, including method function check and browser compatibility check. This article focuses on the first one.
The JS Code I checked in this article is a previously written JS date formatting method. The original text is here (the javascript date Formatting Function is similar to the method used in C #). The Code is as follows:
The Code is as follows:
Date. prototype. toString = function (format ){
Var time = {};
Time. Year = this. getFullYear ();
Time. TYear = ("" + time. Year). substr (2 );
Time. Month = this. getMonth () + 1;
Time. TMonth = time. Month <10? "0" + time. Month: time. Month;
Time. Day = this. getDate ();
Time. TDay = time. Day <10? "0" + time. Day: time. Day;
Time. Hour = this. getHours ();
Time. THour = time. Hour <10? "0" + time. Hour: time. Hour;
Time. hour = time. Hour <13? Time. Hour: time. Hour-12;
Time. Thour = time. hour <10? "0" + time. hour: time. hour;
Time. Minute = this. getMinutes ();
Time. TMinute = time. Minute <10? "0" + time. Minute: time. Minute;
Time. Second = this. getSeconds ();
Time. TSecond = time. Second <10? "0" + time. Second: time. Second;
Time. Millisecond = this. getMilliseconds ();
Var oNumber = time. Millisecond/1000;
If (format! = Undefined & format. replace (/\ s/g, ""). length> 0 ){
Format = format
. Replace (/yyyy/ig, time. Year)
. Replace (/yyy/ig, time. Year)
. Replace (/yy/ig, time. TYear)
. Replace (/y/ig, time. TYear)
. Replace (/MM/g, time. TMonth)
. Replace (/M/g, time. Month)
. Replace (/dd/ig, time. TDay)
. Replace (/d/ig, time. Day)
. Replace (/HH/g, time. THour)
. Replace (/H/g, time. Hour)
. Replace (/hh/g, time. Thour)
. Replace (/h/g, time. hour)
. Replace (/mm/g, time. TMinute)
. Replace (/m/g, time. Minute)
. Replace (/ss/ig, time. TSecond)
. Replace (/s/ig, time. Second)
. Replace (/fff/ig, time. Millisecond)
. Replace (/ff/ig, oNumber. toFixed (2) * 100)
. Replace (/f/ig, oNumber. toFixed (1) * 10 );
}
Else {
Format = time. year + "-" + time. month + "-" + time. day + "" + time. hour + ":" + time. minute + ":" + time. second;
}
Return format;
}
No serious bugs have been found in this Code. To test this code. replace (/MM/g, time. TMonth). replace (/MM/g, time. month). The error is that when the Month is less than 10, two digits are not used to represent the Month.
Now there is such a sentence, good design is reconstructed, and in this article, we start from the simplest.
First version: use the original alert
As the first version, we use alert to check the code. The complete code is as follows:
The Code is as follows:
Demo