Unit Testing is really important to guarantee that the code that we write is work. As a Software Engineer, sometimes dealing with testing can be little bit difficult to read, if we don’t do it in a clean and proper way.
SUT stands for System Under Test, is a system that is being tested. Usually, it can be a class or method, that we want to test the logic, so we can guarantee the code that we write is bug-free.
Why We Need System Under Test Helper Method?
So here is the example of making a test that is little bit hard to understand when we read it right away.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Extra effort for to read, right? Imagine we have more than one method in the Tests class. It will be a huge instance creation repetition. Bad code.
Using System Under Test
In the sample code, we can tell the SUT immediately by creating makeSUT helper method. By refactoring the instance that we want to test, we can see our SUT in an instant.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
And the `makeSUT()` helper method will be like below :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
By using makeSUT helper method, we can simplifytheinstancecreation that we are going to test and it can helps readinga lot better. So it is clear why it is better to use SUT helper method. Happy coding!
iOS Engineer currently working at a tech company, graduated from Apple Developer Academy with experienced iOS Development. Strong software engineering and like to learn, share and craft remarkable and scalable software by following good software engineering disciples and principles that can supports product for long term business.
View all posts by arifinfrds