Sharing Top Content from the Angular-sphere.

What is Unit Testing and Why You Need to Learn It – SLATECODE

  • You can write several hundreds or thousands of unit tests for various parts of your application, and run them all in just a few seconds.
  • When you don’t have unit tests, every time you refactor your code, you have to manually test every part of the application that could be affected by your refactoring.
  • With unit tests, every time you refactor your code, you run your tests and make sure you didn’t accidentally break anything that used to previously work.
  • With unit tests, you make sure that every function works with different inputs under varying circumstances.
  • So, to recap, unit tests help you: – – Test your code frequently and in less time – – Catch more bugs before deploying – – Deploy your application with confidence – – Refactor your code with confidence – – Write cleaner and maintainable code – – You need to…

Here is an example. Imagine you have this function somewhere in your code. It’s a basic calculate function that takes an input and depending on some conditions, it returns different values.

In this post, I’m going to give you a brief introduction to unit testing and its benefits. So, let’s get started!

What is unit testing?

Unit testing is the practice of writing code to test your code and then run those tests in an automated fashion.

Here is an example. Imagine you have this function somewhere in your code. It’s a basic calculate function that takes an input and depending on some conditions, it returns different values.

public float CalculateTax(int input)

if (x) return …;

if (y) return …;

return …;

If you want to test this function manually, you have to run your application, perhaps you have to login, or maybe do a few clicks here and there to get to a page where this function is used. Then, you have to fill out a form, submit it and verify if this function returned the right result. And then you have to repeat all these steps, each time using different values in your form.

Manual testing is expensive!

As you can see, this is very time-consuming. This workflow to test this function may take several minutes every time! Now to make matters worse, this is not the only function in your application. In a real application, you have tens or hundreds of functions like this! As your application grows in size and complexity, the time required to manually test…

What is Unit Testing and Why You Need to Learn It – SLATECODE