DEV Community

Cover image for How to test your code as a Developer?
Pranav for CODEDAMN

Posted on • Originally published at codedamn.com

How to test your code as a Developer?

Testing your code is very important before deploying your code to production. By testing your code every time you ship, you can easily eliminate security and application bugs, then spend hours debugging after shipping to the production.

Static Testing

Let's start from the bottom of the pyramid of testing. The first one is Static Testing. The compiler itself performs this test for statically typed languages like Rust, C, and C++. To know more about static and dynamic types of languages, see here. Static testing can't be found or done well when you come to languages like JavaScript and Python. In the case of JavaScript, you can migrate your codebase to TypeScript to get Static Testing to a certain extent. TypeScript is highly recommended when you are building large projects as it enforces good practices and discipline to the codebase.

Unit Testing

Unit is the small part of the whole. Similarly, the unit here refers to a function in the more extensive codebase that you are writing and maintaining. Rigorously testing a particular function is called unit testing. So the next question is, how do you write unit tests to your code? Technically, there are libraries for unit testing each function of your code, but how they work is the bigger question.

Let's take an example of a function that finds whether a given number is prime or not. Now that you have written the code for the function, it's time to check whether the function works appropriately or not, so how do you do that? Well, you already know in some cases what will be the expected output of the function. You know that when you give 2,3, or 5, that output should be prime. So if you have a set of computed answers already, you can cross-check and evaluate whether the function is working appropriately. From the largest website like WolframAlpha or a graphing website like Desmos, the developers of this website have unit testing operations to test the code before every push to the production so that nothing gets broken when the end-user accesses the website.

Integration Test

The term integration test might be a little intriguing at the start but is very simple regarding how it works. Well, some functions need to work flawlessly when put together, like checking the whole login functionality or something which can't be completed in a single function. Till now, we have discussed three testing methods, and all of them are tested at the code level by another code.

End to End Testing

End to End testing is also called E2E testing. With this, we complete the pyramid of testing. So the tests that we have done will check whether the code that we have written works perfectly and fetches us our desired outputs, but then how would you check whether the UI of the code is functioning and generating the desired output for the user. This is where the End to End testing comes into play.

E2E testing considers your whole application as a black box and simulates a user's behaviour and checks for a given behaviour that the given output is correct or not. These include logging into the application, or creating a new account, checking the display of the graphs, and the most important and delicate part, "the payment gateway". All the tasks mentioned above are high-level and are very important for them to function as required. With the help of the simulation that E2E offers, you can do that.
Input - Output Black Box Testing

The amount of time testing takes

According to the pyramid, you can see that the amount of time the testing takes in each stage increases with the stage level. At the bottom is the static testing, which is handled by the compiler itself for typed languages, and in the case of unit testing, you need a runner, and a library like Jest or mocha would do the job for JavaScript.

Testing Pyramid

Conclusion

Testing is your code is a horrifying experience as a beginner, but it finally pays off in the end. You don't need to debug your applications in production anymore and give bug-free code to the end-user, which is the ultimate goal of any developer. ‌It's never wrong to learn to test, but in fact, it's essential to have minimum tests before passing them to your colleagues or getting ready for the production as well.

You can watch the video on Codedamn YouTube Channel.

Discussion (0)