Unit testing

Lecture



Unit Testing Definition
Unit testing is a software quality improvement method in which individual sections of software code (classes, methods, functions)
tested for adequacy of behavior (no errors, exceptions, correct output parameters).


Unit testing is an example of automated white-box testing.
Unit testing is an example of regression testing.
Unit testing is carried out by developers (development and launch of tests).

  Why unit testing is important
 ● The code is of higher quality from the very beginning.
 ● Fewer errors
 ● Self-description code, documentation
 ● Reducing the cost of correcting errors to eliminate them earlier than later
 ● Safe further refactoring 


  Application Unit Testing 

a piece of software code is being developed (as
rule new method or class).
public class Sort {
private void swap (int [] arr, int i, int j) {
int t = arr [i];
arr [i] = arr [j];
arr [j] = t;
}
public void bubblesort (int [] arr) {
for (int i = arr.length-1; i> = 0; i -) {
for (int j = 0; j if (arr [j]> arr [j + 1])
swap (arr, j, j + 1);
}
}
}
}

Code size should be relatively small. For such sites it is easier to develop unit tests.
● Presence of input / output parameters

Several unit tests are being developed for the site, so that all conditions and cycles are covered, as well as a combination of different input output parameters.

public class SortTestCase extends TestCase {
public void testSwap {
int [] i = {1,2};
Sort sorter = new Sort ();
sorter.swap (i, 0,1);
AssertEquals (2, i [0]);
AssertEquals (1, i [1]);
}
public void testSortWithOrdinaryData () {
int [] i = new int [3] {4,1,2};
Sort sorter = new Sort ();
sorter.bubblesort (i);
AssertEquals (new int [3] {1,2,4}, i);
}
public void testSortWithSOrtedData () {
int [] i = new int [3] {1,2,4};
Sort sorter = new Sort ();
sorter.bubblesort (i);
AssertEquals (new int [3] {1,2,4}, i);
}
public void testSortWithEmptyData () {
int [] i = new int [0];
Sort sorter = new Sort ();
sorter.bubblesort (i);
AssertEquals (new int [0], i);
}
}

Run unit tests via the console

Launch

Unit testing

● Getting results

Unit testing

Run unit tests

● Through the development environment (Eclipse, NetBeans, Idea.

Unit testing

Best practics

● The program code should be divided into small sections with well isolated input and output parameters

● Development of tests in a pair with a program code

● Avoid explicit creation of objects within the program code. Use the Factory pattern instead, as well as the Dependency Injection Frameworks.

● Avoid using global variables.

● Providing the highest test coverage possible.

● Structuring tests by test cases. Each testing method must have a friendly name.

● Accumulation of tests (Regression)

● Running tests after each change in the program code, as well as before commit to the repository.

● Rule 3A (Arrange - Act - Assert)

● To manage dependencies, use the Mock frameworks.

Strengths and limitations of unit tests

Virtues

● Early detection of defects

● Ability to refactor code in the later stages of the project

● Early detection of architectural flaws in system interfaces

● Documentation. Unit tests are a kind of code usage examples.

Restrictions

● Psychological obstacles to developers implementing tests

● Increased application development time

● Permanent support for the performance of tests with any changes in the program code

Unit testing is not Grail and does not allow to catch 100% of defects (out of scope: integration tests, UI tests, load and stress tests).

TDD - Test Driven Development

Unit testing

More about TDD

● Two types of test writing in TDD

● At the very beginning, tests implement the specification of the behavior of the future class through the inputs of the outputs.

● After successful implementation of the code, the tests are adjusted and new ones are added to support all the routes in the code and all boundary input parameters.

TDD Features

● TDD is preferably used where class interfaces are well defined.

● In the case of unstable interfaces, you first need to bring them to the "mind", after which you can implement tests.

Advanced Unit tests

● Handling exceptions

● The method has no explicit output parameters.

● Dependency Management

● Work with DB and network

● Integration tests

Exception Handling

public class DataBaseReaderTestCase extends TestCase {

@Test ( expected = UnauthorizedException. Class ) public void testUnauthorizedAccess () {

DatabaseReader reader = new DatabaseReaderImpl (); reader.getUsersCount ();

}

}

Two points of view on the coverage of class methods

● Paranoid

● All methods, including private ones, should be covered with tests, and all called private methods should be isolated (partial mocking + slims)

● Pragmatic

● All public and private methods should be covered by tests. In this case, the methods invoked are not isolated, but remain as is. For these methods are written separate tests.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Software and information systems development

Terms: Software and information systems development