What is unit testing and how does it work?
Jan 19, 2023What is unit testing and how does it work?
Unit testing is a software testing method in which individual units or components of a software application, such as functions or classes, are tested in isolation from the rest of the system. The goal of unit testing is to validate that each unit of the software application is working as intended, and to detect and fix any bugs early in the development process.
Unit tests are typically written by developers as they write the code, and are automated so they can be easily run as part of the software development process. They typically include test inputs, the expected output, and a call to the function or class being tested. The test will then compare the output from the function or class to the expected output, and report any differences as failures.
Unit testing helps to ensure that code changes don't break existing functionality, and can also be used as a form of documentation, as the tests can serve as examples of how the code is intended to be used.
What is an example of unit testing?
An example of a unit test for a function that performs a mathematical calculation might look something like this:
This test includes test inputs, in this case a list of numbers and expected result, a call to the function calculate_average
which is being tested, and a comparison between the output result and the expected output. If the function is working correctly, the assert
statement should not raise an exception, otherwise it will raise an AssertionError and the test will be considered as failed.
Another example could be a class test which looks like this:
Here, we're testing the __init__
method of Person
class which initialize the class with name and age, and the test verifies if it's initialized correctly by comparing the string representation of the object with expected output.
These are just examples, the implementation details will vary depending on the programming language and the testing framework being used.
Positive feedback is feedback too!
We often think of feedback as “critical feedback”, but positive feedback is just as important! Team cohesive and effective teamwork ultimately comes from a place of positivity and a sense of forward/upward momentum. It’s difficult to have these when just focusing on critical feedback. You want to know what you’re doing right along with ways you can improve. So as much as possible, ask for positive feedback like “What did you like about [x] that you’d like to see me continue doing?” and “What was your favorite part about [x]?”
If you want to level up your technical skills and your ability to communicate and collaborate with engineers, enroll in the Skiplevel program. The Skiplevel program is a comprehensive, on-demand course + community that helps you become more technical without learning how to code.
Become more technical without learning to code with the Skiplevel program.
The Skiplevel program is specially designed for the non-engineering professional to give you the strong technical foundation you need to feel more confident in your technical abilities in your day-to-day role and during interviews.