You could probably meet the deadlines and test the logic behind every LOC. When dealing with a function that incorporates multiple logic paths and various edge cases, the traditional approach of cluttering the function with numerous assert statements for different scenarios can lead to code bloat. This not only makes the code harder to read and maintain but also complicates the testing process.
However, testing conditions that go beyond simple equality comparisons, such as checking if a specific exception is raised, can pose challenges. Unit testing frameworks offer dedicated methods like self.assertRaises() that streamline the verification process
UNIT TESTING
Unit testing is a software testing approach where you test each of the components that you built individually to ensure if what you have intended to have been implemented.In case of huge codebases - Automation testing is much more preferred.
There are different types of unit testing in general :
Different types of Unit Testing Tools:
- JUnit
- TestNG
- NUnit
- Appium
- Mockito
Let's learn more about them
- JUnit
It is an open-source testing framework for java programmers. Being very compatible with multiple IDE’s and platforms, it is widely used. Here is some basic annotations to follow through:
Before : Executes the required before each test
After : Executes the statements after each test
Test : Declares which public void method to be executed as the test case
Test(time out = anything) : Used to set a time out for the test execution
Advantages:
Open source and can be widely accessed with community support
Prefixed annotations to access the test functions
It provides text-based command lines as well as AWT-based and Swing-based graphical test mechanisms
Disadvantages:
JUnits can be a way to crunch away the bugs but can’t be your only way to detect(keep in mind integration and system).
JUnit 5.0 does not support virtual threads as of now. Since the latest Java version(21) supports Virtual threads, the implementation is not out yet
- TestNG
: A collection of TestNG tests together is considered a suite tag.
- The test tag can be given any name and indicates your test sets.
- This is the combination of your package name and test case name and cannot write anything else.
Advantages :
Has a wide range of support for variety of tools and plug-ins
It offers the flexibility to run your tests through multiple data sets through the TestNG.xml file or via the data-provider concept.
TestNG provides a rich set of annotations that allows you to define the test methods, set up and tear down procedures, and more. This simplifies the testing process and makes the code more readable.
Disadvantages:
TestNG does not honor cross-class method dependencies correctly; in cases where a method in one class depends on a method in another class, if the depended-upon method fails, TestNG still executes the dependent method from another class instead of skipping it.
In parallel execution, TestNG does not ensure that methods with dependencies in different classes share the same instance, leading to potential issues where test methods dependent on a common method (testCommon) may run concurrently, rendering the parallel execution with test dependencies less effective.
1. NUnit
If you use the .net framework then NUnit is the right choice for you.This open source platform was initially ported from JUnit. NUnit uses attributes to configure the way that you can write the tests.Here are some basics of how NUnit is configured:
[Test] : Attribute used to define the test methods
Assert.AreEqual : NUnit provides a variety of assertion methods for validating expected outcomes in test methods.
[Order]: control the order of test execution to see if it can run parallelly
[Ignore]: Used to ignore test cases
Advantages:
NUnit has a lot of assertion methods that make it easy to implement the test cases
It supports parallel execution as well,you can enable parallel execution in NUnit by either specifying the --workers option in the console or setting it in the NUnit test runner
configuration.
Disadvantages:
Running a large suite of NUnit tests can consume significant time, impacting the workflow, especially if tests need to be executed frequently during development.
The architecture is not as extensible (eg:xUnit) and is perceived as having a less intuitive syntax for certain advanced features.
Mockito
Mockito is a popular open-source Java framework used for creating and managing mock objects in unit testing. It simplifies the process as you can create mocks of components that you specifically want to test to ensure isolation. This would in turn help you to narrow down much better.
1.[when and thenReturn] : You can define specific behaviors for
method calls on mock objects
2.[verify]: check whether specific interactions with mock
objects have occurred.
Advantages:
Mockito supports flexible mocking of interfaces, abstract classes, and concrete classes.This would help you in creating mocks for a wide range of scenarios, enhancing the testability of their code.
It provides annotations such as @Mock, @InjectMocks, and @spy to simplify the creation and injection of mock objects into test classes, in turn reducing boilerplate code.
Disadvantages:
Severe performance degradation occurred during the migration from Mockito version 1.10.19 to 2.28.2, resulting in a test suite execution time increasing from
Handling complex scenarios, such as mocking static methods or final classes, can be challenging with Mockito. Mostly works well for simple scenarios, but in case of complex ones you would need external libraries to support.