No Result
View All Result
Cloud Reports
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • PC & Laptop
  • Technology & Digital
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • PC & Laptop
  • Technology & Digital
No Result
View All Result
Cloud Reports
No Result
View All Result

What is Unit Test? Introduction and Example about Unit Test

npn by npn
December 24, 2020
in Web development
Reading Time: 5min read
A A
0
What is Unit Test? Introduction and Example about Unit Test

If you do not want to read the theory then scroll down to see examples of how to write unit test and see where you are?

READ ALSO

Javascript developer should stop for … in for now

Javascript developer should stop for … in for now

December 31, 2020
13
How to display colors (color, bgcolor) in console.log () for javascript developers!

How to display colors (color, bgcolor) in console.log () for javascript developers!

December 29, 2020
9

Anyone who has been involved in the software development life cycle with little or no experience has heard of Unit Test. Unit Test is an acceptance mechanism for discovering how well software performs according to specified requirements. Although the purpose of testing is to find defects, it cannot guarantee the absence of other errors, regardless of how creative test cases have been designed.

There are many companies that do not matter this issue but if your Company strictly enforces this issue then I believe and I write where your position is. A programmer who is always close to coding requires test cases at all times.

 This article will give you the following: 

  • What is Unit Test? 
  • Is Unit Test necessary?
  •  What is the disadvantage of Unit Test? 
  • Write Unit Test through example. 

What is #Unit Test?

Unit Test is a method in which individual software components or units are tested to determine their compliance with the designed specifications and also include testing of related data and processes. use. A unit is simply a small piece of code for any function. 

The unit test itself is a short piece of code or code designed to verify the behavior of a particular unit to produce a pass or fail result. The purpose of unit testing is to allow developers to run as many unit tests as possible to identify potential vulnerabilities. Once the application has passed the unit testing, other test forms will then need to be applied for further validation. 

Is #Unit Test necessary? 

The answer is of course necessary for a responsible Programmer with his own lines of code. 

1 – Grow faster.

Once developers write test cases, debugging takes less time and then more confidence in making code changes. Skills will develop in all areas faster than normal Programmers. 

Cautiousness and accountability in unit tests also asserted outside their lives. 

2 – Better Code structure.

As developers write unit tests, their emphasis is on thinking about how their code will be used system-wide, which often leads to better design. 

And there are many other benefits such as reducing work for testers, reducing code costs, helping to reduce costs for future maintenance … 

What downsides does #Unit Test have?

While Unit Test’s benefits are starting to be understood more broadly, there are still some reasons why it is not being fully applied, which has its potential not being realized. 

1 – There is no time for Unit Test.

Writing Unit Test is time consuming which is why it is so difficult to meet deadlines. In fact Unit Test can save a lot of development time and efforts in the long run.

Unit tests are different from coding

Yes, you think that writing a unit test sometimes takes longer than writing a code function. And maybe there are programmers who can write code but not sure to write test cases. There are no guarantees, even if the code is thoroughly tested, there will be no errors. 

# Write Unit Test through example 

Now I will do a little exercise for you to understand how to write a unit test like? 

I will take the full article Capitalizing the first letter of each word in the sentence in the lesson Differences in Experience in Javascript Programming for an example:

Using reduce () increases efficiency, and the main concern is performance. Solution uses reduce () and map ().

const capitalize = (word) => {
  const rest = word.slice(1);
  const firstLtr = word.charAt(0);
  return firstLtr.toUpperCase() + rest.toLowerCase();
}
const titleCase = (phrase) => {
  if(!phrase) return phrase;
  [first, ...rest] = phrase.split(' ');
  return rest.reduce((res,a) =>  res +' ' + capitalize(a),
                                            capitalize(first))
}

Unit Test:

 I wrote a assertion helper function and some test cases. This is not a comprehensive test

const assert = (fun, input, expected) => {
 return fun(input) === expected ?
  'passed' :
  `failed on input=${input}. expected ${expected}, but got ${fun(input)}`;
}

Test cases:

let testCases = [
 {input: "I’m a little tea pot", expected: "I’m A Little Tea Pot"},
 {input: "sHoRt AnD sToUt", expected: "Short And Stout"},
 {input: "sHoRt AnD sToUt", expected: "Short And Stout"},
 { input : "I'm a javascript programmer" , expected : "I'm a Javascript Programmer" },
 {input: "HERE IS MY HANDLE HERE IS MY SPOUT", expected: "Here Is My Handle Here Is My Spout"},
 {input: "", expected: ""},
 {input: undefined, expected: undefined},
]

Do this test, you will pass all the tests:

let testResult = testCases.map(d => assert(titleCase, d.input, d.expected))

console.log(testResult);
output:
//Array(6)
0: "passed"
1: "passed"
2: "passed"
3: "passed"
4: "passed"
5: "passed"

#Conclude

Join us:

One good reason Unit Test is recommended for developers is that quality is a shared responsibility of all stakeholders. Unit Test offers many advantages, although there are some limitations related to it such as being able to only test Unit Test, but not integration or performance issue. But by implementing Unit Test consistently and with rigorous discipline, teams can overcome the limitations and benefit from a more integrated building process. 

ADVERTISEMENT

See at: 

https://www.guru99.com/unit-testing-guide.html

https://areknawo.com/lets-talk-js-unit-testing/

https://blog.testlodge.com/what-is-unit-testing/

ShareTweetShare
Previous Post

If you want to learn React JS within 3 months, the following 8 basics must first be known

Next Post

How to display colors (color, bgcolor) in console.log () for javascript developers!

npn

npn

Related Posts

Javascript developer should stop for … in for now
Javascript

Javascript developer should stop for … in for now

December 31, 2020
13
How to display colors (color, bgcolor) in console.log () for javascript developers!
Javascript

How to display colors (color, bgcolor) in console.log () for javascript developers!

December 29, 2020
9
Accelerate your Continuous Integration pipelines with Directed Acyclic Graph (DAG)
Web development

Accelerate your Continuous Integration pipelines with Directed Acyclic Graph (DAG)

December 22, 2020
3
The FUNDAMENTAL model for learning Web Development.
Web development

The FUNDAMENTAL model for learning Web Development.

December 21, 2020
19
UTM Google, know everything to better use them!
Web development

UTM Google, know everything to better use them!

December 18, 2020
8
Developers and Trades united in 2021?
Web development

Developers and Trades united in 2021?

December 17, 2020
6
Next Post
How to display colors (color, bgcolor) in console.log () for javascript developers!

How to display colors (color, bgcolor) in console.log () for javascript developers!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • Javascript (21)
  • Layout and Routing (2)
  • Linux (3)
  • PC & Laptop (34)
  • React (17)
  • SQL (2)
  • Technology & Digital (124)
  • The Basics (5)
  • Web development (38)

Search

No Result
View All Result
No Result
View All Result
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • PC & Laptop
  • Technology & Digital